What are the different types of functions in C? (2024)

A function is a block of statements that can perform a particular task.As we all know, there is always at least one function in C, and that is main().

Example

In the example below, the function’s name is sum and the data type is int. This task of this function is to produce the sum of two numbers:

int sum(int a,int b){ return(a+b);}

Below, the function is declared in main():

void main(){ int sum(int,int); //function declaration int x=5,y=6; total = sum(x,y);}

Formal parameters and actual parameters

When we call a function in main() or anywhere else in the program, and the function we created needs parameters, we would pass parameters to it while calling the function. In the example above, we passed variables x and y to obtain the sum of x and y.

According to the example above, the formal parameters are a and b, and the *actual *parameters are x and y.

Essentially, the variables being passed in the function call are actual parameters, and the variables being initialized and used in the function are formal* *parameters.

Note: Number of formal parameters = Number of actual parametersAlways!

Advantages of a function

  1. A function can be used any number of times after it is defined once.

  2. Functions make programs manageable and easy to understand.

Note: After termination of the function, the program control goes back to main() to execute any left out statements.

Function categories

There are 4 types of functions:

1. Functions with arguments and return values

This function has arguments and returns a value:

What are the different types of functions in C? (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6458

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.