Reputation: 682
I have just now started learning Higher Order Function in Scala, I cannot understand how the output is coming over here, where according to the Documentation https://docs.scala-lang.org/tour/higher-order-functions.html Higher order function is a function that either takes a function as argument or returns a function. So my question is,
b
=a
(b
is assigned to a
) in my program's multiplyBy2
function body, then why f(a)
is 50
(Check Line 1) and after that a is expected 25
(Check Line 2).f:Int=>Any
instead of only f:Int
in Line number 3I am confused about the workflow, I have tried to understand many times from my end. I will be really appreciated if anybody help me by describing the whole workflow of this program. Thanks.
package function
object HigherOrderFunction
{
def main(args: Array[String]):Unit =
{
functionExample(25, multiplyBy2)
}
def functionExample(a:Int, f:Int=>Any):Unit = //Line 3
{
println(f(a)) //Line 1
println(a) //Line 2
}
def multiplyBy2(b:Int):Int =
{
println(b) // Assigned a = b, So, a is 25 Now
b*2
// println(b) // Type Mismatch: Int, Required: Unit
}
}
Output
25
50
25
Upvotes: 0
Views: 280
Reputation: 86
Here You Will get some steps to follow. It will be easy to get out from your confusion.
Steps in the Comment Lines
package function
object HigherOrderFunction
{
def main(args: Array[String]):Unit =
{
functionExample(25, multiplyBy2) // Passing a function as parameter
}
def functionExample(a:Int, f:Int=>Int):Unit =
{
println(f(a)) // STEP-2: Here f=multiplyBy2, So multiplyBy2=25*2, which is 50 // Calling that function
println(a) // STEP-3: Here Simply first Argument is printed out 25
}
def multiplyBy2(b:Int):Int =
{
println(b) // STEP-1: Here b=a, So the value of b is 25
b*2
}
}
Upvotes: 2
Reputation: 1678
You can substitute a function with its implementation to reason about it:
Step 1:
object HigherOrderFunction
{
def main(args: Array[String]):Unit =
{
println(multiplyBy2(25)) //Line 1
println(25) //Line 2
}
def multiplyBy2(b:Int):Int =
{
println(b) // Assigned a = b, So, a is 25 Now
b*2
}
}
Step 2:
object HigherOrderFunction
{
def main(args: Array[String]):Unit =
{
functionExample(25, multiplyBy2)
println({println(25); 25*2})
// Evaluates the "value" passed in println, which causes 25 to be printed,
// then prints the result of the value, 50.
println(25)
}
}
I hope it now becomes clear why you see 25 50 25 printed out :)
Upvotes: 0
Reputation: 491
To understand the above code flow, lets move step by step with each method.
It is like you have defined functions, where functionExample is a higher order function as it accepts a function as an argument.
scala> def functionExample(a:Int, f:Int=>Any):Unit = //Line 3
| {
| println(f(a)) //Line 1
| println(a) //Line 2
| }
functionExample: (a: Int, f: Int => Any)Unit
scala> def multiplyBy2(b:Int):Int =
| {
| println(b) // Assigned a = b, So, a is 25 Now
| b*2
| // println(b) // Type Mismatch: Int, Required: Unit
| }
multiplyBy2: (b: Int)Int
After this we are trying to invoke :
scala> functionExample(25, multiplyBy2)
25
50
25
1) First call is made to functionExample making a=25 and binding the function with multiplyBy2 which takes an argument type of int and returns Any (as can be seen by the declaration f: Int => Any)
2) The first println is for f(a) which gets converted to multiplyBy2(25).
3) Inside multiplyBy2 function we are first printing the argument received which is 25.
So first output value is 25.
4) Next the multiplyBy2 function is returning b*2, which makes it 25*2 = 50 and this is the value returned by f(a) and printed due to Line1 in your sample code.
5) Lastly, in Line2 we are printing the argument we received in functionExample. So it prints 25.
Hence output turns out to be :
25
50
25
Upvotes: 4
Reputation: 385
The result is normal,
First your a=25, then you apply f(a) in println(f(a))
, so you print 25 and then you return a*2 = 50. then you println(f(a))
ie 50, and finaly you print a = 25 with has never changed because when you call multiplyBy2, It do not change the value of the parameter.
For the second question, It is just the signature of the fucntion , because f:Int
(f is a parameter value) but f:Int=>Any
(f it is a parameter function).
You can write your function as
def functionExample(a:Int)(f:Int=>Int):Unit = //Line 3
{enter code here`enter code here
println(f(a)) //Line 1
println(a) `enter code here`//Line 2
}
Upvotes: 1