Share via

C++ Function Return With Reference

Sid Kraft 31 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

1 answer

Sort by: Most helpful
  1. 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.