User defined function – free C programming tutorial

In this segment you will be familiarise to types of user defined function available for C programming language.

User defined functions :

C language allow programmers to define function . Such functions which are created by the user are called user-defined functions.
Depending upon requirements of the program, you can create as many user-defined functions as you want.For example:
int add();
int is a return type in the above statement i.e. function add will return an integer value.

Types of user defined function in c :

User defined functions can be further divided into four types of user defined function on the basis if return type and parameters are present or not that are given as following :

  1. Function with no arguments and no return value
    For example :
    void add();
  2. Function with no arguments and a return value
    For example :
    int add();
  3. Function with arguments and no return value
    For example :
    void add(num1 , num2);
  4. Function with arguments and a return value
    For example :
    int add(num1 , num2);

Example of Types of user defined function

Function with no arguments and no return value :

Program illustrating function with no arguments and no return value is given below :

# include <stdio.h>
void add();
int main()
{
   printf(" Executing the function add() ");
   add();
   return 0;
}

void add()
{
   int num1 , num2 , result ;
   printf(" Enter two values....");
   scanf("%d %d", &num1 , &num2);
   result = num1+num2 ;&nbsp;
   printf(" Sum of entered values is %d",result);
}

When the above code is executed and compiled successfully, result given below is obtained.

OUTPUT:

Executing the function add()
Enter two values.... 10 20
Sum of entered values is 30

Function with no arguments and a return value :

Program illustrating function with no arguments and a return value is given below :

# include <stdio.h>
int add();
int main()
{
   int result;
   printf(" Executing the function add() ");
   result = add();
   printf(" Sum of entered values is %d",result);
   return 0;
}

int add()
{
   int num1 , num2 , sum ;
   printf(" Enter two values....");
   scanf("%d %d", &num1 , &num2);
   sum = num1+num2;
   return sum;
}

When the above code is executed and compiled successfully, result given below is obtained.

OUTPUT:

Executing the function add()
Enter two values.... 10 20
Sum of entered values is 30

Function with arguments and no return value :

Program illustrating function with arguments and no return value is given below :

# include <stdio.h>
void add(int,int);
int main()
 {
   int num1 , num2 ;
   printf(" Enter two values....");
   scanf("%d %d", &num1 , &num2);
   printf(" Executing the function add() ");
   add();
   return 0;
 }

void add(int n1 , int n2)
  {
    int result;
        result = n1+n2;
    printf(" Sum of entered values is %d",result);
  }

When the above code is executed and compiled successfully, result given below is obtained.

OUTPUT:

Enter two values.... 10 20
Executing the function add()
Sum of entered values is 30

Function with arguments and a return value :

Program illustrating function with arguments and a return value is given below :

# include <stdio.h>
int add(int,int);
int main()
 {
   int num1,num2,sum;
   printf(" Enter two values....");
   scanf("%d %d", &num1 , &num2);
   printf(" Executing the function add() ");
   sum = add();
   printf(" Sum of entered values is %d",result);
   return 0;
 }

int add(int n1 , int n2)
  {
    int result;
        result = n1+n2;
    return result;
  }

When the above code is executed and compiled successfully, result given below is obtained.

OUTPUT:

Enter two values.... 10 20
Executing the function add()
Sum of entered values is 30

This is all about types of user defined function available for C programming language.

If you have any query please comment below.

Leave a Comment

Your email address will not be published. Required fields are marked *