The programs written in C language are highly dependent on functions. Every C program is a combination of one or more functions.
Every C program starts its execution from the main function. The main function calls the other functions to share the work.

Function:
A function in C programming is a self-contained block of statements that are written to perform a certain task according to the user’s requirement.
Table of Contents
Types of functions in C

Library functions
- Library functions are predefined functions. Library functions are limited.
- We cannot understand the internal workings of the function.
- We cannot modify the function but still, we can use the function.
- Ex: printf, scanf, abs(), pow(), strlen()
User-defined function
- The functions which the user defines according to his/her requirements are called user-defined functions.
- The user can modify the function at any time and has full scope to implement his/her own ideas in the function.
- The user can understand the internal workings of the function.
- Ex: add, sub
Different Parts of a Functions in C
The different parts of a function are
Function Declaration
- Before using the function we have to declare the function. The function is declared before the main function.
- The function declaration helps the user to know the name of the function, what type of value that function is going to write, and the parameters which are used in the function.
Syntax of Function Declaration
return datatype function name(datatype variable 1, datatype variable 2, ….., datatype variable n)
int add(int a, int b);
void add(int a, int b);
void add();
void add(void);
Function Call
In order to execute the function we have to call the function. The function is called from the main function. There are four different ways of calling the function.
- With parameters with return value
Variable: function name (parameter list);
C= add(a,b);
- With parameters without return value
function name(parameter list);
add(a,b);
- Without parameters with return value
Variable=function name();
C=add();
- Without parameters without return value
add();
Function Body
The function body contains a block of statements which are written for a specific purpose to complete the task. The function body is written as another main function.
Syntax of Function Bady
datatype function name (datatype variable1, datatype variable2,…..datatype variable n);
{
Local variable declaration;
Statement 1;
Statement 2;
….
Statement n;
[return variable constant in expression];
}
Advantages of Function in C:
- The length of the source code can be reduced into smaller modules called functions.
- By using functions, it is very easy to locate and identify the errors.
- The user-defined functions can be used in other programs whenever it is necessary.
- Function avoids coding of repeated programming of similar instructions.
- It is very easy to understand the logic of the code.
- Function facilitates a top-down programming approach.
Disadvantages of Function in C:
- Function call overhead: Calling a function introduces overhead due to saving the current state, passing arguments, and returning control, which can impact performance.
- Recursion challenges: Recursive functions can consume significant memory if not managed carefully, potentially leading to stack overflow errors.
- Code complexity: Functions, especially when nested or interacting complexly, can sometimes make code harder to understand and maintain.
- Debugging difficulty: Debugging can be more challenging when errors occur within functions, requiring tracing through multiple calls.
- Limited scope visibility: Variables declared within a function are not directly accessible outside it, potentially affecting code readability and maintainability.
Function program in c language
1. C program “With parameters with return value”
#include <stdio.h> // Function declaration (prototype) int calculateArea(int length, int width); // Calculates the area of a rectangle int main() { int length, width, area; printf("Enter the length of the rectangle: "); scanf("%d", &length); // Read length from user printf("Enter the width of the rectangle: "); scanf("%d", &width); // Read width from user // Function call area = calculateArea(length, width); // Pass arguments (length and width) to the function // Output the result printf("The area of the rectangle is: %d\n", area); return 0; // Indicate successful program completion } // Function definition int calculateArea(int length, int width) { // Formal parameters: length and width int area; // Calculation area = length * width; return area; // Return the calculated area back to the calling function (main) }
Output:
Enter the length of the rectangle: 25
Enter the width of the rectangle: 20
The area of the rectangle is: 500
2. C program “With parameters without return value”
#include <stdio.h> // Function declaration (prototype) void calculateAndPrintArea(int length, int width); // Calculates and prints the area directly int main() { int length, width; // Input: Get dimensions from user printf("Enter the length of the rectangle: "); scanf("%d", &length); printf("Enter the width of the rectangle: "); scanf("%d", &width); // Function call calculateAndPrintArea(length, width); // Pass dimensions as arguments return 0; // Indicate successful program completion } // Function definition (calculates and prints directly) void calculateAndPrintArea(int length, int width) { // Receives length and width int area; // Calculation area = length * width; // Output: Print the result directly printf("The area of the rectangle is: %d\n", area); // No return statement }
Output:
Enter the length of the rectangle: 25
Enter the width of the rectangle: 20
The area of the rectangle is: 500
3. C program “Without parameters with return value”
#include <stdio.h> // Function declaration (prototype) int calculateArea(); // Calculates the area and returns it (no parameters) int main() { int area; // Function call (no arguments passed) area = calculateArea(); // Get area from the function // Output: Print the result obtained from the function printf("The area of the rectangle is: %d\n", area); return 0; // Indicate successful program completion } // Function definition (calculates and returns the area) int calculateArea() { // No parameters received int length, width, area; // Input: Get dimensions from user directly within the function printf("Enter the length of the rectangle: "); scanf("%d", &length); printf("Enter the width of the rectangle: "); scanf("%d", &width); // Calculation area = length * width; return area; // Send the calculated area back to the main function }
Output:
Enter the length of the rectangle: 25
Enter the width of the rectangle: 20
The area of the rectangle is: 500
4. C program “Without parameters without return value”
#include <stdio.h> // Function declaration (prototype) void calculateAndPrintArea(); // Calculates and prints the area directly (no parameters, no return) int main() { // Function call (no arguments passed) calculateAndPrintArea(); // Performs the calculation and printing internally return 0; // Indicate successful program completion } // Function definition (handles input, calculation, and output) void calculateAndPrintArea() { int length, width, area; // Input: Get dimensions from user printf("Enter the length of the rectangle: "); scanf("%d", &length); printf("Enter the width of the rectangle: "); scanf("%d", &width); // Calculation area = length * width; // Output: Print the result directly printf("The area of the rectangle is: %d\n", area); // No return statement }
Output:
Enter the length of the rectangle: 25
Enter the width of the rectangle: 20
The area of the rectangle is: 500
FAQs of function in C programming
What is a function in C, and why do we use them?
A function in C is a self-contained block of code that performs a specific task.
They promote code reusability (write once, use many times), improve organization, and make programs easier to read and maintain by breaking down complex problems into smaller, manageable parts.What’s the difference between function declaration and function definition?
i) Declaration (Prototype): Tell the compiler about the function’s name, return type, and parameters (the information it needs to check if the function is called correctly).
ii) Definition: Provides the actual code implementation of the function, including the steps it performs and (optionally) the value it returns.How do I pass values to a function (arguments vs. parameters)?
Arguments: The actual values you provide when calling a function.
Example: calculateArea(10, 5); Here, 10 and 5 are arguments.
Parameters: Placeholders in the function definition that receive the argument values.
Example: int calculateArea(int length, int width) { … }
Here, length and width are parameters.Can a function return multiple values? If not, what are the alternatives?
No, a C function can directly return only one value. However, you have alternatives:
Return a struct: Create a struct to hold multiple values and return the struct.
Pass pointers as arguments: Modify variables passed as pointers within the function, effectively returning multiple values indirectly.
Use global variables: (Generally not recommended) Store results in global variables that can be accessed outside the function.What does the void keyword mean in a function declaration?
The void keyword has two main uses in function contexts:
Return Type: void indicates that the function does not return any value.
Parameters: void within the parentheses means the function takes no arguments (e.g., void myFunction(void);).