C

User-Defined Function Types In C With Examples – Most Detailed

This tutorial will teach you about various types of user defined functions. We’ll see how to pass arguments and call various variations of these functions.
I strongly advise you to read the following articles on functions:

  • C Programming Functions

Type 1 user defined functions in C:

When a function has no parameters and returns nothing
In this example, we have a user-defined function checkEvenOdd(), which accepts no arguments because the parameter list is empty (notice the empty brackets in function prototype). This function also lacks a return statement, which is why it has a void return type. Because void indicates that this function does not return any value, we do not assign the function call to any variable when calling it in main().

#include <stdio.h>

// This function doesn't return anything
// return type is void
void checkEvenOdd() {
  int num;
  printf("Enter an integer number: ");
  scanf("%d",&num);

  //checking even or odd
  if(num%2==0)
    printf("%d is an even number.", num);
  else
    printf("%d is an odd number.", num);
}

int main() {
  checkEvenOdd(); //no argument passed
  return 0;
}

Type 2: The function has no parameters but returns a value.

Let’s change the code again. This time, the function does not return a void value, but rather a value. The function call must now be assigned to a variable of the same type (type must match return value data type).
Because this method does not have parameters this time, no arguments are passed when calling it.

#include <stdio.h>

// Function returns an integer value
// thus the return type is int
int checkEvenOdd() {

  int num, flag=0;
  printf("Enter an integer number: ");
  scanf("%d",&num);

  //checking even or odd
  if(num%2==0)
    flag = 1; //set flag to 1 if number is even

  return flag;
}

int main() {
  //the function call is assigned to a variable because
  //this time the function returns an int value
  int num = checkEvenOdd(); //no argument passed
  if (num==1)
    printf("Entered number is an even number.");
  else
    printf("Entered number is an odd number.");
  return 0;
}

Type 3: The function has parameters but no return statement.

Because the function checkEvenOdd() lacks a return statement, the return type is void in this case. Because the function has an int parameter, it can take an integer argument. In the main() function, we take user input and pass the entered number as an argument to the checkEvenOdd() function.
The function takes the passed argument and divides it by two; if the remainder is zero, it prints “number is an even number,” otherwise it prints “number is an odd number.”

#include <stdio.h>
//void return type: doesn't return any value
void checkEvenOdd(int n) {
  //printing even or odd based on the remainder after
  //dividing the number by 2
  if(n%2==0)
    printf("%d is an even number.", n);
  else
    printf("%d is an odd number.", n);
}

int main() {
  int num;
  printf("Enter an integer number: ");
  scanf("%d",&num);

  //passing the variable num as argument while calling function
  //no need to assign the function call to a variable as it doesn't
  //return anything.
  checkEvenOdd(num);
}

Type 4: A function with parameters and a return value.

The function in this case returns the value of an int variable flag. The flag value 1 indicates that the entered number is even, while the flag value 0 indicates that the entered number is odd. The if…else statement is used to implement this logic in the main() function.

#include <stdio.h>

//returns an int value
int checkEvenOdd(int n) {
  int flag=0;
  if(n%2==0)
    flag = 1; //set flag to 1 if number is even

  return flag;
}

int main() {
  int num;
  printf("Enter an integer number: ");
  scanf("%d",&num);

  int f = checkEvenOdd(num);
  if (f==1)
    printf("%d is an even number.", num);
  else
    printf("%d is an odd number.", num);
}

Related Articles

Leave a Reply

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

Back to top button