Types of functions in C

A function is a sub-part of a program that contains a collection of statements grouped together to perform some specific task. Functions in C programming is categorized in two category –

  1. Library function
  2. User defined function

Library functions

Function defined by the C distributors and are included with C compilers are known as library functions. These functions are built-in, pre-compiled and ready to use.

Since the first day of programming, you have used many library functions. Functions such as printf(), scanf(), pow(), sqrt() etc. are part of C standard library functions. These functions are defined in C header files. We include these header files in our program as per our need.

User defined functions

Despite of having hundreds of library function, C allows programmers to define their own function. Functions defined by an end programmer is known as user defined function. A programmer can define any number of function depending on the need.

You can also compile a set of functions as library function and can use them later in another program.

Based on prototype, user defined function are further categorized in four categories.

  1. Function with no return and no argument
  2. Function with no return but arguments
  3. Function with return but no argument
  4. Function with return and arguments

Function with no return no argument

Function with no return no argument, neither returns a value nor accepts any argument. This type of function does not communicate with the caller function. It works as an independent working block. Use this type of function when you have some task to do independent of the caller function.

Note: You must add void as function return type for this type of function.

Syntax to define function with no return no argument

void function_name()
{
    // Function body
}

Example of function with no return no argument

Write a C function to input a number and generate Fibonacci series.

/**
 * C program to generate Fibonacci series using function
 */
#include <stdio.h>

/* Function declaration */
void generateFibo();

int main()
{
    generateFibo();
    return 0;
}

/* Function definition */
void generateFibo()
{
    int a, b, c, i, terms;

    /* Input a number from user */
    printf("Enter number of terms: ");
    scanf("%d", &terms);

    a = 0;
    b = 1;
    c = 0;

    printf("Fibonacci terms: \n");

    // Iterate through n terms
    for(i=1; i<=terms; i++)
    {
        printf("%d, ", c);
        a = b;      // Copy n-1 to n-2
        b = c;      // Copy current to n-1
        c = a + b;  // New term
    }
}

Output –

Enter number of terms: 10
Fibonacci terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

In the generateFibo() function, void keyword specifies the function will not return any value. Also since the function does not accept any argument, hence parameter list inside parenthesis() is blank.

Function with no return but with arguments

Function with no return but with arguments, does not return a value but accepts arguments as input. For this type of function you must define function return type as void.

Syntax to define function with no return but with arguments

void function_name(type arg1, type arg2, ...)
{
    // Function body
}

Where type is a valid C data type and argN is a valid C identifier.

Example program of function with no return but with arguments

Write a C function to accept two arguments start and end. Print all natural numbers between start to end.

/**
 * C program to print natural numbers using functions
 */

#include <stdio.h>

/* Function declaration */
void printNaturalNumbers(int start, int end);

int main()
{
    int s, e;
    printf("Enter lower range to print natural numbers: ");
    scanf("%d", &s);
    printf("Enter upper limit to print natural numbers: ");
    scanf("%d", &e);

    printNaturalNumbers(s, e);

    return 0;
}

/* Function definition */
void printNaturalNumbers(int start, int end)
{
    printf("Natural numbers from %d to %d are: \n", start, end);
    while(start <= end)
    {
        printf("%d, ", start);
        start++;
    }
}

Output –

Enter lower range to print natural numbers: 5
Enter upper limit to print natural numbers: 10
Natural numbers from 5 to 10 are:
5, 6, 7, 8, 9, 10,

In printNaturalNumbers(int start, int end) function, start and end are two parameters passed to the function. The value of s and e is copied and passed to start and end respectively.
In addition the void keyword specifies that the function will not return any value to its caller.

Function with return but no arguments

Function with return but no arguments, returns a value but does not accept any argument. This type of function communicates with the caller function by returning value back to the caller.

Syntax to define function with return but no arguments

return_type function_name()
{
    // Function body

    return some_value;
}

Where return_type and some_value must be declared with same type.

Example program of function with return but no arguments

Write a C function that returns a random prime number on each function call.

/**
 * C program to print random prime numbers
 */

#include <stdio.h>
#include <stdlib.h> // Used for rand() function

/* Function declaration */
int randPrime();

int main()
{
    int i;
    printf("Random 5 prime numbers are: \n");
    for(i=1; i<=5; i++)
    {
        printf("%d\n", randPrime());
    }

    return 0;
}

/* Function definition */
int randPrime()
{
    int i, n, isPrime;
    isPrime = 0;

    while(!isPrime)
    {
        n = rand(); // Generates a random number

        /* Prime checking logic */
        isPrime = 1;
        for(i=2; i<=n/2; i++)
        {
            if(n%i==0)
            {
                isPrime = 0;
                break;
            }
        }

        if(isPrime ==1)
        {
            return n;
        }
    }
}

Output –

Random 5 prime numbers are:
41
491
14771
25667
28703

In the above program int randPrime() is a function. It returns an int value i.e. a random prime number. The function does not accept any parameter from user.

Function with return and arguments

Function with return and arguments, returns a value and may accept arguments. Since the function accepts input and returns a result to the caller, hence this type of functions are most used and best for modular programming.

Syntax to define function with return and arguments

return_type function_name(type arg1, type arg2, ...)
{
    // Function body

    return some_variable;
}

Example program of function with return and arguments

Write a C program to input a number and check the input is even or odd. Define a function that accepts the number and return a value 0 or 1. Return 0 if argument is even, otherwise return 1.

/**
 * C check even odd using function
 */

#include <stdio.h>

/* Function declaration */
int evenOdd(int num);

int main()
{
    int num, isEven;

    printf("Enter a number: ");
    scanf("%d", &num);

    /* Function call */
    isEven = evenOdd(num);
    if(isEven == 0)
        printf("The given number is EVEN.");
    else
        printf("The given number is ODD.");
    return 0;
}

/* Function definition */
int evenOdd(int num)
{
    /* Return 0 if num is even */
    if(num % 2 == 0)
        return 0;
    else
        return 1;
}

Output –

Enter a number: 5
The given number is ODD.

int num in function int evenOdd(int num) specifies that the function accepts an integer parameter. The function returns an integer value either 0 or 1, based on even or odd condition.

Which is the best function type for my program?

Choosing a correct function type completely depends on the programming need.

  • Consider function with return and arguments. If your function requires input from other functions and returns the processed result back to the caller function. The returned value might be input for other function.

    For example – double sqrt(double x), double pow(double x, double y) etc.

  • Consider function with return but no argument. If your function does not require any input from other function. Rather, it just produce some output that may be input to other functions.

    For example – int rand(void), int getchar(void) etc.

  • Consider function with no return but arguments. If your function is dependent on the input of some other function. But, does not generates any output that could be consumed by the caller function.

    For example – void free(void *ptr), void exit(int status) etc.

  • Consider function with no return and no argument. If your function works as an independent block. It does not communicate with other functions.

    For example – void abort(void).