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

  • SEARCH
  • Home
  • 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 8599573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:25:34+00:00 2026-06-12T01:25:34+00:00

I know that values sent to a function are by default passed as value

  • 0

I know that values sent to a function are by default passed as value and the method receives a copy of the variable. I know that when a variable is passed by reference the method can change the value of the variables from it was called. That being said, can someone help explain what’s going on in these simple illustrations? thanks in advance. Are expressions passed by reference, I guess?

using System;
class Program
{
    static void Main(string[] args)
    {
        int x = 2;
        int y = 20;
        Console.WriteLine(Add(x, y));
    }

    static int Add(int x, int y)
    {
        int ans = x + y;
         x = 20;
         y = 40;
       // return x+y;
         return ans;
        //returns 22
    }
}

and then

using System;
class Program
{
    static void Main(string[] args)
    {
        int x = 2;
        int y = 20;
        Console.WriteLine(Add(x, y));
    }

    static int Add(int x, int y)
    {
        int ans = x + y;
         x = 20;
         y = 40;
       return x+y;
      //   return ans;
        //returns 60
    }
}
  • 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-12T01:25:35+00:00Added an answer on June 12, 2026 at 1:25 am

    Both of these illustrate pass by value, and in general, value type semantics.

    In your first example, you are saying:

    int ans = x + y;
    

    This evaluates x and y at that time and adds them together to store in ans, setting x and y to new values later does not affect the value of ans.

    Consider it this way:

    static int Add(int x, int y)
    {
        int ans = x + y;  // evaluates ans = x + y = 2 + 20 = 22
         x = 20;          // here ans = 22, x = 20, y = 20
         y = 40;          // here ans = 22, x = 20, y = 40
         return ans;      // returns ans which is still 22 since x & y are independent
    }
    

    In your second example, you are delaying the addition till after you set the new values, thus the new values of x and y are used in the computation of

    return x + y;
    

    So, in essence, you get:

    static int Add(int x, int y)
    {
        int ans = x + y;  // ans = 22, x = 2, y = 20
         x = 20;          // ans = 22, x = 20, y = 20
         y = 40;          // ans = 22, x = 20, y = 40
       return x+y;        // evaluates x + y = 60
    }
    

    I think what may be confusing you is that:

    ans = x + y;
    

    Is not a function, it’s an expression which is evaluated and returned, changing x or y after this statement executes does not affect ans again.

    The key point to remember, again, is that ans = x + y; evaluates x and y at the time that statement is executed. Further changes to x and y don’t come into play here.

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

Sidebar

Related Questions

I know that there can be multiple values for an email, but I'm not
Is it a good practice to initialize columns that we can know their values
I know that when passing an object by value to a function, the move
I know that component-model indicates whether a property has a default value or not,
I know that for an integer, you can use: int value; I tried: string
I know that using the following codes will make each column have unique values,
Should we be testing values that we already know the answer to? If a
I would like to know how to extract values in a dictionary that appear
Given int a; , I know that the following returns the largest value that
I know that when you want to have a value in the form like

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.