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 }
- On line 3 of the code block we declared a function called
addTwoNumbers
, the parameters it takes are twoint
type variables namedx
andy
:
3 addTwoNumbers(int x, int y)
- We invoke the
addTwoNumbers
function on line 13, and we pass thea
andb
variables as arguments:
13 addTwoNumbers(a, b)
Top comments (3)
So in conclusion parameters are used in the function definition while arguments are used in the function invocation
Yes!, that is correct.
Definition and declaration (if declared separately).