Function of __ function

Source: Internet
Author: User
Tags numeric value

de jure a simple function

function has been used before, here is a program for the function of the description:

#include <stdio.h>

int a=0; Global variables

int main ()

{

void Function1 (); Function 1

int fuction2 (int temp); Function 2

int b=0; Variable

printf ("%d\n", Fuction2 (5));

return 0;

}

void Function1 ()

{

int c=0; Local variables

}

int fuction2 (int temp)

{

temp+=2;

return temp;

}

1, first we see the word global variable in the annotation, in a program there are variables that are valid for the entire program, and we can be identified as global variables, and the variables defined in the function are considered to be valid only in this function, we are called local variables, and variables defined in the loop body or smaller structure, So out of this structure, this variable will no longer be valid.

2, and then we look at the entire structure of the program, the function is declared in the main function, it can be invoked in the main function, or the function is defined in front of the main function, so it is not necessary to declare that the main function can be invoked at execution time, but it is a better method to declare in the main function. This is like the declaration of a header file, but the difference between the two statements is actually the same, and you want to use it in the right place for the life cycle.

3, then is the function of the format: function type function name (parameter type parameter 1, parameter type parameter 2 ...) )

A function type is the value that the function needs to return to the caller at the end of execution, and if it is void, it must be the data type of int after the return in the calling function. As for parameters, the parameters that are put into the program when the function is called are called arguments, and the arguments in the place where the function is declared or defined are called formal parameters.

second, the function parameter problem 2.1 Value Delivery and address delivery

The data stored in the program can be so understood that each value is stored in the corresponding address, then in the function parameter pass, the incoming parameter is a numeric value, then any modification of the value within the function does not affect the value of the argument at the place where the function is called. and address delivery is equivalent to the value of the address as a parameter, then within the function of the value stored in this address will be effective, in the process of learning C + +, we can also know that the use of reference to this function can affect the original value of the role. Here we know that you want to let a function affect an external value internally, in the following ways:

1, address delivery;

2, the reference;

3, using the method of global variable without parameter;

4, the use of the container;

nested calls and recursive calls to 2.2 functions

Nested invocations of functions and loops in loops can be considered, relatively simple, and can be intuitively introduced into the understanding. and recursive call is not necessarily, recursion is a stack, advanced out of the principle, is the soul of dynamic planning, here with recursive implementation of a string forward input, reverse output function, the code is as follows:

#include <stdio.h>

int main ()

{

void recursion (); function declaration

Recursion (); Function call

return 0;

}

void recursion ()

{

char c;

scanf ("%c", &c); Input characters

if (c!=10)//input is entered when entering a carriage return

{

Recursion ();

}

printf ("%c", c); Output

}

The idea is to enter a character one character, when the input stops (this is judged to be carriage return), it begins to output the current character, then ends the current function, jumps back to the previous function, outputs the input character of the previous function, until the end of the program, and realizes the function of the input string reverse output.

iii. scope of variables and storage mode

The scope is the problem that is involved in the use of local variables and global variables, as mentioned in the previous note, where the storage mode is described.

3.1 dynamic storage and static storage

Dynamic storage refers to the use of variables in the execution of the program to allocate the storage unit, the use is completed immediately release. For example, the formal parameter of a function is this way.

Static storage is the allocation of a storage unit when a variable is defined and remains until the end of the entire program. For example, global variables are this way.

This characteristic, which is produced by different storage modes, is called the lifetime of the variable. Lifetimes and scopes describe the characteristics of variables from two different concepts of time and space, both relevant and different. What kind of storage does a variable belong to, and the type of storage outside the scope.

3.2 Storage Type

Key words

Type name

Description

Note

Auto

Automatic type

Default Type

No initial value, initial value is random

Register

Register type

Dynamic storage, in a general-purpose register in the CPU

generally only int, char, and pointer

Static

Static type

The difference between external and internal static variables

is limited to a certain range.

extern

External type

Defines global variables that are outside all functions

Used to share data directly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.