The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

Types of Functions in C

In C Programming Language, based on providing the function definition, functions are divided into two types. Those are as follows...

  • System Defined Functions
  • User Defined Functions

System Defined Functions

The C Programming Language provides pre-defined functions to make programming easy. These pre-defined functions are known as syatem defined functions. The system defined function is defined as follows...

The function whose definition is defined by the system is called as system defined function.

The system defined functions are also called as Library Functions or Standard Functions or Pre-Defined Functions. The implementation of system defined functions is already defined by the system.

In C, all the system defined functions are defined inside the header files like stdio.h, conio.h, math.h, string.h etc., For example, the funtions printf() and scanf() are defined in the header file called 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.

Points to be Remembered

  • System defined functions are declared in header files
  • System defined functions are implemented in .dll files. (DLL stands for Dynamic Link Library).
  • To use system defined functions the respective header file must be included.

User Defined Functions

In C programming language, users can also create their own functions. The functions that are created by users are called as user defined functions. The user defined function is defined as follows...

The function whose definition is defined by the user is called as user defined function.

That means the function that is implemented by user is called as user defined function. For example, the function main is implemented by user so it is called as user defined function.

In C every user defined function must be declared and implemented. Whenever we make function call the function definition gets executed. For example, consider the following program in which we create a fucntion called addition with two paramenters and a return 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)" tells the compiler that there is a function with name addition which takes two integer 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 comes 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 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 funcion to calling function is called Return value.

Based on the data flow 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 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 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. 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 comes back to the calling function along with a return value. For example, 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, 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) ;
}

Points to be Remembered

  • 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.

Place your ad here
Place your ad here