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:
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:
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:
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:
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: