Types of Functions in C
In C Programming Language, functions are divided into two parts based on providing the function definition. Those are as follows…
1)System Defined Functions (SDF)
2)User Defined Functions(UDF)
1)System Defined Functions
In C Programming Language, pre-defined functions are provided to make the programming very easy. These functions are also known as built-in functions. These pre-defined functions are called system defined functions.
Simply we can define the pre-defined functions as, the function whose definition is defined by the system are known as system defined function.
Some other synonyms of system defined functions are Library Functions or Standard Functions or Pre-Defined Functions. System defined functions are implemented by the system.
In C programming laguage, all the system defined functions must be defined inside the header files like stdio.h, conio.h, math.h, string.h etc.
As for example:- The funtions printf() and scanf() are defined into the header file called as stdio.h.
Whenever we use system defined functions in the program, we must include the respective header file using #include statement. For example, if we use a system defined function sqrt() in the program, we must include the header file called math.h because the function sqrt() is defined in math.h.
#Memorable facts of System Defined Functions in Brief#
- System defined functions must have to declare in header files
- System defined functions are implemented in .dll files. (DLL stands for Dynamic Link Library).
- When we use system defined functions, the respective header file must be included.
2)User Defined Functions
In C programming language, users can also create their own functions. The functions which are being created by users are known as user defined functions.
Simply, we can define the user defined function as:
-The function whose definition is defined by the user is called as user defined function.
Or
The functions which are created by the user are known as user defined function.
That means implementation of the function by user is called user defined function.
As example:- the function ‘main’ is implemented by the user so it is called as user defined function.
In C programming language,
Every user defined function must be declared and implemented.
Whenever we make function call the function definition gets executed.
For example, let us consider the program given below in which we create a fucntion called addition with two parameters and a return the value.
Example Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, result ;
int addition(int,int) ; // function declaration
clrscr() ;
printf(“Enter any two integer numbers : “) ;
scanf(“%d%d”, &num1, &num2);
result = addition(num1, num2) ; // function call
printf(“SUM = %d”, result);
getch() ;
}
int addition(int a, int b) // function definition
{
return a+b ;
}
In the above example program, the function declaration statement “int addition(int,int)” says the compiler, “ there is a function with name addition that takes two integeral values as parameters and returns an integer value. The function call statement takes the execution control to the additon() definition along with values of num1 and num2. Then function definition executes the code written inside it and returns back to the function call along with return value.
In the concept of functions, the function call is known as “calling function” and the function definition is called as “Called Function”.
When we call a function, the execution control jumps from “calling function” to “called function”. After executing the called function, the execution control returns back to calling function from called function. When the control jumps from calling function to called function it may carry one or more than one data values which is called “Paramenters” and while coming back it can carry a single value which is called “return value“.
That clearifies that the data values transferred from calling function to called function are called as Parameters and the data value jumping from called funcion to calling function is called Return value.
On the basis of flow of data between the calling function and called function, the functions are classified as follows…
- Function without Parameters and without Return value
- Function with Parameters and without Return value
- Function without Parameters and with Return value
- Function with Parameters and with Return value
Function without Parameters and without 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 it comes back to the calling function. For example, consider the following program…
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
void addition() ; // function declaration
clrscr() ;
addition() ; // function call
getch() ;
}
void addition() // function definition
{
int num1, num2 ;
printf(“Enter any two integer numbers : “) ;
scanf(“%d%d”, &num1, &num2);
printf(“Sum = %d”, num1+num2 ) ;
}
Function with Parameters and without Return value
In this type of functions there is data transfer from calling function to called function (parameters) but data doesn’t 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 the data comes back to the calling function. For example, consider the following program…
Example Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2 ;
void addition(int, int) ; // function declaration
clrscr() ;
printf(“Enter any two integer numbers : “) ;
scanf(“%d%d”, &num1, &num2);
addition(num1, num2) ; // function call
getch() ;
}
void addition(int a, int b) // function definition
{
printf(“Sum = %d”, a+b ) ;
}
Function without Parameters and with 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 it returns back to the calling function along with a return value.
For example, let us consider the following program…
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int result ;
int addition() ; // function declaration
clrscr() ;
result = addition() ; // function call
printf(“Sum = %d”, result) ;
getch() ;
}
int addition() // function definition
{
int num1, num2 ;
printf(“Enter any two integer numbers : “) ;
scanf(“%d%d”, &num1, &num2);
return (num1+num2) ;
}
Function with Parameters and with 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.
For example, let us consider the following program…
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2, result ;
int addition(int, int) ; // function declaration
clrscr() ;
printf(“Enter any two integer numbers : “) ;
scanf(“%d%d”, &num1, &num2);
result = addition(num1, num2) ; // function call
printf(“Sum = %d”, result) ;
getch() ;
}
int addition(int a, int b) // function definition
{
return (a+b) ;
}
Some Memorable points about calling & called function
- The parameters specified in calling function are said to be Actual Parameters.
- The parameters declared in called function are said to be Formal Parameters.
- The value of actual parameters is always copied into formal parameters.