Share via

C++ Argument Return

Sid Kraft 31 Reputation points
2026-05-14T00:19:34.9433333+00:00

Have a function in C++ with three input arguments. When I call the function with three arguments, I return the 3 argument values in the function but it does not return into the original arguments used in the function call sequence. Example:

float values(arg1, arg2, arg3)

arg1=2.;

arg2=3.;

arg3=4;

return(arg1,arg2,arg3);

float AA=0.;

float BB=0.;

float CC=0.;

values(AA,BB,CC);

//One would expect AA=2, BB=3, CC=4. However, comes back as AA=0., BB=0., CC=0. ??

Not sure why this is happening, 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

Answer recommended by moderator

  1. Q&A Assist
    2026-05-14T00:19:46.8966667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    In C++, a function can return only a single value (its declared return type), and arguments are passed by value by default. The code in the example does not modify the caller’s variables because:

    1. The parameters arg1, arg2, arg3 are copies of AA, BB, CC.
    2. return(arg1, arg2, arg3); uses the comma operator. In C and C++, the comma operator evaluates all expressions but the result of the whole expression is the value of the last operand. So this is equivalent to return arg3;.
    3. The function is declared as float values(arg1, arg2, arg3), so it returns a single float (the value of arg3), and it does not assign anything back into AA, BB, CC.
    4. The call values(AA, BB, CC); ignores the single returned float value, so AA, BB, CC remain unchanged at 0.

    To achieve the intended behavior (have the function write into AA, BB, CC), use one of these approaches:

    1. Pass by reference (C++)

    void values(float& arg1, float& arg2, float& arg3)
    {
        arg1 = 2.0f;
        arg2 = 3.0f;
        arg3 = 4.0f;
    }
    
    int main()
    {
        float AA = 0.0f;
        float BB = 0.0f;
        float CC = 0.0f;
    
        values(AA, BB, CC);
        // Now AA == 2, BB == 3, CC == 4
    }
    

    2. Return a struct or class (C++)

    struct Values {
        float a;
        float b;
        float c;
    };
    
    Values values()
    {
        return {2.0f, 3.0f, 4.0f};
    }
    
    int main()
    {
        Values v = values();
        float AA = v.a;
        float BB = v.b;
        float CC = v.c;
    }
    

    3. Use out parameters (C style, via pointers)

    void values(float* arg1, float* arg2, float* arg3)
    {
        *arg1 = 2.0f;
        *arg2 = 3.0f;
        *arg3 = 4.0f;
    }
    
    int main()
    {
        float AA = 0.0f;
        float BB = 0.0f;
        float CC = 0.0f;
    
        values(&AA, &BB, &CC);
    }
    

    The key points from the language rules are:

    • Function arguments are passed by value unless explicitly passed by reference or pointer.
    • A function returns exactly one value, of its declared return type.
    • return (a, b, c); returns only c because of the comma operator semantics.

    References:

    Was this answer helpful?

1 additional answer

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 17,905 Reputation points Microsoft External Staff Moderator
    2026-05-14T02:57:31.5466667+00:00

    Hi @Sid Kraft ,

    Thanks for reaching out.

    From the example, it looks like the function arguments are being passed by value, so the function is working with copies rather than the original variables. That would explain why changing arg1, arg2, and arg3 inside the function does not update AA, BB, and CC in the calling code.

    There is also one other detail here: return(arg1, arg2, arg3); does not return three values in C++. That expression uses the comma operator, so only the last value, arg3, is actually returned.

    If you want the function to update the original variables, the simplest fix is to pass them by reference instead:

    void values(float& arg1, float& arg2, float& arg3)
    {
      arg1 = 2.0f;
      arg2 = 3.0f;
      arg3 = 4.0f;
    }
    
    int main()
    {
      float AA = 0.0f;
      float BB = 0.0f;
      float CC = 0.0f;
    
      values(AA, BB, CC);
    }
    

    With that change, AA, BB, and CC will contain 2.0f, 3.0f, and 4.0f after the call.

    If you prefer not to modify arguments directly, another option is to return a single object such as a struct or std::tuple, but for the example you posted, passing by reference is the most direct solution.

    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

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.