| 1. |
What Is Call-by-name? Does Scala And Java Support Call-by-name? What Is The Difference Between Call-by-value And Call-by-name Function Parameters? |
|
Answer» Call-by-name means evaluates method/function parameters only when we need them or we access them. If we don’t use them, then it does not EVALUATE them. Scala supports both call-by-value and call-by-name function parameters. However, Java supports only call-by-value, but not call-by-name. Difference between call-by-value and call-by-name: The major difference between these two are described below:
Call-by-value: 1 def myFunction(a: Int, b: Int) { } Here both a and b are Call-by-value parameters to myFunction. Call-by-name: 1 def myFunction(a: Int, b: => Int) { } Here both a is a Call-by-value parameter and b is Call-by-name to myFunction. Call-by-name means evaluates method/function parameters only when we need them or we access them. If we don’t use them, then it does not evaluate them. Scala supports both call-by-value and call-by-name function parameters. However, Java supports only call-by-value, but not call-by-name. Difference between call-by-value and call-by-name: The major difference between these two are described below: Call-by-value: 1 def myFunction(a: Int, b: Int) { } Here both a and b are Call-by-value parameters to myFunction. Call-by-name: 1 def myFunction(a: Int, b: => Int) { } Here both a is a Call-by-value parameter and b is Call-by-name to myFunction. |
|