Scala | Call by Value and Call by Name

Reading Time: 2 minutes

In this blog we discuss about different ways of passing arguments in function call in scala.

Call-by-Value

By default scala use call-by-value for passing arguments to a function.Here is a function adder that uses the call-by-value strategy.

def adder(x: Int) = x + x

The Call-by-value function evaluate the passed-in expression value before calling the function, so that same value accessed every time.Above all call by-value strategy has the advantage that it evaluates every function argument only once.

Call-by-Name

In Call-by-Name we just prepend the => symbol in the argument type as shown the given adder function.

def adder(x: => Int) = x + x

The Call-by-Name functions evaluate the passed-in expression’s value for every single use.

The value of expression is evaluate at the time of the function call in Call by value. In other words value is pass as the parameter to the corresponding function and can be use throughout the function.

And in Call-by-Name, the expression itself is pass as a parameter to the function and it is only evaluate inside the function, whenever that particular parameter is call.

Example

Lets define a simple function that shows the current value of the most precise available system timer.

  def showTime_CallByValue(time: Long): Unit = {
    println(s"System time using Call-by-value: $time")
    println(s"System time using Call-by-value: $time")
  }

 def showTime_CallByName(time: => Long): Unit = {
    println(s"System time using Call-by-name: $time")
    println(s"System time using Call-by-name: $time")
  }

We have define two function showTime_CallByValue and showTime_CallByName that uses the call-by-value and call-by-name strategy respectively.

These functions take the System time in Long type and print it two times.So now we call both function by passing the argument System.nanotime that gives current system time in nano-seconds.

  showTime_CallByValue(System.nanoTime())
  showTime_CallByName(System.nanoTime())

Output:-

The output shows that when we pass the argument in call by value function it prints the same value two times. It shows argument is evaluated to a value and is used in the function, so expression System.nanoTime executes one time.

But on the other hand the output of Call-by-Name function is different on both println statement.

Whenever value is use in the body it evaluates the expression System.nanoTime again which result into different system time because in call-by-name the whole argument is pass to called function and it is execute in it.

Call-by-Name is passed value as it is and evaluated every time it is used in the function.

Conclusion

In this blog, we discuss about the basic differences of call-by-value and call-by-name using the basic example.

In addition these two strategies have some advantage over the other as the requirement we can use it.

References

GeeksforGeeks:- https://www.geeksforgeeks.org/scala-functions-call-by-name/?ref=gcse

Written by 

Akash Kumar is a Software Consultant at Knoldus Software LLP. He has done B.tech from Abdul Kalam Technical University. He is majorly focused on Scala and Angular. On the personnel side, he loves to play Cricket and online video games.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading