Function in C
In C programming, a function is a self-contained block of code that performs a specific task or a set of tasks. Functions are used to break down a program into smaller, more manageable pieces, making the code easier to understand, debug, and maintain.
// Function definition
int add(int a, int b) {
return a + b;
}addis the name of the function.intbefore the function name specifies that the function will return an integer value.- Inside the parentheses, the function takes two integer parameters
aandb. - Finally, it returns the sum of a and b.
#include <stdio.h>
int main() {
int num1 = 5;
int num2 = 10;
int sum;
// Function call
sum = add(num1, num2);
printf("Sum: %d\n", sum);
return 0;
}- In the
main()function,add(num1, num2)is called, passingnum1andnum2as arguments. - The
addfunction definition specifies the logic to add the two numbers and return the result.
Get Md Robiul islam’s stories in your inbox
Join Medium for free to get updates from this writer.
Functions in C can be used for a wide range of purposes and can take different parameter types, return different data types, and perform various operations. They can also be void (not returning any value) or accept no parameters.
// Function definition
void add(int a, int b) {
printf("%d",(a+b));
}#include <stdio.h>
int main() {
int num1 = 5;
int num2 = 10;
int sum;
// Function call
add(num1, num2);
return 0;
}In here, we did the same but as we are not returning something that’s why our return type is void.
Let’s see without any parameters
// Function definition
void add() {
int num1 = 5;
int num2 = 10;
int sum;
sum = num1+num2;
printf("%d",sum);
}int main() {
// Function call
add();
return 0;
}We did the same thing but all the calculations have been done inside the add() function and we call that function in our main function.






