In this lesson, we would be covering function call by-value parameters.
We would cover the following:
- Function Call by Name
- Functions With Variable Parameters
- Functions With Default Arguments
- Functions With Named Arguments
- Recursive Functions
1. Function Call by Name
Call-by-name allows us to pass the name of a function as parameter to another function call. This means that the function parameter is evaluated each time the calling function is executed.
In the example below, we have a function getTime that returns the current system time. Then we have another function currentTime that takes a time object as parameter.
// Program to demostrate Call by Name object CallByNameDemo { def getTime() = { println("Getting system time...") System.nanoTime() } def currentTime(t: =>Long) ={ println("Now in currentTime function...") println("Time parameter is " + t) } def main(args: Array[String]) { currentTime(getTime()) } }
Please try it out and see the output.
Now in currentTime function... Getting system time... Time parameter is 370723546094499
2. Function With Variable Parameters
In this kind of functions, you specify a parameter and then indicate that this parameter could be repeated number of time. For example, if a function takes 5 integer parameters, you can indicate this using Int*. An example is given below:
// Program to demostrate Repeated parameters object FunctionDemo { def printNums(nums: Int*) = { var i: Int = 0; for (num <- nums){ println("Num [" + i + "] = " + num ) i = i + 1 } } def main(args: Array[String]) { printNums(34, 65, 5, 37, 76) } }
3. Function Default Argument
In Scala, you can define a function and specify default arguments for the parameter. This means that when the function is called without any argument, it would evaluate using the default values specified in the function definition. An example is given below:
// Program to demonstrate Default argument object DefaultArgumentDemo { def getSum(num1: Int = 10, num2: Int = 20): Int = { var sum: Int = 0; sum = num1 + num2 return sum //return keyword is redundant but ok } def main(args: Array[String]) { println("The sum is " + getSum()) } }
So even though no argument is specified in the function call, the sum is calculated as 30 using default arguments.
4. Function with Named Arguments
Normally, in a function call, you pass arguments to the function in the order they appear in the function definition. However, you can also pass the arguments to the functions in different order using named arguments. The program below uses named arguments for the function call
// Program to demostrate Named argument object NamedArgumentDemo { def check(a: Int, b: Int, c: Int): Unit = { println("Argument 1 is " + a) println("Argument 2 is " + b) println("Argument 3 is " + c) } def main(args: Array[String]) { check(b = 40, c = 12, a = 9) } }
If you execute this program, you will see that the output is:
Argument 1 is 9 Argument 2 is 40 Argument 3 is 12
So even though the arguments were passed out of order, we still have them ordered using named arguments
5. Recursive Functions
Recursion is a very important concept in programming and Scala is not an exception. Let’s write a Scala program to perform factorial using recursion. As you know, a recursive function is a function that calls itself.
// Program to demostrate Recursive Function object RecursionDemo { def factorial(num: Int): Int = { var fact: Int = 1 if (num <= 1) { fact = 1 } else { fact = num * factorial(num - 1) } return fact } def main(args: Array[String]) { println("Factorial of 5 is " + factorial(5)) } }
I recommend you ensure to enter these programs and run them to see the output. Also feel free to watch the free video tutorials for a clearer step by step explanation.