OneBite.Dev - Coding blog in a bite size

call a function with parameter in C

Code snippet on how to call a function with parameter in C

  int addTwoNumbers(int x, int y) {
    int sum = x + y;
    return sum;
  }

  int main() {
    int a = 1;
    int b = 3;
    int c = addTwoNumbers(a, b);
    printf("The sum is %d\n", c);
  }

This code creates a function called addTwoNumbers which accepts two int parameters, x and y and returns their sum as an int. In the main function, two variables, a and b, are declared and assigned values 1 and 3 respectively. The addTwoNumbers function is then called with a and b as parameters. The function’s return value is stored in another variable, c, and finally printed to the console.

c