Three Functions, Three Specific Purposes

Well, I'm back to Introduction to Programming I. cplusplus.com forums were perfect, unfortunately, I found you too late. Perhaps this time I can pass the class.

I am working on a three-phase programming project assignment for school. I'll quote the section of the instructions for phase one:

"Use at least three functions: one for input, one or more for calculating (conversions), and one for output."

I think I missed something important here. How do the values get out of the first function to be used by the others if it is specified for input only? For that matter, if function two's sole purpose is for calculating, the same would hold true for function two. I'm thinking the reason I can't figure this out is because it's more than likely right under my nose.

Thank You!

K
How many values do you have to pass between functions?
One ==> you can use the return value for the input function as argument for the others
More than one ==> you can pass parameter by reference
Thanks Bazzy,

The point of the first phase is to take in american measurements and convert to metric. So it could be as many values as two--feet and inches.

Is "passing parameter by reference" the same as "call by reference"? I didn't think of that. It's referred to in the chapter, so it's a viable option for this phase of development.

K
Here's an example of using reference variables to do what you're looking to do:

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
#include <iostream>

void input(int x&);
void output(int x);

int main(int argc, char* argv[])
{
    int x; // Does not have to be the same identifier as the variable in the function.
    // Arrays can only be passed by reference or as pointers.
    input(x);
    output(x);

    return 0;
}

void input(int x&)
{
    // some code to take input. reference variables passed into the function
    // will be modified. For example... the variable, x, passed from main, will
    // be modified when it is modified in the input() function.
}

void output(int x)
{
    std::cout << x << std::endl;
}
Last edited on
Thank you, packetpirate.

It appears I need to study the ampersand sign (&) use a little more closely. This is helpful.

K
Logging of for a few hours to absorb this option. However, I'd like to get a mock up and see what y'all think.

K
Well:
1
2
3
4
5
6
void input(int &x) // Note: &x and not x&
{
    // some code to take input. reference variables passed into the function
    // will be modified. For example... the variable, x, passed from main, will
    // be modified when it is modified in the input() function.
}
Notice that ( as I said above ) you don't need references to output a single value form a function:
1
2
3
4
5
6
int input ()
{
    int result;
    // read inside result
    return result;
}
+1 Bazzy
I did the three functions using call-by-reference (&). It actually worked--a.k.a.--Did what I wanted it to do. I'm not sure that I understand 100% what I did. However, between the textbook and the forum, I'm pleased with the results. Phase One is ready to turn in for a grade.

Thank you, C++ forum.

K
Topic archived. No new replies allowed.