Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:19:10+00:00 2026-06-14T14:19:10+00:00

As I understand it, in Scala, a function may be called either by-value or

  • 0

As I understand it, in Scala, a function may be called either

  • by-value or
  • by-name

For example, given the following declarations, do we know how the function will be called?

Declaration:

def  f (x:Int, y:Int) = x;

Call

f (1,2)
f (23+55,5)
f (12+3, 44*11)

What are the rules please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:19:11+00:00Added an answer on June 14, 2026 at 2:19 pm

    The example you have given only uses call-by-value, so I will give a new, simpler, example that shows the difference.

    First, let’s assume we have a function with a side-effect. This function prints something out and then returns an Int.

    def something() = {
      println("calling something")
      1 // return value
    }
    

    Now we are going to define two function that accept Int arguments that are exactly the same except that one takes the argument in a call-by-value style (x: Int) and the other in a call-by-name style (x: => Int).

    def callByValue(x: Int) = {
      println("x1=" + x)
      println("x2=" + x)
    }
    
    def callByName(x: => Int) = {
      println("x1=" + x)
      println("x2=" + x)
    }
    

    Now what happens when we call them with our side-effecting function?

    scala> callByValue(something())
    calling something
    x1=1
    x2=1
    
    scala> callByName(something())
    calling something
    x1=1
    calling something
    x2=1
    

    So you can see that in the call-by-value version, the side-effect of the passed-in function call (something()) only happened once. However, in the call-by-name version, the side-effect happened twice.

    This is because call-by-value functions compute the passed-in expression’s value before calling the function, thus the same value is accessed every time. Instead, call-by-name functions recompute the passed-in expression’s value every time it is accessed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand type members in Scala. I wrote a simple example
I don't understand why scala needs me to sometimes name args to anon fns:
As far as I understand, in Scala we can define a function with no
I realize the following function calls are all same, but I do not understand
As I understand it, Function types in Scala compile to instances of FunctionN .
I am trying to understand the Scala quicksort example from Wikipedia. How could the
I am struggling to understand the following function definition in Traverse trait in Scalaz:
I'll ask this with a Scala example, but it may well be that this
I have to say I don't understand Scala enumeration classes. I can copy-paste the
I'm trying to understand the behavior of Scala for-loop implicit box/unboxing of numerical types.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.