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:

  • In Call-by-name, the function parameters are evaluated only whenever they are needed but not when the function is CALLED.
  • In Call-by-value, the function parameters are evaluated when the function is called.
  • In Call-by-value, the parameters are evaluated before EXECUTING function and they are evaluated only once IRRESPECTIVE of how many times we used them in that function.
  • In Call-by-name, the parameters are evaluated whenever we access them and they are evaluated each time we use them in that function.
  • Scala SYNTAX Differences

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.



Discussion

No Comment Found