A function is a block of codes that performs a specific task and may return value. The main() function is the first user defined function invoked by the compiler. A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.
Definition of function
A function definition contains function name, parameters, its code and return type.
- Function Name – This is the actual name of the function. The function name and the parameter list together constitute the function signature.
- Return type – Including main All functions can return a value. Void Return type is specified if the function is returning no value. Functions can return arithmetic values (int, float, etc.), pointers structures, unions, or will not return anything (void). But they cannot return an array or a function.
- Function Parameters – Any function (as well as main) can receive some values called parameters. While calling a function we must pass values of parameters.
< return_type> < function_name>(arguments/parameters );
Definition in C programming language is as follows −
/* Declaration of area */
int area(int x, int y);
int x=8, y=5;
Declaration of function:
A function declaration contains only the name and return type. User can define a function only once but it can be declared several times.
< return_type> < function_name>(arguments)
/* Declaration of area*/
int area(int x, int y)
z = x*y;
Functions are able to accept input parameters in the form of variables. These input parameter variables can then be used in the function body.
There are two ways in which arguments can be passed to a function.
Call by value:
Function call by value is the default way of calling a function in C programming.
Call by reference:
When we call a function by passing the addresses of actual parameters then this way of calling the function is known as call by reference.