您的位置:首页 > 其它

C language tutorial Chapter fifth: function 3

2015-03-20 16:20 302 查看
Two, the value of the function

The value of the function refers to the function to be called after the execution of the function body, the program segment achieved and returns to the caller function value. If the call has sine sine function, the max function calls in 5.1 cases achieved the
maximum number. On the value of the function (or function return value) has the following description:

1 the value of the function only through the return statement returns. The general form of the return statement for the:

Return expression;

Or for the:

Return (expression);

The statement of the function is to calculate the value of the expression, and returns to the caller. In the function allows multiple return statements, but each call can have only one return statement is executed, therefore can only return a function value.

The type of function types and functions defined in the 2 function values should be consistent. If an inconsistency between the two, with a function type is accurate, automatic type conversion.

3 if the function value is an integer, the function definition may be omitted when the type description.

Function 4 does not return the function value, can be defined as a "free type", "type specifier is void". 5.3 for example function s is not to the main function returning function value, so it can be defined as:

Void s (int n)

{......

}

Once the function is defined as the empty type, not in the main function using the function called function value. For example, in the definition of s null type, write the following sentence in the main function of the sum=s (n) is wrong. In order to make the
program a good readability and to reduce the error, who do not require the return value of the function should be defined as empty types. Function description in the calling function before calling a function in response to the called function are described,
and the use of the variable before the first variable that is the same. For illustration of the called function in the main function of the objective is to make the compiler system know what type of the called function return values, so that in the main function
in this type of the return value for the corresponding treatment. There are two kinds of formats of the called function description, a traditional format, the general format: the type descriptor called function name (); this format is given only the type of
the value returned by the function, adjusting function name and an empty parentheses.

This format without any parameter information in parentheses, therefore not easy to compile the system error checking, prone to error. The other is a modern format, in its general form:

The type specifier is adjustable function name (a type parameter, the type parameter... );

Or for the:

The type specifier is adjustable function name (type, type... );

The modern format given in parentheses the parameter type and parameter names, or just give the parameter type. This facilitates compile system for error detection, to prevent possible errors. Description of Max function with main function if 5.1

With the traditional format can be written as:

Max (int);

With modern format can be written as:

Int max (int a, int b);

Or write to:

Int max (int, int);

C language and rules in the following situations can be explained on the function called function omitted in the calling function.

1 if the return value is adjustable function is integer or character type, can not be adjusted function is stated, and direct calls. Then the system will automatically be adjusted according to the integer processing function return value. The main function
of 5.3 cases were not on the function of s for illustration and direct calls that belong to this kind of situation.
2 when the function definition is adjustable function appeared in the calling function before, in the main function can not be adjusted function explanation and direct calls. For instance in example 5.1, the definition of Max function in the main function
before, so can save function of the max function in the main function that int max (int a, int b).

3 if, before all the function definitions, in the function of pre to indicate the type of each function in the future, each tone function, can no longer on the called function explanation. For example:

Char str (int a);

Float f (float B);

Main ()

{

......

}

Char str (int a)

{

......

}

Float f (float b)

{

......

}

The first, two for the str function and F function previously described. Therefore after each function does not need to be STR and F function as the description can be called directly.

4 pairs of library function call does not need to be explained, but must have the function of the header file with the include command in the source file containing the front. Array as a function of the parameters of the array can be used as a parameter of
the function, data transfer. Array as a function parameter has two forms, one is the array element (subscript variables) as arguments to use; another is the array as a function of the parameter and argument using. An array element, function argument array
element is subscripted variable, there is no difference between it and ordinary variables. Thus as the function arguments used with common variable is exactly the same, in the event of a function call, as the elements of the array argument value is transmitted
to the parameter value, realize unidirectional transmission. Example 5.4 illustrates this situation. [example 5.4] discriminant of each element in an integer value in the array, if more than 0 of the value of the output, if less than or equal to 0 the output
value of 0. Programming is as follows:

Void NZP (int V)

{

If (v>0)

Printf ("%d", V);

Else

Printf ("%d", 0);

}

Main ()

{

Int a[5], i;

Printf ("input 5 numbers\n");

For (i=0; i<5; i++)

{

Scanf ("%d", &a[i]);

NZP (a[i]);

}

}void NZP (int V)

{......

}

Main ()

{

Int a[5], i;

Printf ("input 5 numbers\n");

For (i=0; i<5; i++)

{scanf ("%d", &a[i]);

NZP (a[i]);

}

}

First, a function that has no return value NZP is defined in this program, and explains its parameter V as integer. The body of a function based on the V value corresponding to the output results. With a for statement, each element in the input array main function,
one for each input to the element as the argument to the call a NZP function, that is, the value of a[i] is transmitted to the V parameter, for using the NZP function.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: