DEV Community

Cover image for Difference between parameters and arguments in a function
Julian
Julian

Posted on

Difference between parameters and arguments in a function

Puede leer la versión en español de este post haciendo click aquí

When I first started learning how to program (in Java) using a book from my local library, they would always mention "parameters" and "arguments", but I never quite understood the difference.

Some of you as programmers may have wondered at some point, what is the difference between a parameter and an argument in a function?

Let's take a look at a the difference:

  • When declaring a function the data it takes (usually goes inside parentheses in most programming languages) is called parameters.

  • When invoking (calling) a function the data we pass to it is referred to as arguments.

We are going to use a code example in C to make things more clear:

1  #include <stdio.h>
2 
3  int addTwoNumbers(int x, int y) {
4     int sum = x + y;
5
6     return sum;
7  }
8
9  int main(void) {
10    int a = 5;
11    int b = 7;
12    
13    int c = addTwoNumbers(a, b)
14
15    printf("The sum of the two numbers is: %d\n");
16 
17    return 0;
18 }
Enter fullscreen mode Exit fullscreen mode
  • On line 3 of the code block we declared a function called addTwoNumbers, the parameters it takes are two int type variables named x and y:
3  addTwoNumbers(int x, int y)
Enter fullscreen mode Exit fullscreen mode
  • We invoke the addTwoNumbers function on line 13, and we pass the a and b variables as arguments:
13 addTwoNumbers(a, b)
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
babib profile image
𝐁𝐚𝐛𝐢 ✨

So in conclusion parameters are used in the function definition while arguments are used in the function invocation

Collapse
 
jfitech profile image
Julian

Yes!, that is correct.

Collapse
 
pauljlucas profile image
Paul J. Lucas

Definition and declaration (if declared separately).