C Function Declaration
Function Declaration and Definition
A function consist of two parts:
- Declaration: the function's name, return type, and parameters (if any)
- Definition: the body of the function (code to be executed)
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.
A function declaration has the following parts −
return_type function_name( parameter list );
For the above defined function max(), the function declaration is as follows −
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so the following is also a valid declaration −
int max(int, int);
Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.
void myFunction() { // declaration
// the body of the function (definition)
}
A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −
- Return Type − A function return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value.
- Function Name − The function name and the parameter list together constitute the function signature.
- Parameters − When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument
- Function Body − The function body contains a collection of statements that define what the function does.