Skip to content

Instantly share code, notes, and snippets.

@klgraham
Created September 22, 2012 23:09
Show Gist options
  • Save klgraham/3768168 to your computer and use it in GitHub Desktop.
Save klgraham/3768168 to your computer and use it in GitHub Desktop.
Call-by-name vs call-by-value in Scala
/* call-by-value means the parameters are evaluated left to
** right to determine their value before the function itself
** is evaluated
*/
def first(a: Int, b: Int): Int = a
first(3 + 4, 5 + 6) // will be reduced to first(7, 5 + 6), then first(7, 11), and then 7
/* call-by-name means the paramter is passed into the function
** as is. Parameter evaluation takes place after
** substitution
*/
def first1(a: Int, b: => Int): Int = a
first1(3 + 4, 5 + 6) // will be reduced to (3 + 4) and then to 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment