Returning value from a void function

I want to return the add value from the void function to the main and output the answer in the main. How would I do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include<iostream>

using namespace std;

void returning(float*, float*);

int main()
{
    float num1 = 2;
    float num2 = 7;
    returning(&pnum1, &pnum2);

    return 0;
}

void returning (float *num1, float*num2)
{

    float add = *num1 + *num2;

}

Last edited on
The easy way to is return a float from the function:
1
2
3
4
5
float returning (float *num1, float*num2)
{
    float add = *num1 + *num2;
    return add;
}

If the function mus return void, then change one of the args:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void returning(float*, float*);

int main()
{
    float num1 = 2;
    float num2 = 7;
    returning(&num1, &num2);
    std::cout << num1;
    return 0;
}

void returning (float *num1, float*num2)
{
    *num1 = *num1 + *num2;
}
Last edited on
Or have additional function argument (references are neat too):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void returning(float&, const float&, const float&);

int main()
{
    float num1 = 2;
    float num2 = 7;
    float result;
    returning(result, num1, num2);
    std::cout << result;
    return 0;
}

void returning( float& foo, const float & bar, const float & gaz )
{
    foo = bar + gaz;
}
Topic archived. No new replies allowed.