A function is a block of statements that is used to solve a specific task or need to perform a same task more than once. In such case you have two options –
a) Use the same set of statements every time you want to perform the task
b) Create a function to perform that task, and just call it every time you need to perform that task.
Here, we use option (b) for the practices and a good programmer always uses functions while writing codes in C.
Types of functions
1) Predefined standard library functions –
such,as
such,as
clrscr()
, getch()
, printf()
, scanf()
etc – These are the functions which already have a definition in header files (.h files like stdio.h, conio.h
), so we just call them whenever there is a need to use them.
2) User Defined functions – The functions that we create in a program are known as user defined functions.
In this section, we will learn how to create user defined functions and how to use them in C Programming
Why we need functions in C
Functions are used because of following reasons –
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by function calls.
Syntax of a function
return_type function_name (argument list){Set of statements – Block of code}
return_type: Return type can be of any data type such as int, double, char, void, short etc. Don’t worry you will understand these terms better once you go through the examples below.
function_name: It can be anything, however it is advised to have a meaningful name for the functions so that it would be easy to understand the purpose of function just by seeing it’s name.
argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables, will be having two integer argument.
Block of code: Set of C statements, which will be executed whenever a call will be made to the function.
How user-defined function works?
#include <studio.h>
void functionName()
{
........
int main()
......
}
{
functionName();
.....
...
}
...
...
The execution of a C program begins from the
main()
function.
When the compiler encounters
void function name()
functionName();
, control of the program jumps tovoid function name()
And, the compiler starts executing the codes inside
functionName()
.
The control of the program jumps back to the
main()
function once code inside the function definition is executed.
In the concept of functions, the function name is known as "Calling Function" and the function definition is known as "Called Function".
When we make a function call, the execution control jumps from calling function to called function. After executing the called function, the execution control comes back to calling function from called function. When the control jumps from calling function to called function it may carry one or more data values called "Paramenters" and while coming back it may carry a single value called "return value". That means the data values transferred from calling function to called function are called as Parameters and the data value transferred from called function to calling function is called Return value.
Type of User-defined Functions in C
There can be 4 different types of user-defined functions, they are:
- Function with no arguments and no return value
- Function with no arguments and a return value
- Function with arguments and no return value
- Function with arguments and a return value
1. Function with no arguments and no return value-
In this type of functions there is no data transfer between calling function and called function. Simply the execution control jumps from calling-function to called function and executes called function, and finally comes back to the calling function.
Example -
#include<conio.h>
void addition(); // function declaration
void main()
{
clrscr();
addition(); // function call
getch();
}
void addition () // function definition
{
int num1,num2,sum;
printf("Enter any two integer numbers : ");
scanf("%d%d",&num1, &num2);
sum= num1+num2;
printf("Sum= %d",sum);
}
Output-
Enter any two integer numbers :
5
7
Sum= 12
Enter any two integer numbers :
5
7
Sum= 12
2. Function with no arguments and a return value-
In this type of functions there is no data transfer from calling-function to called-function (parameters) but there is data transfer from called function to calling-function (return value). The execution control jumps from calling-function to called function and executes called function, and finally comes back to the calling function along with a return value.
#include<stdio.h>
#include<conio.h>
void addition(); // function declaration
void main()
{
int result;
clrscr();
result=addition(); // function call
printf("%d",result);
getch();
}
void addition () // function definition
{
int num1,num2,sum;
printf("Enter any two integer numbers : ");
scanf("%d%d",&num1, &num2);
sum= num1+num2;
return sum;
}
#include<conio.h>
void addition(); // function declaration
void main()
{
int result;
clrscr();
result=addition(); // function call
printf("%d",result);
getch();
}
void addition () // function definition
{
int num1,num2,sum;
printf("Enter any two integer numbers : ");
scanf("%d%d",&num1, &num2);
sum= num1+num2;
return sum;
}
Output-
Enter any two integer numbers :
5
7
Sum= 12
Enter any two integer numbers :
5
7
Sum= 12
3. Function with arguments and no return value-
In this type of functions there is data transfer from calling-function to called function (parameters) but there is no data transfer from called function to calling-function (return value). The execution control jumps from calling-function to called function along with the parameters and executes called function, and finally comes back to the calling function.
void addition(); // function declaration
void main()
void addition () // function definition
Enter any two integer numbers :
#include<stdio.h>
#include<conio.h>
void addition(); // function declaration
void main()
{
int result;
clrscr();
result=addition(); // function call
printf("Sum=%d",result);
getch();
}
void addition () // function definition
{
int num1,num2;
printf("Enter any two integer numbers : ");
scanf("%d%d",&num1, &num2);
sum= num1+num2;
return sum;
}
Output-
Enter any two integer numbers :
5
7
Sum= 12
4. Function with arguments and a return value-
In this type of functions there is data transfer from calling-function to called-function (parameters) and also from called function to calling-function (return value). The execution control jumps from calling-function to called function along with parameters and executes called function, and finally comes back to the calling function along with a return value.
void addition(int a, int b) // function definition
{
return (num1+num2);
}
Output-
Enter any two integer numbers :
5
7
Sum= 12
#include<stdio.h>
#include<conio.h>
void addition(int,int); //function declaration
void main()
{
void main()
{
int num1,num2,result;
clrscr();
printf("Enter any two integer numbers : ");
scanf("%d%d",&num1, &num2);
result=addition(num1,num2); //function call
printf("Sum= %d",result);
getch();
}clrscr();
printf("Enter any two integer numbers : ");
scanf("%d%d",&num1, &num2);
result=addition(num1,num2); //function call
printf("Sum= %d",result);
getch();
void addition(int a, int b) // function definition
{
return (num1+num2);
}
Output-
Enter any two integer numbers :
5
7
Sum= 12