What is Function in C?
In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.
Advantage of functions in C
- By using functions, we can avoid rewriting same logic/code again and again in a program.
- We can call C functions any number of times in a program and from any place in a program.
- We can track a large C program easily when it is divided into multiple functions.
- Reusability is the main achievement of C functions.
- However, Function calling is always a overhead in a C program.
Function Aspects
There are three aspects of a C function.
- Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.
- Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.
- Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.
SN | C Function Aspects | Syntax |
---|---|---|
1 | Function declaration | return_type function_name (argument list); |
2 | Function call | function_name (argument_list) |
3 | Function definition | return_type function_name (argument list) {function body;} |
The syntax of creating function in c language is given below:
syntax
return_type function_name(data_type parameter...){
//code to be executed
}
Types of Functions
There are two types of functions in C programming:
- Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
- User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

Why Use Functions?
Functions in C provide several advantages, such as:
- Reusability: Once defined, functions can be used multiple times.
- Modularity: Functions break a program into smaller, manageable parts.
- Maintainability: Easier to debug and update individual functions.
- Organization: Functions make the program more structured and easier to read.
Types of Functions in C
In C, functions can be categorized into two types:
- Library Functions: These are pre-defined functions available in C libraries (e.g., printf(), scanf()).
- User-defined Functions: These are functions defined by the programmer to perform specific tasks.
Syntax of a Function
The general syntax of a function in C is as follows:
Example
return_type function_name(parameters) {
// Body of the function
// Code to perform the task
}
Example of a Simple Function
Here is an example of a simple function in C:
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main()
{
int result = add(5, 3);
printf("Sum: %d", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Output:
Syntax
type_name variable = (type_name) expression;
Example of Explicit Type Casting:
Example
#include <stdio.h>
int main()
{
float a = 5.75;
int b = (int) a; // Explicit type casting from float to int
printf("Value of b: %d\n", b);
return 0;
}