×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Call by name vs call by value in Scala

By IncludeHelp Last updated : October 20, 2024

Scala Method

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.

Example

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.