I'm trying to figure out why my prof would want us to run a conversion of metric to english and english to metric through a void function and using call-by-reference. Also i was wondering if anyone had any points in how to start this, i really don't understand the void functions with call-by-reference.
void functions
Collapse
X
-
Originally posted by socondc22I'm trying to figure out why my prof would want us to run a conversion of metric to english and english to metric through a void function and using call-by-reference. Also i was wondering if anyone had any points in how to start this, i really don't understand the void functions with call-by-reference. -
A Google search of 'c++ void function tutorial' probably would have been a more efficient use of your time.Comment
-
Ok, the objective of your function is to convert from one measurement system to another. As in you have an input and you get an output that reflects the new measurement system. One way to do is to write a function that takes in a value and returns the appropriate result. With me here so far?
Great. But you have a void function here. So you can't return a result directly. Your other option then is to modify the input value directly. To do so, you have two syntactical options in C++.
The first is to use pointers. You will eventually learn how to do this, and this is the method in C anyway. But for now, you will learn the second method.
The second method involves passing by reference. If you read the material on passing by reference, you'll understand the idea is to modify the function arguments directly, because those modifications are persistent.
If you don't pass by reference, the default method is to make a copy of whatever you are passing in. Obviously, modifying a copy won't affect the real thing.
You say you don't understand, but as far as we can tell, the cplusplus.com tutorials are straightforward . So you'll have to point out to us what you precisely don't understand.Comment
-
This is my exact program that i have to write. Now if i get inputs using void get_numbers (int& input1, int& input2). then when i use void swap_values would that be where my conversion would take place?
1. ask the user if he wants to convert from metric (m), convert from english (e), or quit (q);
2. get either feet & inches or meters & centimeters from the user;
3. perform the appropriate conversion;
4. display the results;
5. loop back to step 1.
For each step above, you must write a function to perform it. Your function to get feet & inches/meters & centimeters must be a void function that uses call by reference. You may also choose to write additional functions to implement your solution.Comment
-
Heya, socondc22.
It might be that using a void function with a pass-by-reference is actually not the best way to do it.
He just wants you to learn how to do it while struggling with trying to understand *why* you would want to do it that way.
The end result being that you will have a new skill and the knowledge of when to use it.
So far, you're doing great :)Comment
Comment