Sitemap
Md Robiul islam

Md Robiul islam

I graduated from Uttara University, Dept. of CSE, currently working as a lecturer of Uttara University.Find me: https://robiul-islam-rubel.github.io/website/

Function in C

2 min readDec 29, 2023

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.

Press enter or click to view image in full size
Source: Google
// Function definition
int add(int a, int b) {
return a + b;
}
  • add is the name of the function.
  • int before the function name specifies that the function will return an integer value.
  • Inside the parentheses, the function takes two integer parameters a and b.
  • 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;
}

    Get Md Robiul islam’s stories in your inbox

    Join Medium for free to get updates from this writer.

  • In the main() function, add(num1, num2) is called, passing num1 and num2 as arguments.
  • The add function definition specifies the logic to add the two numbers and return the result.

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.

--

--

Md Robiul islam
Md Robiul islam

Written by Md Robiul islam

I graduated from Uttara University, Dept. of CSE, currently working as a lecturer of Uttara University.Find me: https://robiul-islam-rubel.github.io/website/