Definition of function
Function definition is the specification or the program logic that is to be executed when the function has been invoked. Every function must be defined before its invocation. We are including the header files like stdio.h, conio.h into our programs since they consist the definitions of pre-defined functions like printf() and scanf(). Similar to that, we can also define our functions and use them in the same program or include them in other programs in your project.
Function definition consists two parts:
- Header: specifies the attributes of the function
- Body: specifies the executable logic of the function.
Example of function definition:
int add(int x, int y) //header
{//beginning of body
…..
….
}//end of body
The General format of header of the function looks like below
retValType funcName(DataType Var1, DataType Var2,…,DataType VarN)
where
retValType – The data type of the value that is to be returned by the function to its caller
funcName – The identifier or name of the function. Choose an appropriate name to indicate its purpose.
You can see number of items separated with commas inside parenthesis followed by funcName. Those items are known as arguments or parameters those acts as holders for the values supplied by the caller function. In other words, the caller function has to supply values for each every arguments by the time of calling this function.
The ‘void’ data type
The ‘void’ keyword is acting as special data type in C language since it is not used to declare any variables but only used with function definitions. A data type of return value or argument of a function can be any of basic or user-defined data types. Moreover, a function can accept any number of arguments but can return only one value at time. But in case when a function has to be defined so that it is not accepting any arguments or not return any value then ‘void’ keyword is used in the place of missing arguments.
Example:
void funcName(void)
The above header indicating that the function is not going to accept any values or return any.
The return keyword
Default return type of any function is int. It means, if we have omitted void as return type the caller function would expect an integer value from your function.
When a function is returning some value to its caller the return keyword must be used inside its definition. The return statement must be the last statement in the code.
Syntax of return statement:
return;
return (const);
return const;
return (expr);
return expr;