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 8307975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:39:53+00:00 2026-06-08T18:39:53+00:00

When pass a variable to a function, why the function only gets a copy/duplicate

  • 0

When pass a variable to a function, why the function only gets a copy/duplicate of the variable?

int n=1;

void foo(int i)
{
    i++;
}

As we all know, the function foo() can’t change the value of n by using foo(n).

Of course we can pass the address of the variable to make some change to the parameter variable.
But don’t you think that is a little bit inconvenient?

Why c/c++ is designed to only give a duplicate to the function instead of directly give the “real” variable itself to the function?
What’t the pro/benefit of this paradigm?


Update:
I have read @paxdiablo’s answer. I think his “encapsulation, modularity and localisation of effect” explanation is good.
But in my way, it can also preserve the parameter argument’s value as well. It can also realize encapsulation. By this way:(assume the function can directly get the “real” variable instead of a duplicate by default )

void foo(int n)
{
    int temp=n;
    //Do something to temp...

}

And in my way, the complicated mechanism,such as “pass by reference” or pointer can be eliminated when you do want to change the value of parameters passed in. That’s the benifit.

After a time of thought. I realise the reason why c/c++ isn’t designed as I proposed is just because of the INCONVINIENCE of my way!
In my way, if a function has a long list of variables, it would to terrible. What I thougth is the more convenient way is infact inconvenient:
You must write like this:

void foo(int a,int b,double c,float d,char s...)
{
    int temp1=a;
    int temp2=b;
    double temp3=c;
    float temp4=d;
    char temp5=s;
    ...
    //Do something to temp{1,2,3,4,5....}

}

So the designers of c/c++ introduce complex mechanism to trade off with convenience.
Am I right?

  • 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-08T18:39:55+00:00Added an answer on June 8, 2026 at 6:39 pm

    The are basically two schools of thought on this matter.

    The first is pass-by-value where a copy of the value is created for the called function.

    The second is pass-by-reference where the parameter that appears in the called function is an “alias” of the original. That means changes you make to it are reflected in the original.

    C is generally a pass-by-value language. You can emulate pass-by-reference by passing the address of a variable and then using that to modify the original:

    void setTo42 (int *x) { *x = 42; }
    :
    int y;
    setTo42 (&y);
    // y is now 42
    

    but that’s more passing the pointer to a variable by value, than passing the variable itself by reference.

    C++ has true reference types, possibly because so many people have trouble with C pointers 🙂 They’re done as follows:

    void setTo42 (int &x) { x = 42; }
    
    :
    int y;
    setTo42 (y);
    // y is now 42
    

    Pass-by-value is usually preferable since it limits the effects that a function can have on the “outside world” – encapsulation, modularity and localisation of effect is usually a good thing.

    Being able to arbitrarily modify any parameters passed in would be nearly as bad as global variables in terms on modularity and code management.

    However, sometimes you need pass-by-reference since it might make sense to change one of the variables passed in.

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

Sidebar

Related Questions

I have a situation where a function can only take int (can't change this)
I want to know how I can pass a variable from one page to
I need to pass a variable to a javascript function,but I got a little
I want to pass a variable as text or somehow to make above function
I am trying to pass one variable to a jQuery function inline (i.e.: using
How do you create a function in jQuery, call it, and pass a variable
You have to pass the args variable to the anonymous function, but the anonymous
How do I pass back a local variable from a function to main ,
A quick question: When i pass a variable to a function, does the program
I have a uint64 variable ...but the function in which I want to pass

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.