DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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 Author

Yes!, that is correct.

Collapse
 
pauljlucas profile image
Paul J. Lucas

Definition and declaration (if declared separately).

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.