C language Function rookie question and answer

Source: Internet
Author: User
Tags definition function prototype header

1. What is a function

In C, a program segment, subroutine called a function, that completes a particular task, in a complete C program, is usually composed of a number of functions, each of which completes its assigned task.

2. What is a function prototype

A function has the function name, the return value of the function, the type of the return value, the parameter of the function, and the type of the parameter. All of these characteristics of a function are described as a prototype of a function. The function prototype is generally placed at the beginning of the entire program file (internal function). For functions that can be used outside the current source file, it should be explained in a header file that the source file that is to use these functions must contain this header file.

3, what is the return value of the function

When a function is executed, it is passed to a value called a return value. Whether you need to return a value to call the function, depending on the needs of the program, sometimes there is no need to return a value to the calling function.

4, why to explain the prototype of the function

The function prototype tells the compiler what parameters a function accepts and what return value it returns, so that the compiler can check that the call to the function is correct and that there are incorrect type conversions. Cases:

int Some_func (Int,char *,long);

The compiler should check that all calls to the function (including the definition of the function) use three parameters and return a value of type int. If the compiler discovers that the call or definition of a function does not match the function prototype, the compiler should report an error or warning message. For example, for the above function prototype, when the compiler examines the following statement, an error or warning message is reported:

X=some_func (1); /* Number of parameters less * *

X=some_func ("hello!", 1, "dude!"); /* Parameter type Error * *

X=some_func (1,str,2879, "T"); /* Too many parameters *

The following function call is also incorrect because the return value of the function Some_func () is not a value of type long.

Lvalue=some_func (1,str,2879); /* Function return value should be int instead of long. */

The same compiler can also check whether the function's definition (or function body) matches the function prototype. For example, when the compiler examines the following function definitions, an error or warning message should be reported:

int Some_func (char * string,,long lvalue,int ivalue)/* parameter is not in the correct position * *

In summary, describing a function prototype in a source file provides a mechanism for checking whether a function is correctly referenced. Many popular compilers now check whether the prototype of the referenced function has been described in the source file, and if not, a warning message is issued.

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.