A function is a self-contained block or a sub-program of one or more statements that performs a special task when called.
The programs written in C language highly depends upon functions. The C program is nothing but a combination of one or more functions.
Every C program starts with user-defined function main(). Each time when a new program is written, main() function must be defined. The main() calls another function to share the work.
The main program to each function can supply data. A specific operation is performed on the data and value is returned to the calling function.
Any C program contains at least one function.
If a program contains only one function, it must be main( ).
If a C program execution always begins with main( ), so it will necessory to define at least one function.
There is no limit on the number of functions that might be present in a C program.
Each function in a program is called in the sequence specified by the function calls in main( ).
After each function has done its thing, control returns to main( ).When main( ) runs out of function calls, the program ends.
A function get call when the function name is followed by a semicolan( ; )
Any function can be called from any other function. Even main( ) can be called from other functions.
A function can be called any number of times.
A function can call itself. Such a process is called ‘recursion’.
Syntax:
data type function (argument 1, argument 2)
{
//functions body;
}
Example:
#include <stdio.h>
int main()
{
int x = 3;
int x = 6;
z = add`(x,y); // Function Call
printf("z=%d",z);
}
add(a,b); // Function Definition
{
a+b;
}
Output:
z=9
Why use functions ?
If we want to perform a task repetitively, then it is not obligatory to rewrite the particular block of the program again and again. Shift the particular block of statements in an user-defined function. The function defined can be called any number of times depending upon the requirement to perform the task.
Using functions, large size programs can be reduced to smaller ones. It is easy to debug and find out the errors in it. It also increases readability.
How Function Works?
Once a function is defined and called, it takes some data from the calling function and returns a value to the called function.
The detail of inner working of a function is unknown to the rest of the program. Whenever a function is called, control passes to the called function and working of calling function is paused.
When the execution of called function is completed, control returns back to the calling function and executes the next statement.
The values of actual arguments passed by the calling function are received by the formal arguments of the called function.
The number of actual and formal arguments should be the same. Extra arguments are discarded if they are defined.
If the formal arguments are more than the actual arguments, then the extra arguments appear as garbage. Any mismatch in the data type will produce the unexpected result.
The function operates on formal arguments and sends back the result to calling function. The return statement performs this task.