C language Function description and return value

Source: Internet
Author: User
Tags error code integer numeric mul numeric value printf valid

Before learning the C language function, we need to understand what a modular programming method is.
When people solve a complicated problem, they usually adopt the method of gradual decomposition and divide and conquer, which is to decompose a big problem into several small problems which are easy to solve, and then solve them separately. Programmers in the design of a complex application, it is often the whole program into a number of more than a single program module, and then implemented separately, and finally all the program modules like building blocks, such as assembly, the design of the divide and conquer strategy, known as modular programming method.
In C language, function is the basic unit of program, so it is convenient to use function as program module to implement C language program.
Using functions, we can not only realize the modularization of the program, programming is simple and intuitive, improve the readability and maintainability of the program, but also can be commonly used in the program to use some of the calculations or operations into a common function for the call at any time, this can greatly reduce the programmer's code workload.
function is the basic component of C language, is the stage of all program activities. The general form of the function is:
Type-specifier function_name (parameter list)
Parameter declarations
{
The body of the function
}
A type specifier defines the type of return statement value returned by a function that can be any valid type. If no type descriptor appears, the function returns an integer value. A parameter table is a comma-delimited list of variables that receive the value of the calling parameter when the function is invoked. A function can have no arguments, and the function table is empty. But even without arguments, parentheses are still required. The parameter description segment defines the type of the parameter.
When a function does not explicitly describe the type, the C language compiler automatically takes the integral type (i n T) as the default type of the function, and the default type applies to a large part of the function. When it is necessary to return other types of data, there are two steps to be processed:
First, you must give the function an explicit type descriptor, and secondly, the description of the function type must precede the first call to it. Only in this way can the C compiler generate the correct code for a function that returns a value of a non integral type.
Description of the type of the 4.1.1 function
The function can be described as returning any valid C language data type.
The type descriptor tells the compiler what type of data it returns. This information is highly related to whether the program works correctly because different data have different lengths and internal representations.
Before a function that returns a non integer data is used, the type must be described to the remainder of the program. If you do not do this, the C language compiler will assume that the function is a function that returns the integer data, and the call point and before the function type description, the compiler generates an error code on the call. To prevent the above problems, you must use a special statement that tells the compiler what value the function returns. The following example shows this method.

The type of the first function indicates that the sum () function returns data of the floating-point type. This description enables the compiler to generate the correct code for the call to sum ().
The general form of a function type description statement is:
Type_specifier function_name (;)
Even if a function uses a formal parameter, do not write it to a descriptive sentence. If a type description statement is not used, the data type returned by the function may be inconsistent with what the caller requires, and the result is unpredictable. If the two are in a single file, the compiler can discover the error and stop compiling. The compiler cannot discover this error if it is not in the same file. Type checking is done only in compilation, and neither link nor runtime checks. It is therefore important to be very careful to ensure that such errors do not occur.
When a function that is described as an integer returns a character, the character value is converted to an integer. Because the C language converts data between character and integer in an otherwise descriptive manner, in most cases the function that returns a character value is not described as a return character value, but implicitly implemented by the conversion of this character of the function into the default type of the integral type.
4.1.2 Return statement
The return statement r e T u r N has two important uses. First, it causes the function that contains it to exit immediately, which means that the program returns to the calling statement where it continues. Second, it can be used to echo a numeric value. This chapter will describe both uses.
1. Return from function
A function can stop running in two ways and return to the calling program. The first is that after executing the last statement of the function, conceptually, it encounters the Terminator "}" of the function (which, of course, does not actually appear in the target code, but we can understand it). For example, the following function displays a string on the screen.

Once the string is displayed, the function has nothing to do, and then it returns to the used-for-use.
In reality, there are not many functions that are terminated in this default way. Because sometimes a value must be returned, most functions terminate with the return statement, and sometimes multiple endpoints are set up in the function to simplify the function and improve efficiency. Remember that a function can have multiple return statements. As shown below, the function returns 1 when it is equal to S 1, s 2, and returns-1 when not equal.

2. Return value
All functions, except for null value types, return a numeric value (remember that null values are extensions to the ANSI recommended standard and may not be appropriate for the reader's C compiler). The value is determined by the return statement. When no return statement is returned, the return value is 0. This means that as long as the function is not described as NULL, it can be used in any valid C-language expression
is the operand. The following expressions are all valid C language expressions.
x = Power (y);
if (max (x,y) >100) printf ("Greater;")
For (Ch=getchar (); isdigit (ch);). ;
However, the function cannot be an assignment object and the following statement is incorrect:
S w a p (x, y) = 100;
The C compiler will consider the statement to be incorrect and not compile the program that contains the error statement.
All functions that are non-null values return a value. Most of the functions we write are of three kinds. The first type is a simple computational type-a function designed to operate on a variable and return a calculated value. A computed function is actually a "pure" function, such as SQR () and sin (). The second type of function processes information and returns a value that represents only
Success or failure of processing. such as write (), which is used to write information to disk files. If the write operation succeeds, write () returns the number of bytes written, and when the function returns-1 o'clock, the flag write fails. The last class of functions does not have an explicit return value. In fact, such functions are strictly process-type functions and do not produce a value. If the reader is using a C compiler that conforms to a N S I recommendation standard, then all such functions should be described as null value types. Strangely, functions that do not produce interesting results have to return something anyway. For example, printf () returns the number of characters that are being written. However, it is difficult to find a program that actually checks the return value. Therefore, although all functions except NULL functions return a value, we do not have to use this return value. A common question about the return value of a function is: Since the value is returned, do I have to assign it to a variable? The answer is: no need. If it is not assigned a value, it is discarded. Take a look at the following program, which uses the Mul () function. The Mul () function is defined as: int mul (int x, int y) {...}

In the first line, the return value of Mul () is assigned to Z, in the second row, the return value is not actually assigned to any variable, but is used by the printf () function. Finally, in the third row, the return value is discarded, because it is neither assigned to the first variable nor used as part of an expression.

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.