| Basic concepts | ||||
| Keywords | ||||
| Preprocessor | ||||
| Statements | ||||
| Expressions | ||||
| Initialization | ||||
| Declarations | ||||
| Functions | ||||
| Miscellaneous | ||||
| History of C | ||||
| Technical Specifications |
A function is a C language construct that associates acompound statement (the function body) with anidentifier (the function name). Every C program begins execution from themain function, which either terminates, or invokes other, user-defined or library functions.
// function definition.// defines a function with the name "sum" and with the body "{ return x+y; }"int sum(int x,int y){return x+ y;}
A function is introduced by afunction declaration or afunction definition.
Functions may accept zero or moreparameters, which are initialized from thearguments of afunction call operator, and may return a value to its caller by means of thereturn statement.
int n= sum(1,2);// parameters x and y are initialized with the arguments 1 and 2
The body of a function is provided in afunction definition. Eachnon-inline(since C99) function that is used in an expression (unlessunevaluated) must bedefined only once in a program.
There are no nested functions (except where allowed through non-standard compiler extensions): each function definition must appear at file scope, and functions have no access to the local variables from the caller:
int main(void)// the main function definition{int sum(int,int);// function declaration (may appear at any scope)int x=1;// local variable in main sum(1,2);// function call // int sum(int a, int b) // error: no nested functions// {// return a + b;// }}int sum(int a,int b)// function definition{// return x + a + b; // error: main's x is not accessible within sumreturn a+ b;}
C++ documentation forDeclaring functions |