Pass by value, reference woes

Maybe I am not correctly understanding how this is supposed to work.

First I declare the functions:

1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

void functionone (void);
void functiontwo (void);

int main()
{


For example, if I have a function using all local variables:

1
2
3
4
5
6
7
8
9
void functionone( void ){
int firstnumber;
int secondnumber;
int thirdnumber;
int sum = 0;
cout << "Enter three separate numbers: "; 
cin >> firstnumber >> secondnumber >> thirdnumber;
sum = firstnumber + secondnumber + thirdnumber;
}


Then I have another function down the line and I want to use the sum variable from function one

1
2
3
4
5
6
7
8
9
void functiontwo( void ){
int fourthnumber;
int fifthnumber;
int sixthnumber;
int totalsum = 0;
cout << "Enter the next three numbers: "; 
cin >> fourthnumber >> fifthnumber >> sixthnumber;
totalsum = sum + fourthnumber + fifthnumber + sixthnumber;
}


How do I get sum from functionone and use it in functiontwo? I am having a hard time understanding the coding behind this.

My goal is to modularize the payroll program we have been working on using no global variables and to use pass by value and pass by reference to calculate everything. My book and instructor are not very helpful...
For that aim you have, it is not necessary to pass the value (here the sum) to functiontwo by reference -unless you want to change it's reference value. Anyway, you can use this way:
replace line 8 of first code to
 
void functiontwo (int);

and call the functiontwo(sum)
was it useful?
Last edited on
There are a couple of ways: One way is to have function one return the contents of the local variable "sum".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

int functionone()
    {
    int firstnumber = 0;
    int secontnumber = 0;
    int thirdnumber = 0;
    int sum = 0;

    cout << "Enter three separate numbers: ";

    cin >> firstnumber >> secondnumber >> thirdnumber;

    sum = firstnumber + secondnumber + thirdnumber;

    return sum;

    }
        /*    functionone()    */




The other way is to pass in a pointer or a reference into function one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

void functionone( int *pSum )
    {
    int firstnumber = 0;
    int secontnumber = 0;
    int thirdnumber = 0;
    int sum = 0;

    if( NULL != pSum )
        {
        cout << "Enter three separate numbers: ";

        cin >> firstnumber >> secondnumber >> thirdnumber;

        sum = firstnumber + secondnumber + thirdnumber;

        *pSum = sum;

        }    /*    if( NULL != pSum )    */
    else
        {
        cerr << "Error: pSum is NULL!" << endl;
        }

    }
        /*    functionone()    */



I will leave it as an exercise for you to find out how to use a reference.
Arguments and return values!

1
2
3
4
5
6
7
int functionone(int initialSum)
{
    int firstnumber, secondnumber, thirdnumber;
    cout << "Enter three separate numbers: ";
    cin >> firstnumber >> secondnumber >> thirdnumber;
    return initialSum + firstNumber + secondNumber + thirdNumber;
}


Now you don't even need function2. You can use it like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int functionone(int initialSum = 0); // = 0 means that if no argument is given, it uses 0.

int main()
{
    int Sum = functionone(); // Sum will output the sum of the first three numbersl

    Sum = functionone(Sum); // Now we input three more numbers and add them to the running sum
}

int functionone(int initialSum)
{
    int firstnumber, secondnumber, thirdnumber;
    cout << "Enter three separate numbers: ";
    cin >> firstnumber >> secondnumber >> thirdnumber;
    return initialSum + firstNumber + secondNumber + thirdNumber;
}


If you want to pass by reference, you could do it this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void functionone(int& Sum)
{
    int firstnumber, secondnumber, thirdnumber;
    cout << "Enter three separate numbers: ";
    cin >> firstnumber >> secondnumber >> thirdnumber;
    Sum += firstNumber + secondNumber + thirdNumber;
}

int main()
{
    int sum = 0;
    functionone(sum); // This will add the initial three numbers to sum.
    functionone(sum); // This will add three more numbers to sum
    functionone(sum); // This will add three more numbers
}
Topic archived. No new replies allowed.