A function is a block of code that performs a particular task. There are times when we need to write a particular block of code for more than once in our program. This may lead to bugs and irritation for the programmer. C language provides an approach in which you need to declare and define a group of statements once and that can be called and used whenever required. This saves both time and space.
C functions can be classified into two categories,
Library functions are those functions which are defined by C library, example printf(), scanf(), strcat() etc.
You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.
User-defined functions are those functions which are defined by the user at the time of writing program.
Functions are made for code reusability and for saving time and space.
General syntax of function declaration is,
return-type function-name (parameter-list);
Like variable and an array, a function must also be declared before its called.
A function declaration tells the compiler about a function name and how to call the function.
The actual body of the function can be defined separately.
A function declaration consist of 4 parts.
General syntax of function definition is,
return-type function-name (parameter-list)
{
function-body ;
}
The first line return-type function-name(parameter)
is known as function header and the statement within curly braces is called function body.
return type specifies the type of value (int, float, char, double) that function is expected to return to the program calling the function.
function name specifies the name of the function. The function name is any valid C identifier and therefore must follow the same rule of formation as other variables in C.
The parameter list declares the variables that will receive the data sent by calling program. They often referred to as formal parameters. These parameters are also used to send values to calling program.
The function body contains the declarations and the statement(algorithm) necessary for performing the required task. The body is enclosed within curly braces { }
and consists of three parts.
Arguments are the values specified during the function call, for which the formal parameters are declared in the function.
//Write a C Program which add two numbers using UDF (User Defined Function).
#include<stdio.h>
#include<conio.h>
int sum(int a, int b); //function declaration
void main()
{
int n1, n2, ans;
clrscr();
n1 = 5;
n2 = 70;
ans = sum(n1, n2); //function call
printf("Sum of %d and %d is %d", n1, n2, ans);
getch();
}
int sum(int a, int b) //function definition
{
return a+b;
}
C language also allows nesting of functions, one function using another function inside its body.
We must be careful while using nested functions, because it may lead to infinte nesting.
function1()
{
function2();
//statements
}
If function2 calls function1 inside it, then in this case it will lead to infinite nesting, they will keep calling each other. Hence we must be careful.
Recursion is a special of nesting functions, where a function calls itself inside it.
We must have certain condition to break out of the recursion, otherwise recursion is infinite.
function1()
{
function1();
//statements
}
Ask Question