In this tutorial, we are going to learn about the User defined functions in C.
User-defined functions are the functions that are defined by the user at the time of writing the program. Functions are made for code re-usability and for saving time and space.
In C, main() is the user-defined function and first calling function in any program. main() is a special function that tells the compiler to start the execution of a C program from the beginning of the function main().
An identifier other than keywords followed by parenthesis is recognized as a function name by the compiler.
To make use of the user-defined function the programmer must be able to know the following 3 concepts.
- Define a Function (or) Function Definition
- Function Prototype (or) Function Declaration
- Calling a Function (or) Invoke a Function
A function definition describes what a function does, how its actions are achieved, and how it is used. It consists of a function header and a function body.
The general format of defining a function is
return_type function_name(parameters list) {
//Local variable declarations
//Executable statements
return(expression);
}
The first line which heads the function is known as the function header. The function header should not end with a semicolon (;) in defining a function.
The function body follows the function header and it is always enclosed in braces. The body of the function is a combination of local variable declarations and executable statements.
Here the statements describe the actions to be performed by the function. The body of function definition is also known as a block or compound statement. The elements specified within the parenthesis of the function name are known as parameters or arguments.
The general format of defining a function is
return_type function_name(parameters list) {
//Local variable declarations
//Executable statements
return(expression);
}
The return_type specifies the data type of the value returned by the function. The return_value may be of the primitive data type or empty data type.
If return_type is omitted then the default return_type of any function is int. A void is specified in the place of return_type if the function returns no value. function_name is any valid identifier. It can’t begin with an underscore because such names are reserved for the use of the C library.
The arguments in the parameter_list are known as formal parameters. Zero or more parameters may be used. Each parameter must be preceded by its data type.
More than one parameter must be separated by commas. Parameters are used to pass the values into the function definition. For parameterless functions, the keyword void is placed within the parenthesis of the function name. It consists of the keyword return followed by an expression within the block and it returns only a single value to the calling function when the function returns a value.
A value or an expression may follow a return if the function returns a value; otherwise, nothing follows return. The syntax of the return statement is
return(expression); (Or) return expression;
The following points must be kept in mind while defining a function.
- A function cannot be defined more than once in a program.
- One function cannot be defined within another function definition.
- Function definitions may appear in any order.
- Built-in functions are predefined and they are available in the C library that is supplied along with the compiler.
Whenever a function is invoked within another function it must be declared before use. Such declaration is known as function declaration or function prototype.
Function declaration always ends with a semicolon (;). The general format of the function prototype is
return_type function_name(parameter_list);
In the above function declaration, the parameter names in the parameter_list are optional.
Hence, it is possible to have the data type of each parameter without mentioning the parameter name as shown below.
return_type function_name(data_type, data_type, data_type,...., data_type);
A parameterless function is declared by using void inside the parenthesis as
return_type function_name(void);
For user understandability, all the function declarations are specified before the main() function. A function is invoked to make use of it. The general format of a function call is
function_name(var_1, var_2 .... var_n);
Where var_1, var_2,…, var_n are argument expressions. For a parameterless function, there is no argument in the function call also.
The arguments var_1, var_2,…, var_n in a function call is called actual arguments.
If a function returns a value, the function call may appear in any expression, and the returned value used as an operand in the evaluation of the expression.
REFERENCES:
Happy Learning 🙂