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.
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
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.
#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;
}
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.
}
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.