BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Types of User-defined Functions in C with Examples

Last Updated: September 14, 2022 by Chaitanya Singh | Filed Under: c-programming

In this tutorial, you will learn various types of user defined functions. We will see examples of how to pass arguments and call different variations of these functions.

I highly recommend you to also read these articles on functions:

  • Functions in C Programming
  • User defined functions in C

Type 1: When function doesn’t have parameters and returns nothing

In this example, we have a user defined function checkEvenOdd(), this function doesn’t accept any arguments as the parameter list is empty (notice the empty brackets in function prototype). Also, there is no return statement in this function, which is why it has a void return type. void indicates that this function doesn’t return any value, this is why while calling this function in main(), we are not assigning the function call to any variable.

#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;
}

Output:
User defined function with no arguments and no return statement

Type 2: Function doesn’t have parameters but returns a value

Let’s modify the same code again. This time, the function doesn’t have void return type instead it is returning a value. Now, we need to assign the function call to a variable of the same type (type must match return value data type).
Since this time also, this method doesn’t have parameters, thus no arguments are passed while calling this method.

#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;
}

Output:
User defined function with return statement and no args

Type 3: Function has parameters but doesn’t have a return statement

In this case, the return type is void as the function checkEvenOdd() doesn’t have a return statement. The function has an int parameter, thus it can accept an integer argument. In the main() function, we are taking input from the user and passing the entered number as an argument while calling the checkEvenOdd() function.

The function uses the passed argument and performs a check on the number by dividing the number by 2, if the remainder is zero, it prints “number is an even number” else 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);
}

Output:
User defined function with parameters and no return statement

Type 4: Function has parameters and returns a value

In this case, the function returns value of an int variable flag. The flag value 1 represents that the entered number is even and 0 value represents that the entered number is odd. This logic is implemented in main() function using if..else statement.

#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);
}

Output:
User defined function has parameters and returns a value

❮ PreviousNext ❯

Top Related Articles:

  1. C – Pointer to Pointer (Double Pointer) with example
  2. Functions printf() and scanf() in C
  3. Function call by reference in C Programming
  4. If statement in C programming with example
  5. C – loops in C programming with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap