Skip to main content

Function Parameters and Arguments in C

Function Parameters

  • In C, the variables that are passed or defined at the time of function declaration are known as parameters.

  • These are also known as formal parameters or formal arguments function parameter. These parameters serve as placeholders for the data being passed to the function, allowing the function to access and manipulate the values of the arguments.

  • They receive the values passed as arguments to a function when it is called.

  • Function parameters are declared within the parentheses following the function name in the function definition.

  • Each parameter must be declared with a specific data type to match the expected argument type.

For example:

int add(int x, int y) {
return x + y;
}

Explanation The function add has two parameters, x and y, both of which are integers. When the function is called, the values passed as arguments will be assigned to these parameters.

Function Arguments

  • values that are declared within a function or passed to a function when it is called are known as an argument.

  • During execution, the function can use these arguments to perform its task. This is why function arguments are also known as Actual arguments or Actual Parameters.

  • When a function is defined, its parameters are listed in parentheses after the function name.

  • When the add function as defined above is called, the values of the arguments are passed in the same order as they are listed in the function definition.

For example:

int result = add(3, 5);

In this case, the values 3 and 5 are passed as arguments to the add function. The function then uses these arguments to perform the addition and return the result.

Difference between Parameters and Arguments

Function ParametersFunction Arguments
Variables that receive the values passed as arguments to a function.Values that are passed to a function when it is called.
Represent the placeholders for the data being passed to a function.Represent the actual data being passed to a function.
Are used in the function definition.Are used in the calling code.
Must be declared with a specific data type to match the expected argument type.Can be any valid expression that evaluates to the correct data type.
Have the same name and type every time the function is defined.May have different values each time the function is called.

Example 1

Following example shows function arguments and parameters, along with comments explaining what is happening:

#include <stdio.h>

// Function definition with parameters
int add(int x, int y)
{
// x and y are the parameters
// They receive the values passed as arguments when the function is called
return x + y;
}

int main()
{
int a = 3; // a is a variable with value 3
int b = 5; // b is a variable with value 5

int result = add(a, b); // function call with arguments a and b
// In this call, the value of a (3) is passed as the first argument to the
// function The value of b (5) is passed as the second argument to the
// function The parameters x and y in the function definition receive these
// values

printf("The result of adding %d and %d is %d\n", a, b,
result); // prints "The result of adding 3 and 5 is 8"
return 0;
}
Output: