Home » Scala language

Call by name vs call by value in Scala

Scala Call by name vs call by value: In Scala, there are various methods of passing parameters to a method while calling. In this tutorial, we will learn the difference between call by name vs call by value in Scala in depth.
Submitted by Shivang Yadav, on August 19, 2019

Scala Call by name vs call by value

A Scala method is a block of code that has a name and which may accept variables from the code that calls the method.

Syntax:

    def method_name (parameter_list){
	    // code 
    } 

The variables that a method accepts are called parameters. The way of passing parameters to a method is called parameter passing.

Methods of parameter passing in Scala

In Scala, there are two ways of passing parameters while calling a method,

  1. Call by value
  2. Call by name

1) Call by Value

In Call by value method of parameter passing in Scala - a copy is passed to the method. This means that the parameters passed to the method have nothing to do with the actual values. These parameters are called formal parameters. The changes made in the formal parameters are not reflected in the actual parameters (the values passed while calling the method ).

Sample of a method declared to accept call by value parameter passing,

    def add(a:Int , b:Int){
        var sum = a + b ; 
        println("sum = " + sum);
    }

The call will pass values that will be copied to variables a and b, and the operations on a and b will be independent of the actual values passed.

Program to demonstrate an example of CALL BY VALUE

object MyClass {
    //function defintion
    def add(x: Int){
        var sum = x + 1
        println("sum = "+sum)
    };
    
    def main(args: Array[String]) {
        var a = 34
        //function call
        add(a)
    }
}

Output

sum = 35

2) Call by name

Call by name method of parameter passing in scala also passes the memory address of the variable instead of its value. So, the program will work on the same variable. Due to address based parameter passing the variable's address is passed to the method which will perform the function.

Sample of the method declared to accept call by name parameter passing,

    def add(a: => Int , b: => Int){
        var sum = a + b; 
        println("sum = " + sum);
    }

This call will copy the memory address of the passed variables and the actual variables are used for processing.

Program to demonstrate an example of CALL BY NAME

object MyClass {
    def add(x: => Int){
        var sum = x + 1
        println("sum = "+sum)
    };
    
    def main(args: Array[String]) {
        var a = 34
        add(a)
    }
}

Output

sum = 35

Why not swapping function?

The most common example demonstrating the use of call by value and call by name in programming is swapping function.

In Scala, the parameters of the functions are val i.e. immutable which means you can't reinitialize them in the function. leaves us to a scenario where the most easy way to learn the difference is not applicable. So, the classical program is created to just give you differences. Now, consider this code, which tries to reinitialize the parameters of Scala.

object MyClass {
    def add(a: => Int){
         a = x + 1
        println("sum = "+a)
    };
    
    def main(args: Array[String]) {
        var a = 34
        add(a)
    }
}

Output

error: reassignment to val
         a = x + 1
           ^
one error found





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.