Defining and Calling C language functions

First, the definition function

We need to determine the definition of a function of three parts:

1. The return type of the function

2. The name of the function

3. function parameters

Identified above can be defined after a function of the unique features:

int wrongplus(int a, int b)

{

a = a + 1;

b = b + 1;

return a + b;

}

For example, the above code is defined a function returns a value of type int, which is a function named wrongplus, the contents of the pair of parentheses is the function argument, that there are two passing function parameters and int int a b. In vivo function, were added to make a 1 on the basis of the original, so that b plus 1 on the basis of the original, and then using the return key and returns a + b. If the value of a is 2, and the value of b is 3, then the return value of this function is 7.

If we want to define a no return type of function you need to design its return value type is void, the same, if we want to define a function with no parameters can be parameter is defined as void, usually if a function has no parameters can be a function name in in parentheses after nothing to write, for example:

void function1(void)

{

}

void function2()

{

}

Second, the function calls and declarations

We define such a wrongplus () function, then how to use it to make other features we see the following procedures?:

#include <stdio.h>

main()

{

int a = 2, b = 3;

int c;

int wrongplus(int a, int b);

c = wrongplus(a, b);

printf("%d + %d = %d\n",a, b, c);

}

int wrongplus(int a, int b)

{

a = a + 1;

b = b + 1;

return a + b;

}

2 + 3 = 7

If the function is defined in the call back function (below) you will need to declare defined function before the call, otherwise without prior declaration. Statement mean to tell the compiler, return type, function name and the parameters of this function.

When we call a function, it needs to pass parameters to the desired functions, such as the two variables a and b, and the other variable can be specified for receiving the return value of the function after the execution of such a function on the reception of the variable c wrongplus () return value, the result is 7. But we see the results of the procedure is 2 + 3 = 7, that is, executes the function wrongplus (), although we parameters are variable within the function plus 1, but the main function of the values ​​of a and b It has not changed.

Third, the real parameter participating

When we call the function, it received its incoming parameters and functions in vivo parameters is actually not the same variable. When the function is called a function call parameters passed actual parameters, referred to as arguments, and a function of an incoming internal call parameters thereof for receiving an external parameter called formal parameters, referred to as parameters.

For example, in the above example, the variables defined in the main function int a and int b, call wrongplus (a, b); when the argument is a two variables, i.e. the value of these two variables passed to the 2 and 3 internal function. In a functional wrongplus () function in vivo, for receiving int when these two parameters a and int b is a parameter, which is the functions that two additional variables, for receiving two values ​​2 and 3 , the two variables a and b main function is not defined.

In wrongplus () function in vivo, to make the parameter a = a + 1; and b = b + 1; operation, in fact, affects only the interior of the body of the function of these two variables, but did not affect the main function of the variable a and b values. Therefore, in wrongplus () a function of the body, and b is 3 and 4, to return the results of running a function value of 7, and the value a in the main function of two variables, and b is 2 and 3 is still, so printf ( ) shows the result of the function is 2 + 3 = 7.

Published 261 original articles · won praise 4 · Views 4260

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105206380