What parameters and arguments are
A function that always does the same thing is of limited use. Parameters let a function receive input, so the same code can produce different results on each call. A parameter is a placeholder: a name, written in the parentheses of the definition, for a value that does not exist yet. An argument is the actual value you pass when you call the function. Inside the function, the parameter behaves like a normal variable holding that argument.
The two words describe the two sides of one hand-off. The definition has parameters; the call has arguments.
Defining a function with a parameter
In Scala every parameter needs a name and a type, separated by a colon. The type is required: the compiler does not guess parameter types.
def sayHello(audience: String): Unit = {
println("Hello " + audience)
}
sayHello("humans") // Hello humans
sayHello("cats") // Hello cats
Here audience is the parameter and "humans" is the argument of the first call. Unit is the return type of a function that does its work (printing, here) and hands back nothing useful.
Several parameters
Separate parameters with commas. Arguments are matched to parameters by position: the first argument goes to the first parameter, the second to the second.
def sayHello(name: String, age: Int): Unit = {
println(name + ", aged " + age)
}
sayHello("John", 30) // John, aged 30
sayHello("Mary", 26) // Mary, aged 26
Scala also lets you name the arguments in a call, which makes the order irrelevant: sayHello(age = 26, name = "Mary") prints Mary, aged 26 too.
Variables as arguments
An argument can be any expression, including a variable. The function receives the value the variable holds at the moment of the call.
val name = "Andy"
val age = 28
sayHello(name, age) // Andy, aged 28
sayHello("Mary", age) // Mary, aged 28
The variable name in the caller and the parameter name inside the function are two separate things that happen to share a name. Only the value travels between them.
Building text with string interpolation
Joining pieces with + works, but Scala has a tidier tool. A string literal with an s in front of the opening quote is an interpolated string: $name is replaced by the value of name, and ${expression} is replaced by the value of any expression.
def show(n: Int): Unit = println(s"n+1 = ${n + 1}, n = $n")
show(4) // n+1 = 5, n = 4
Without the s, nothing is replaced: println("Hello $name") prints the characters Hello $name.
The exercise, step by step
The lesson asks for a function welcomeUser and two calls to it.
- Define the function inside
Solution, abovemainFunction, with oneStringparameter callednameand the return typeUnit. - Print the greeting with an interpolated string, so the text changes with the parameter: for
"Andy"it must readWelcome, Andy!. - Call it with a literal from
mainFunction:welcomeUser("Andy"). - Create a variable in
mainFunction. Avalis the right choice because the value never changes. - Call it with the variable as the argument, to show that an argument doesn't have to be a literal.
class Solution {
def welcomeUser(name: String): Unit = {
println(s"Welcome, $name!")
}
def mainFunction(): Unit = {
welcomeUser("Andy")
val name = "John"
welcomeUser(name)
}
}
Running mainFunction prints:
Welcome, Andy!
Welcome, John!
Common mistakes
- Too few or too many arguments. Calling
sayHello("John")when two parameters are required fails to compile withmissing argument for parameter age of method sayHello. - Wrong order or types.
sayHello(30, "John")does not compile: the compiler reportsFound: Int, Required: Stringfor the first argument and the reverse for the second. - Assigning to a parameter. Parameters are read-only. Writing
n = n + 1inside a function whose parameter isnfails withReassignment to val n. Compute a new value instead. - Forgetting the
sprefix. The text then contains a literal$name. - A parameter that is never used. The code compiles, but every call behaves the same. A parameter only matters if the body reads it.
Key takeaways
- Parameters are declared in the definition, with a name and a type each; arguments are the values passed in the call.
- Arguments are matched to parameters by position, or by name when you write
name = value. - Any expression can be an argument, including a variable or a computed value.
- Use
s"... $name ..."to put a parameter inside a string.
