Learn Scala - Functions

Question Click to View Answer

What does the following code print?

def howdy() = "hi y'all"
println(howdy())

hi y'all is printed. The howdy() function is defined and then invoked. The howdy() function does not take any parameters and the string hi y'all is returned by the function.

What does the following code print?

def view = "mountains"
println(view)

mountains is printed. The view function is defined without parenthesis () and also invoked without parenthesis. Scala doesn't require parenthesis.

What does the following code print?

def building = {
  "tall"
}
println(building)

tall is printed. Functions in Scala are named expressions that can take inputs. When functions are defined on a single line, brackets {} are not required. Multiline functions require brackets.

What does the following code print?

def happy(thing: String) = {
  s"I like $thing"
}
println(happy("cats"))

i like cats is printed. The happy() function takes one String parameter and returns a String. The happy() function is invoked with the "cats" argument.

What does the following code print?

def crazy(number: Int) = {
  number + 3
}
println(crazy("book"))

This throws a type mismatch error. The crazy() function must be invoked with a single integer argument. This example invokes the crazy() function with a string argument, so the Scala compiler throws an error.

What does the following code print?

def cool(number: Double): String = {
  s"${number.toString} is cool"
}
println(cool(3.14))

3.14 is cool is printed. The cool() function takes a Double as an input and returns a String and the output. In previous examples, we have not explicitly specified the return type of the function. In this example, we explicitly state that the cool() function will return a string.

What does the following code print?

def singSong(times: Int): Int = {
  s"I'm going to sing ${times.toString} times"
}
println(singSong(5))

This code throws an error because the singSong() function is set to return an integer, but actually returns a string.

What does the following code print?

def add(a: Int, b: Int): Int = {
  a + b
}
println(add(3, 6))

9 is printed. The add() function takes two integers as arguments and returns an integer.

What does the following code print?

def fullName(first: String, last: String = "balvin"): String = {
  s"$first $last"
}
println(fullName("j"))

j balvin is printed. The last parameter is given a default value of "balvin". The default value is only used when the second parameter is not used.

What does the following code print?

def mathFun(x: Int, y: Int = 99): Int = {
  x * y
}
println(mathFun(4, 5))

20 is printed. The mathFun() function takes two arguments and returns the product. The second argument is given a default value, but this default value is not used when two arguments are supplied. mathFun(4, 5) has two arguments so the default value is not used and the result of 20 is returned.

What does the following code print?

def airplane(company: String): String = {
  s"The plane belongs to $company"
}
println(airplane(company = "Delta"))

The plane belongs to Delta is printed. The airplane() function is invoked with a named parameter. The function could also have been invoked as airplane("Delta") to achieve the same result. Named parameters can make code more readable.

What does the following code print?

def theBest(category: String, thing: String): String = {
  s"The best type of $category is $thing"
}
println(theBest(thing = "Squirtle", category = "Pokemon"))

The best type of Pokemon is Squirtle is printed. theBest() function is defined with the category as the first argument and the thing as the second argument, but invoked with the arguments in the reverse order. When theBest() is invoked, the thing is the first argument and the category is the second argument.

Named arguments can be specified in any order, thus eliminating argument order dependencies.