C language tutorial Chapter fifth: function 2

The general form of the function definition
1 general non parametric function
The type specifier function name ()
{
The type description
Sentence
}
The type specifier and the name of the function as a function of the head. The type specifier indicates the type of this function, the type of the function is actually a type of function return value. Various descriptor of the type specifier and the second chapter introduces the same. The name of the function is defined by the user identifier, after the function name has an empty parentheses, with no parameters, but the brackets indispensable. {} known as the function of the content. In the function of the type that are also used, this is the type of the function of internal variable description. In many cases do not require nullary function has a return value, this type of function symbols can be written as void.
We can change as a function definition:
Hello (void)
{
Printf ("Hello, world \n");
}
Here, only the main instead of Hello as the function name, the remaining unchanged. The Hello function is a nullary function, when called by other functions, output Hello world string.
A general form of the 2 parameter function
The type specifier function name (formal parameter list)
Formal parameter type description
{
The type description
Sentence
}
A function of the Senate than the nullary function more than two contents, one is the formal parameter list, the second is the formal parameter type description. The parameters are given in the parameter list is called formal parameters, they can be of various types of variables, each parameter between the interval with a comma. In a function call, the calling function will give the formal parameters to the actual value. Since it is the variable parameter, certainly must give the type description. For example, the definition of a function, is used to calculate the large number of two numbers, can be written as:
Int max (a, b)
Int a, b;
{
If (a>b) return a;
Else return b;
}
The first line shows that the max function is an integer function, the return value is an integer function. Parameter for a, B. Second for a, B is integer quantity. A, the specific value of B is composed of a main function is in the call sent. In {} in the body function, in addition to outside the parameters without use of other variables, so only the statement without variable type description. The above definition method called "traditional format". This format is not easy to compile the system check, which will lead to some very subtle and difficult to tracking error. The new standard ANSI C in the type of the parameter that merged into the parameter list, known as the "modern format".
For example, the max function with the modern format can be defined as:
Int max (int a, int b)
{
If (a>b) return a;
Else return b;
}
The modern format specification in function definitions and function (as will be introduced), the formal parameter and given the type, easy to their error checking at compile time, thus ensuring the consistency of the function description and definition. 1.3 cases of using the modern format. The return statement in the body of the max function is to a (or b) values as a function of the value is returned to the caller. The return value should have at least a function in the return statement. In the C program, the definition of a function can be placed in any position, can release before the main function of main, but also can be placed after the main. For example, 1.3 cases were defined a max function, its position in the main after, you can also put it before main.
The modified procedure is as follows.
Int max (int a, int b)
{
If (a>b) return a;
Else return b;
}
Main (void)
{
Int max (int a, int b);
Int x, y, z;
Printf ("input two numbers:\n");
Scanf ("%d%d", &x, &y);
Z=max (x, y);
Printf ("maxmum=%d", Z);
}
Now we can from the function definition, function description and function call point of view to analyze the whole program, further understanding of the various features of the function from the. First to fifth Max function defines the program's behavior. Enter the main function, because of preparing to call the max function, the first on the max function descriptions (eighth lines). The function definitions and function declarations are not the same, behind also devoted. We can see that the function header part of the definition of function description and function the same, but to add a semicolon at the end of. The twelfth act program calls the max function, and the X, the value of the Y is transmitted to the max parameter a, B. The max function implementation
Results (a or b) will return to the variable Z. Finally, by the main function of the output value of Z.
In front of the general form of the function call has said, in the program is to perform the function through the function call, its process and other similar language subroutine call. In the C language, the general form of the function call for:
The name of the function (the actual parameters table) no actual parameters table without the Senate function call. The actual parameters in the table parameters can be a constant, variable or other type of data structure and expression. Use commas to separate between each parameter. 'Next of Page in the C language, can be called in the following several ways of function:
1 function expressions
Function as an expression of the return value in the expression, involved in the expression of computing to function. This approach requires is the return value of the function. For example: z=max (x, y) is an assignment expression, the max returns a value to a variable Z. 'Next of Page
2 function statement
Function call a general form and semicolons that constitute the function statement. For example: printf ("%D", a); scanf ("%d", &b); is a call to function statement function.
3 the function arguments

Function as the actual parameter to another function call. This situation is to return the function value as the argument for transmission, so the requirement of the function must be a return value. For example: printf ("%d", max (x, y)); that is the return value of the max call and as an argument to the use of the printf function

Special attention should be paid to the is, whether it is evaluated from left to right, or from right to left is evaluated, the output sequence is unchanged, namely the output sequence always and argument list in the same order of arguments. Because the Turbo C now is evaluated from right to left, so the result is 8, 7, 7, 8. These problems if not understand, on a test will understand. The parameters of the function and the value of the function
Parameter, function
Front has been introduced, the parameters of the function is divided into parameter and argument two. In this section, the relationship between the two parameters, and further introduces the characteristics of the argument. The parameter appears in a function definition, can be used in the whole body function, leaving the function cannot use. The argument appears in the calling function, enter the called function, argument variables cannot be used. The function of the parameter and the parameter for data transmission. The occurrence of a function call, the calling function to value transmitted to the called function's parameter so as to realize the main function to transmit the data transfer function arguments.
The function's parameter and argument has the following characteristics:
The 1 parameter variable allocates the memory unit only when called, in the call to the end, immediately release the allocated memory unit. Therefore, the parameter is valid only inside the function. The end of the function call returns after you can no longer use the parameter variables.
The 2 argument can be a constant, variable, expression, function and so on, no matter what type of the argument is the amount, in a function call, they must have a definite value, so as to transmit these values to the parameter. Therefore should advance with the assignment, input. So the argument obtained value.
3 arguments and parameters in the number, type, the order shall be strictly consistent, otherwise it will happen "type mismatch errors".
4 occurred in a function call is a one-way data transfer. Only the argument value is transmitted to the parameter, and not the value of reverse transmission for argument parameter. Therefore in the function call in the process, the values of the parameters are changed, the value will not change the argument in the. Example 5.3 to illustrate this problem.
Main (void)
{
Int n;
Printf ("input number\n");
Scanf ("%d", &n);
S (n);
Printf ("n=%d\n", n);
}
Int s (int n)
{
Int i;
For (i=n-1; i>=1; i--)
N=n+i;
Printf ("n=%d\n", n);
}
This procedure defines a function of S, the demand function is the function of the sigma ni=1i value. Enter the n value in the main function, and as an argument in the call, transmitted to the s function of the shape parameter n (attention, in this case the identifier parameter variables and the argument variables are n, but this is two different amounts, their scope is different). Use printf statements to output a n value in the main function, the n value is the value of the n argument. In the function of s also use printf statements output a n value, the value of the n parameter is finally obtained the n value of 0. From the operating situation, enter the n value is 100. That is the argument n = 100. This value is passed to the s function, the initial value of parameter n is also 100, in the course of execution of the function, the parameter n value is changed into 5050. After the return to the main function, the output value of n is 100 argument. Visible argument values do not vary with the parameter changes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值