C Language Learning Tutorial fifth chapter-Functions (3)

Source: Internet
Author: User
Tags definition empty function definition printf types of functions

Second, the value of the function

The value of a function is the value that is obtained by the program segment in the function body and returned to the calling function after the function is called. If the sine function is invoked to obtain the sine value, the maximum number obtained by the MAX function of Example 5.1 is called. The value of a function (or the return value of a function) has the following descriptions:

1. The value of a function can only be returned by return statement to the melody function. The general form of the return statement is:
return expression;
Or as:
return (expression);
The function of the statement is to evaluate the value of an expression and return it to the calling function. Multiple return statements are allowed in a function, but only one returns statement can be executed per call, so only one function value can be returned.

2. The type of the function value and the type of the function in the function definition should be consistent. If the two are inconsistent, type conversions are done automatically, whichever is the type of the function.

3. If the function value is integral type, you can omit the type description when the function is defined.

4. Functions that do not return function values can be explicitly defined as "null type" and the type specifier is "void". The function s in Example 5.3 does not return the function value to the main function, so it can be defined as:
void s (int n)
{ ......
}

Once a function is defined as an empty type, the function value of the called function cannot be used in the keynote function. For example, after defining s as an empty type, write the following statement in the main function sum=s (n); Is wrong. In order for the program to be readable and to reduce errors, any function that does not require a return value should be defined as an empty type. Function description The called function should be described before invoking a function in the calling function, which is the same as the variable description before the variable is used. The purpose of the tuning function in the keynote function is to make the compiled system know the type of the returned value of the called function, so that the return value can be treated accordingly in the keynote function. There are also two formats for the description of the called function, one is the traditional format, and the general format is: The type descriptor is called the function name (); This format gives only the type of function return value, the name of the function being called, and an empty bracket.

This format is not easy to compile the system for error checking because it does not have any parameter information in parentheses. The other is a modern format, and its general form is:
Type descriptor called function name (type parameter, type parameter ...) ;
Or as:
Type descriptor called function name (type, type ...) ;
The type and parameter names of the formal parameters are given in parentheses in the modern format, or only the formal parameter types are given. This facilitates compiling the system for error checking to prevent possible errors. Example 5.1 A description of the Max function in the main function if
The traditional format can be written as:
int Max ();
It can be written in modern format as:
int max (int a,int b);
or write as:
int Max (int,int);
The C language also stipulates that in the following several cases can omit the function description of the function in the keynote function.
1. If the returned value of the modulated function is integral or character, it can be invoked without a description of the called function. The system will automatically handle the returned value of the called function according to the integral type. This is the case where the function S is not described in the main function of Example 5.3.

2. When the function definition of the modulated function appears before the calling function, it can also be invoked directly in the keynote function without further explanation of the function. For example 5.1, the definition of function max is placed before the main function, so you can omit the function description int max (int a,int b) for the MAX function in the main function.

3. If, before the definition of all functions, the types of functions are described in advance, then the function can no longer be described in the subsequent keynote functions. For example:
Char str (int a);
float F (float B);
Main ()
{
......
}
Char str (int a)
{
......
}
float F (float b)
{
......
}
The first, two lines of the STR function and F function are described in advance. Therefore, no further explanation of STR and F functions can be invoked in subsequent functions.

4. A call to a library function requires no further explanation, but the header file of the function must be included with the include command in the front of the source file. An array of function parameters can be used as parameters of a function to transmit data. An array is used as a function parameter in two forms, one in which the array element (subscript variable) is used as the argument, and the other is to use the array name as the parameter and argument of the function. One, the array elements as function real parameter group element is subscript variable, it is not different from ordinary variables. Therefore, it is used as a function argument is exactly the same as the ordinary variable, in the event of a function call, the value of an array element as an argument is transmitted to the formal parameter to achieve a one-way value transfer. Example 5.4 illustrates this situation. [Example 5.4] to determine the value of each element in an array of integers, output the value if greater than 0, or 0 if it is less than or equal to 0. Programming 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]);
}
}
In this program, we first define a NZP function with no return value, and explain that its formal parameter v is an integer variable. Output the corresponding result in the function body according to the V value. In the main function, enter the elements of the array with a for statement, and each input will call the NZP function with the argument of the element, that is, the value of the a[i] is passed to the parameter V for use by the NZP function.

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.