Also, if my argument and parameter are the same value can I just use the same identifier for both? Thanks
-
3Could you provide an example of a language that makes a distinction between arguments and parameters? AFAIK most only use one term, or use the terms interchangeably, or use formal parameter to refer to the variable, but argument to the value or to the expression at the callsite.– amonMay 30, 2018 at 17:42
-
the question is nonsensical, parameters and arguments are just different names for different uses of the term; arguments when calling a method or function, parameters at definition.– esoterikMay 30, 2018 at 18:27
3 Answers
This distinction is important when you want to talk about the definition of a parameter (a formal parameter) versus the value that is passed into that definition when the function is called (an argument).
Can you use the same identifiers for both? Yes, so long as you understand the difference between pass by value and pass by reference. If you're passing by value, the identifier that you use for your argument does not refer to the same datum as the identifer used by your formal parameter, even though you used the same name. That's because the data in the original argument is copied and the copy is assigned to the variable declared in your formal parameter.
So you now have two different variables with the same name in two different scopes. Confusing, right?
Arguments and Parameters are exact opposites of each other, so using the same term for both not only wouldn't make sense, it would be actively harmful and confusing.
A parameter is a "hole" that you leave in the definition of a subroutine.
An argument is what you use to fill that hole in the usage of a subroutine.
Think about the following tail-recursive implementation of a counting loop:
def loop_times(n, func)
if n > 0
func(n)
loop_times(n-1, func)
end
end
loop_times(10, (n) → { print("Hello") })
# Hello
# Hello
# Hello
# Hello
# Hello
# Hello
# Hello
# Hello
# Hello
# Hello
The difference between the parameter n
and the argument n-1
is fundamental to understanding how this works. According to the Sapir-Whorf Hypothesis, the language we use shapes the way we think, can you imagine how hard it would be to think about two fundemantally different things that have the same name?
Parameters are the list of ingredients needed in a function. This tells what rules the arguments must follow in order to be valid, and what arguments are necessary for the function to execute.
Arguments are the ingredients you end up putting into the function, and must adhere to those rules.
These are not always the same because you could have a generic parameter,i.e. in C++ an object of type T called "firstParam" can be anything. The parameter will always be referred to by the identifier "firstParam" within its scope, and will always be recognized as a T, but the argument can be an integer, double, string etc.
-
1