Functions in C Programming
A function in C programming is defined as the set of instruction that is design for the purpose to perform a specific task. A function is considered an independent and complete program. A function is executed by the main function or any other function of the program to perform its task.
The functions are written to write the code of the large program by dividing them into smaller units. When the program is very large about more than a hundred lines, it becomes difficult to follow.
So there is a need to divide the whole program into smaller independent units. These smaller independent units are coded as a function.
The function is also written to avoid replication of code in the program. In large programs, one has often interested to execute the piece of code several times the code is written only once as a function and this function is called to execute its code.
The function is of two type’s built-in function and user-defined function.
The functions that have already been defined as a part of the languages and can be used in any program are called built-in functions. The built-in function is provided for general use.
The function which is created by the user is called user-defined functions. These functions are written as a part of a program to perform a specific task. These functions are written for a specific use.
A user-defined function has three parts. These are
- Function Declaration (prototype)
- Function Definition
- Function Calling
The function declaration is also called prototype. Prototype means sample or model. The function declaration only provides the model of the function. Since function declaration provides the model of the function it is called prototype.
The following information is provided to the compiler in the function declaration
- The name of the function
- The type of data which are returned by the function
- The number and types of arguments or parameters used in the function.
To indicate the end of function declaration semicolon is used at the end of the function declaration.
The function declaration is similar to the declaration of a variable. The rules for naming functions are the same as those for naming variables.
Syntax of the function declaration is
Type’s function-name (argument);
Here the type specifies the type of data returned by the function. For example, int is used if the function returns integer type data. Similarly, the float is used if the function is to return floating-point data.
If the function does not return to any value then the word void is used.
The function-name specifies the name of the function.
And the arguments specify the type of parameters separated by commas that are provided to the function.
void display ( void )
int x = 20;
The above function declaration provides the following information about the function.
- Name of the function is displayed
- The return data type is void. It does not return any value.
- List of parameters is void no parameters or arguments are required.
The actual code of a function is called function definition. It is a set of instruction that is written to perform s specific task.
The function definition is always outside the main() function. It can be written before or after the main() function. It can also be written in a separate file. If the function is written in a separate file it is included in the program using the #include directive. As you would notice from the examples the function definition comprises of two parts.
- Body of function
Declarator is the heading line of the function definition. The heading line of the function definition is the same as the function declaration. But you must know that it is not terminated by the semicolon.
Body of function
The set of statements enclosed in braces after the Declarator are called the body of the function. The statement in the body of the function is written to perform a specific task. The general format of a function definition is:
Type function_name ([list of parameters]) } Declarator
Set of statements } Body of function
If you can understand the context of the body of function you can write the program easily. Here in this context, the type represents the data type returned by the function. The void type represents that no data is returned by the function.
Function_name represents the name of the function. It must be the same name that is given in the function declaration.
And the list of parameters represents a list of parameters or arguments which are separated by commas. These parameters collect the data values that are passed to the function. If the function needs no data or parameters then the word ‘Void’ is written or the braces are left blank( ).
Calling a Function
Executing the statement of a function to perfume a task is called calling of the function. As you have seen above that if any parameters of the function are given in parenthesis after the name of the function. If a function has no parameters then the parenthesis is left blank.
If a function has a return value of numeric type then it can be used or called in an arithmetic expression.