Share via

C++ Function Return With Reference

Sid Kraft 41 Reputation points
2026-05-16T18:06:33.61+00:00

Have a function that I call with the variable references below. Was told that when I call, can change the values back to the original call if the variables are "reference" &A, &B. However, the system does not allow one to do, I.E. as follows:

void Function(const &A, const &B)

&A = 2.;

&B = 3.;

return;

Function(C,D);

C=0.;

D=0.;

After the return, the variables C and D are still 0.

Supposedly one can assign the values to the variables in the function A and B by reference, however, the system says that that the variables must be changeable, will not let the assignments within the function to happen. Sid Kraft

..

..

..

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 18,145 Reputation points Microsoft External Staff Moderator
    2026-05-18T03:49:31.2966667+00:00

    Hi @Sid Kraft ,

    Thanks for reaching out.

    What you want is possible, but the parameters cannot be declared as const references if you intend to change them inside the function. In C++, const means the function is only allowed to read the values, not assign new ones through those references.

    One small detail worth calling out is that the parameters also need an actual type in the declaration. In practice, that would look like const double& A or double& A, not just const &A.

    If the goal is to have the function update the caller's variables, declare them as non-const references instead, for example:

    void Function(double& A, double& B)
    {
      A = 2.0;
      B = 3.0;
    }
    

    Inside the function, you assign to the reference directly with A = 2.0; and B = 3.0;. You would not use &A or &B there, because & is the address-of operator, not something needed to modify a referenced value.

    Then when you call:

    double C = 0.0;
    double D = 0.0;
    
    Function(C, D);
    

    the values of C and D will become 2.0 and 3.0.

    One other thing to keep in mind is that if you later assign:

    C = 0.0;
    D = 0.0;
    

    after the function call, that will overwrite the values the function just set. So the reference part is working correctly, but any assignment afterward will naturally replace those updated values.

    So in short, use references when you want the original variables updated, and use const references only when you want to prevent the function from modifying them.

    Hope this helps! If my explanation and the information I provided were helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

    Was this answer helpful?

    0 comments No comments

  2. Darran Rowe 2,611 Reputation points
    2026-05-17T19:15:19.07+00:00

    There is a bigger problem, there is no types given in your code so it is going to be an outright compiler error anyway. There must be a type either just before or just after the const for this to be valid C++.

    void fun1(int const &); // These are equivalent reference types.
    void fun2(const int &); // They both signify a reference to a constant int.
    

    If you want to post code then either use the "insert code block" button or switch to "View rich text" and use markdown to insert your code.

    But I'll make one or two assumptions here, first, you are trying to pass in references to float or double to a function. Secondly, you are trying to do simple assignments to them. This will also touch on some of the slop answer.

    Anyway, the function prototype is explicit here, the const means constant. This is used in C++ to indicate that it is read only. One thing that I find useful is to read the types from right to left. Making the assumption that your prototype is supposed to be:

    void Function(const float &A, const float &B);
    

    or

    void Function(const double &A, const double &B);
    

    then it is clear on what it should be. Reading the types right to left indicates that A is a reference to a constant float/double. B is also a reference to a constant float/double. The const can move a little there, and people do put the const on the right hand side of the type since it is a little easier to read.

    This type of reference is useful for passing around non editable objects:

    void inspect_string(const std::string &);
    

    The compiler would make a copy of the string otherwise.

    A modifiable reference to a variable would therefore not include const.

    void modify_value(int &a); // Notice, no const.
    

    The other thing that I noticed is that you were trying to use & when trying to assign to the reference. & is the address of operator. Suppose we have the following.

    void fun()
    {
        int a = 0;
        &a; // This is of type int *.
    }
    

    So what you are trying to do is take an address of a reference and then assign to that. That isn't going to work, the value that is being provided is of a different type to the pointer. You don't actually need any special syntax to assign to a modifiable reference.

    #include <iostream>
    
    void fun(int &a)
    {
    	a = 3; // Direct assignment to the reference. The compiler knows this is a refence.
    }
    
    extern"C"
    int main()
    {
    	int var = 2;
    
    	std::cout << var << "\n";
    
    	fun(var);
    
    	std::cout << var << "\n";
    
    	return 0;
    }
    

    The compiler understands the type and will update the underlying object through the reference with the plain assignment. This will output:

    2
    3
    

    which is expected for the results. There is nothing else you need to do with floating point types either, besides the types having to match.

    #include <iostream>
    
    void fun(float &a)
    {
    	a = 3.f;
    }
    
    extern"C"
    int main()
    {
    	float var = 2.f;
    
    	std::cout << var << "\n";
    
    	fun(var);
    
    	std::cout << var << "\n";
    
    	return 0;
    }
    

    This works just as well.

    Was this answer helpful?

    0 comments No comments

  3. Q&A Assist
    2026-05-16T18:06:53.2833333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    const is what prevents the assignments from working.

    In this declaration:

    void Function(const float &A, const float &B)
    

    A and B are references, but they are references to const. That means inside Function they are read‑only; any attempt to assign to them:

    A = 2.0f;  // error
    B = 3.0f;  // error
    

    violates const‑correctness, so the compiler rejects it. This is exactly what “the variables must be changeable” in the error message refers to.

    To modify the caller’s variables via references, the parameters must be non‑const references:

    void Function(float &A, float &B)  // non-const references
    {
        A = 2.0f;
        B = 3.0f;
    }
    
    int main()
    {
        float C = 0.0f;
        float D = 0.0f;
    
        Function(C, D);   // C and D are passed by reference
    
        // Now C == 2.0f and D == 3.0f
    }
    

    Key points from the language rules:

    • Function arguments are passed by value by default; to let the function change the caller’s variables, use reference parameters (e.g., float &A).
    • A reference behaves syntactically like the object it refers to, but if it is a reference to const, it cannot be assigned to.
    • Returning multiple values is usually done either by reference parameters, by returning a struct/class, or by returning a std::tuple/std::pair.

    For example, using a struct instead of out‑parameters:

    struct Values
    {
        float a;
        float b;
    };
    
    Values Function()
    {
        return {2.0f, 3.0f};
    }
    
    int main()
    {
        Values v = Function();
        // v.a == 2.0f, v.b == 3.0f
    }
    

    References:

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.