C Language Learning Tutorial fifth chapter-Functions (2)

Source: Internet
Author: User
Tags expression function definition variables printf

General form of function definition

1. General form of non-parametric function
Type descriptor function name ()
{
Type description
Statement
}
Where the type descriptor and function name are function headers. The type specifier indicates the type of this function, and the type of the function is actually the type of the function return value. The type descriptor is the same as the various specifiers described in Chapter two. The name of the function is a user-defined identifier, with an empty parenthesis after its name, with no arguments, but not fewer parentheses. The content in {} is called a function body. There is also a type description in the function body, which is the type description of the variable used within the body of the function. In many cases, the parameterless function is not required to have a return value, at which point the function type character can be written as void.
We can change to a function definition:
void Hello ()
{
printf ("Hello,world \ n");
}
Here, just change main to Hello as the function name, the rest unchanged. The Hello function is a parameterless function that outputs the Hello World string when called by another function.

2. General form of parametric function
Type descriptor function name (Form parameter table)
Description of type parameter types
{
Type description
Statement
}
There are more than two elements in the parameter function than the parameterless function, one is the form parameter table, the other is the formal parameter type description. The parameters given in the formal parameter list are called formal parameters, and they can be variables of various types, separated by commas. When a function call is made, the calling function assigns the actual values of these formal arguments. Since a formal parameter is a variable, it must of course be given a type description. For example, define a function that is used to find the large number in two numbers, which 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 that returns the value of the function as a whole number. The formal parameter is a,b. The second line shows that a,b are all integral types. The specific value of the a,b is sent by the calling function in newsletters. In the function body in {}, no other variables are used except the formal parameters, so there is only a statement and no variable type description. Above this definition method is called "traditional format". This format is not easy to compile system checks, resulting in some very subtle and difficult to track errors. The new standard for ANSI C merges the type descriptions of the formal parameters into the formal parameter list, called the "modern format."
For example, the MAX function can be defined in modern format as:
int max (int a,int b)
{
if (a>b) return A;
else return B;
}
Modern format in function definition and function description (will be introduced later), given the formal parameters and their types, at compile-time easy to check them, so as to ensure the consistency of the function description and definition. This modern format is used in Example 1.3. The return statement in the MAX function body returns the value of a (or b) as the value of the function to the calling function. There should be at least one return statement in the returned value function. In the C program, the definition of a function can be placed in any position, either before the main function main or after main. For example, in Example 1.3, a max function is defined, which is positioned after main, or it can be placed before main.
The modified program is shown below.
int max (int a,int b)
{
if (a>b) return A;
else return B;
}
void Main ()
{
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 analyze the whole program from the angle of function definition, function description and function call, and learn more about the various features of function. The 1th line to the 5th behavior of the program Max function definition. After entering the main function, because you are ready to call the Max function, you first explain the Max function (the 8th line of the program). function definitions and function descriptions are not the same thing, and are discussed later. You can see that the function description is the same as the function header in the function definition, but with the semicolon appended to the end. The 12th behavior of the program calls the Max function and passes the value in the X,y to the parameter a,b of Max. The Max function executes the
The result (A or B) is returned to the variable Z. Finally, the value of z is output by the main function.

The general form of a function call has already been said to execute the function body in a program by calling the function, and its procedure is similar to that of a subroutine call in other languages. In the C language, the general form of a function call is:

The function name (the actual parameter table) does not have an actual parameter table when called without a parameter. Parameters in the actual parameter table can be constants, variables, or other constructed type data and expressions. The arguments are separated by commas. ' Next of page ' in C, you can invoke a function in the following ways:
1. Expression of function
An entry in a function as an expression appears in an expression, and the function returns a value to participate in the expression's operation. This method requires the function to have a return value. For example: Z=max (x,y) is an assignment expression that assigns the return value of Max to variable Z. ' Next of Page '
2. Function statement
The general form of a function call, plus a semicolon, constitutes a function statement. For example: printf ("%d", a); scanf ("%d", &b); all are called functions in the form of function statements.
3. Function arguments
The function appears as the actual argument of another function call. In this case, the return value of the function is passed as an argument, so the function must have a return value. For example: printf ("%d", Max (x,y)); That is, the return value of the Max call is also used as the argument to the printf function. One problem that should be noted in function calls is the question of the order of evaluation. The Order of evaluation refers to whether each quantity in the argument list is used from left to right or from right to left. In this respect, the rules of each system are not necessarily the same. Mentioned in the 3.1.3 section when describing the printf function
Here, I'll emphasize this from the point of view of the function call. See example 5.2 procedure.
void Main ()
{
int i=8;
printf ("%d\n%d\n%d\n%d\n", ++i,--i,i++,i--);
}
such as in the right to left order of evaluation. The running result of example 5.2 should be:
8
7
7
8
If you evaluate ++i,--i,i++,i--from left to right in a printf statement, the result should be:
9
8
8
9
It should be noted that the output order is constant regardless of whether the value is evaluated from left to right or from right to left, that is, the order of output is always the same as the order of the arguments in the argument table. Since Turbo C is now determined to evaluate from right to left, the result is 8,7,7,8. If you don't understand the above questions, you'll get a try on the machine. The value of the function's arguments and functions
Parameters of a function
As discussed earlier, the parameters of a function are divided into two types: formal parameter and real parameter. In this section, the characteristics of the formal parameters, the arguments, and the relationship between the two are further described. The formal parameter appears in the function definition and can be used throughout the function, leaving the function unused. The actual parameter appears in the keynote function, and the actual parametric can not be used after the function is transferred. The function of formal parameters and arguments is data transfer. When a function call is made, the calling function transmits the value of the argument to the parameter of the called function to realize the data transfer of the keynote function to the modulated function.

The formal parameters and arguments of a function have the following characteristics:
1. Parametric allocates memory cells only when called, releasing the allocated memory cells at the end of the call. Therefore, the formal parameters are valid only within the function. When a function call finishes returning the calling function, the parameter variable is no longer available.

2. Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, they must have a definite value to pass the value to the formal parameter when the function call is made. Therefore, the actual parameters should be given a definite value by means of assignment, input and so on.

3. Arguments and formal parameters should be strictly consistent on a quantity, type, or order, otherwise a "type mismatch" error will occur.

4. The data transfer occurring in the function call is one-way. That is, the value of the argument can only be passed to the formal parameter, and the value of the parameter cannot be transferred back to the argument. Therefore, during a function call, the value of the formal parameter changes, and the value in the argument does not change. Example 5.3 can illustrate this issue.
void Main ()
{
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 program defines a function s, the function of which is to find the value of ∑ni=1i. Enter the n value in the main function and, as an argument, call newsletters the form parameter n of the S function (Note that this example's parameter variable and the argument variable have an identifier of N, but this is two different quantities, each of which has different scopes). Output the N value once in the main function with the printf statement, and the n value is the value of the actual parameter n. In function S, a n value is also output with the printf statement, which is the last n value of the parameter 0. From the operation, the input n value is 100. That is, the value of the argument n is 100. When this value is passed to the function s, the initial value of the parameter n is also 100, and in the process of executing the function, the parameter n values become 5050. After returning the main function, the value of the output argument n is still 100. The value of the visible argument does not vary with the shape parameter.

Related Article

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.