What are the Different Types of Functions in C?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

A function is defined as a block of codes that are used to perform a specific task. These codes are defined a single time and can be used multiple times as per the need of the programmer. Types of functions in C language are mainly divided into two major types:

    1. Predefined Functions. (also known as library functions)
    1. User-defined functions. (defined by the user)

C Functions are Broadly Classified into 2 Types (Description & Example Program)

Types of functions in C language are classified into 2 types.

a) Predefined Functions

Predefined functions in C, a type of function integral to efficient coding, come predefined in the system library. Utilizing these functions, such as the printf() function from the <stdio.h> header file, is essential for writing error-free code. These functions showcase the different types of functions in C, illustrating the importance of including the correct header files to avoid execution errors. For example, to print a sentence, incorporating the printf() function demonstrates the practical application of predefined functions.

Output:

Let us see an example where more than one function is used in a program. Suppose we need to write a program where we will take a number as input and print its square root. For this we need three predefined functions:

  • printf(): present in <stdio.h>
  • scanf() : present in <stdio.h>
  • sqrt() : present in <math.h>

All these header files must be included at the beginning of program.

Program:

Output:

Note: "Predefined functions are also known as "Standard library functions".

b) User-defined Functions

In C programming, user-defined functions are a key concept, enabling the creation of custom functionality without requiring additional header files. This approach is essential for tasks such as calculating the area and perimeter of a rectangle, where separate functions streamline the process. By leveraging types of functions in C, developers can optimize their code, highlighting the significance of understanding different types of functions in C language. Such modularization not only simplifies coding but also enhances maintainability and readability, embodying the advantages of various types of function in C.

Let us see an example:

Output:

Formal Parameters and Actual Parameters

  • When we define a function, there are some variables inside the function that receive values when the functions are called. These are called as function parameters. While, the parameters that are passed to a function when the function is invoked, are called as Actual parameters.
  • The parameters which are called in the subprogram are known as formal parameters. While the parameters that are present inside the subprogram are known as Actual parameters.
  • The data type in the formal parameter must be defined. While the data type in the actual parameter does not need to be defined.

parameters

Let us see an example.

Output:

Explanation:

In the given example, values 4 and 2, known as actual parameters, are passed to the add function from the main function. Inside it, these values are copied into variables a and b, termed formal parameters, which are accessible only within this method. Post-execution, control returns to the main function.

Note: "The number of formal parameters is always equal to the number of actual parameters".

Advantages of a Function

Given below are the advantages of using functions in C-

  • Using functions, repetition of the same code can be avoided.
  • Function defined once can be used infinite times throughout the programming.
  • The readability of the program increases.
  • more possibility of error-free code.
  • since it is divided into many simpler functions for a specific task, it will be easy to change or update the functionality of any function.

Function Categories

Functions are defined using parameters, arguments, and return values. All these elements can be used in different ways in a program to define the functionality of a function as per the need of the user. When we talk about types of functions in C, user-defined functions can be broadly classified in four different ways:

a) Functions With Arguments and Return Values

This type of function has arguments and always returns a value. In this method, the arguments are passed to the function while calling it. The function will return some value when it is called from main() or any subfunction in the program. In it, data types are a must to define. If the return is of 'int' type, then the return value will also be 'int' type. This type of user-defined function is also called a fully dynamic function because the total control is in the hands of the end-user.

Lets see an example:

Output:

b) Functions With Arguments and Without Return Values

In this type of function, the arguments are also passed to the function while calling it. But it will not return any value when the function is called from the main function or any submethod. This function contains arguments but does not return any value. In this method, we allow the user to enter the values as input rather than fixed values. Since we are allowing the user to enter the values as input, we do not expect any return type value. This type of function can be used in real-life problems. Let us take an example where the user will enter two values as input and these values will be passed to the user-defined function, which will do addition.

Output:

Explanation:

Integer variables x and y are declared inside the main function. Then we called the "Add" with contains user-entered values. In the "Add(x,y)", there are integer variables of 'Sum' also integer (x,y) is present as arguments in this function.

c) Functions Without Arguments and With Return Values

In this user-defined function in C, without passing arguments, it returns a value when called from main() or a submethod. The return value's data type depends on its declaration. For instance, if declared as 'int', the return is also int. This showcases types of functions in C language. Let us see an example:

Output:

d) Functions Without Arguments and Without Return Values

In the C language, understanding different types of functions is crucial. A specific user-defined type, which neither accepts arguments nor returns values, exemplifies the diverse types of functions in C. Such functions are pivotal when output is displayed without data exchange, highlighting the varied types of function in C language

Let us see an example :

Output:

Learn More

You can visit this website to learn more about Functions in the C language

Conclusion

  • A function is defined as a piece of code that is used to perform a short and specific task, illustrating the types of functions in C.
  • Predefined functions are already present in a system library, serving as a prime example of different types of functions in C.
  • User-defined functions are defined by the user and highlight what are the types of functions in C language. It does not require any header file.
  • Formal parameters are those values that are determined by the function that accepts the value when a function is declared, an essential aspect of understanding the types of functions in C language.
  • Actual parameters are those variables that are moved to the function when the function is called inside the program, illustrating the practical application of types of function in C.
  • Using functions we can make a program simpler, easier to understand, and reduce error and repetition of the same code again and again, demonstrating the efficiency of utilizing various types of functions in C language.
  • The user-defined functions are further divided into four types on the basis of arguments and return value processing, an intricate part of the types of functions in C:
    1. Functions with arguments and return values
    2. Functions with arguments and without return values
    3. Functions without arguments and with return values
    4. Functions without arguments and without return values