User Defined Functions in C Language

User-defined functions have contained the block of statements which are written by the user to perform a task. In other words we can say that a function created by user is known as user defined function. In this tutorial, we will cover different - different type of user defined functions in C Language.


Example 1: Function with no arguments and no return value
C
#include<stdio.h>

void smallNum();// function declaration

int main()
{

smallNum(); // function call

return 0;
}

void smallNum(){ // function definition
    int i, j;
    printf("Enter 2 numbers to compare: \n");
    scanf("%d%d", &i, &j);
    if(i < j)
    {
        printf("The smaller number is: %d", i);
    }

   else
   {
     printf("The smaller number is: %d", j);
    }
 }

Output :

Enter 2 numbers to compare: 6 9 The smaller number is: 6


Above is an example of smallNum() function, which takes 2 numbers as input from user, and display which is the smaller number.

The return type of the function is void. Hence, no value is returned from the function.




Example 2: No Arguments Passed But Returns a Value
C
#include<stdio.h>
int smallNum(); // function declaration

int main()
{
    int result;
     result = smallNum();  // function call

    printf("The smaller number is: %d", result);

    return 0;
}

int smallNum() // function definition
{
    int i, j, smallNum;
    printf("Enter 2 numbers that you want to compare: \n");
    scanf("%d%d", &i, &j);
    if(i < j)
    {
     smallNum = i;
    }

     else
    {
     smallNum = j;
     }
 // returning the result
  return smallNum;
}

Output :

Enter 2 numbers that you want to compare: 9 23 The smaller number is: 9




Example 3: Argument Passed But No Return Value
C
#include <stdio.h>
void checkPrimeAndDisplay(int n);

int main() {

  int n;

  printf("Enter a positive integer: ");
  scanf("%d",&n);

  // n is passed to the function
  checkPrimeAndDisplay(n);

  return 0;
}

// return type is void meaning doesn't return any value
void checkPrimeAndDisplay(int n) {
  int i, check = 0;

  // 0 and 1 are not prime numbers
  if (n == 0 || n == 1)
    check = 1;

  for(i = 2; i <= n/2; ++i) {
    if(n%i == 0){
      check = 1;
      break;
    }
  }

  if(check == 1)
    printf("%d is not a prime number.",n);
  else
    printf("%d is a prime number.", n);
}

Output :

Enter a positive integer: 67 67 is a prime number.


The integer value entered by the user is passed to the checkPrimeAndDisplay() function.

Here, the checkPrimeAndDisplay() function checks whether the argument passed is a prime number or not and displays the appropriate message.




Example 4: Argument Passed and Returns a Value
C
#include <stdio.h>
int checkPrimeNumber(int n);

int main() {

  int n, flag;

  printf("Enter a positive integer: ");
  scanf("%d",&n);

  // n is passed to the checkPrimeNumber() function
  // the returned value is assigned to the flag variable
  flag = checkPrimeNumber(n);

  if(flag == 1)
    printf("%d is not a prime number",n);
  else
    printf("%d is a prime number",n);

  return 0;
}

// int is returned from the function
int checkPrimeNumber(int n) {

  // 0 and 1 are not prime numbers
  if (n == 0 || n == 1)
    return 1;

  int i;

  for(i=2; i <= n/2; ++i) {
    if(n%i == 0)
      return 1;
  }

  return 0;
}

Output :

Enter a positive integer: 56 56 is not a prime number