Scala | Functions Call-by-Name
Last Updated :
08 Apr, 2019
In
Scala when arguments pass through call-by-value function it compute the passed-in expression's or arguments value once before calling the function . But a
call-by-Name function in Scala calls the expression and recompute the passed-in expression's value every time it get accessed inside the function. Here example are shown with difference and syntax.
Call-by-value
This method uses in-mode semantics. Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment. This method is also called as call by value.
Syntax :
def callByValue(x: Int)
Scala
// Scala program of function call-by-value
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Defined function
def ArticleCounts(i: Int)
{
println("Tanya did article " +
"on day one is 1 - Total = " + i)
println("Tanya did article " +
"on day two is 1 - Total = " + i)
println("Tanya did article "+
"on day three is 1 - Total = " + i)
println("Tanya did article " +
"on day four is 1 - Total = " + i)
}
var Total = 0;
// function call
ArticleCounts
{
Total += 1 ; Total
}
}
}
Output:
Tanya did article on day one is 1 - Total = 1
Tanya did article on day two is 1 - Total = 1
Tanya did article on day three is 1 - Total = 1
Tanya did article on day four is 1 - Total = 1
Here, by using function
call-by-value mechanism in above program total count of article not increased.
Call-by-Name
A call-by-name mechanism passes a code block to the function call and the code block is complied, executed and calculated the value. message will be printed first than returns the value.
Syntax :
def callByName(x: => Int)
Example :
Scala
// Scala program of function call-by-name
// Creating object
object main
{
// Main method
def main(args: Array[String])
{
// Defined function call-by-name
def ArticleCounts(i: => Int)
{
println("Tanya did articles " +
" on day one is 1 - Total = " + i)
println("Tanya did articles "+
"on day two is 1 - Total = " + i)
println("Tanya did articles " +
"on day three is 1 - Total = " + i)
println("Tanya did articles " +
"on day four is 1 - Total = " + i)
}
var Total = 0;
// calling function
ArticleCounts
{
Total += 1 ; Total
}
}
}
Output:
Tanya did articles on day one is 1 - Total = 1
Tanya did articles on day two is 1 - Total = 2
Tanya did articles on day three is 1 - Total = 3
Tanya did articles on day four is 1 - Total = 4
Here, by using function
call-by-name mechanism in above program total count of article will increased.
Another program of function call-by-name.
Example :
Scala
// Scala program of function call-by-name
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
def something() =
{
println("calling something")
1 // return value
}
// Defined function
def callByName(x: => Int) =
{
println("x1=" + x)
println("x2=" + x)
}
// Calling function
callByName(something)
}
}
Output:
calling something
x1=1
calling something
x2=1
Similar Reads
Scala | Functions - Basics A function is a collection of statements that perform a certain task. One can divide up the code into separate functions, keeping in mind that each function must perform a specific task. Functions are used to put some common and repeated task into a single function, so instead of writing the same co
3 min read
Scala | Nested Functions A function definition inside an another function is known as Nested Function. It is not supported by C++, Java, etc. In other languages, we can call a function inside a function, but itâs not a nested function. In Scala, we can define functions inside a function and functions defined inside other fu
2 min read
Anonymous Functions in Scala In Scala, An anonymous function is also known as a function literal. A function which does not contain a name is known as an anonymous function. An anonymous function provides a lightweight function definition. It is useful when we want to create an inline function. Syntax: (z:Int, y:Int)=> z*yOr
2 min read
Pure Function In Scala In any programming language, there are two types of functions: 1. PURE FUNCTIONS 2. IMPURE FUNCTIONSThere is a basic difference between the two, that is pure function doesnât change the variable itâs passed and an impure function does. For example, there exists a function which increases the input b
3 min read
Partial Functions in Scala Introduction: When a function is not able to produce a return for every single variable input data given to it then that function is termed as Partial function. It can determine an output for a subset of some practicable inputs only. It can only be applied partially to the stated inputs. Some import
4 min read