void functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • socondc22
    New Member
    • Sep 2007
    • 67

    void functions

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by socondc22
    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.
    Have you tried going through a tutorial for these first?

    Comment

    • socondc22
      New Member
      • Sep 2007
      • 67

      #3
      Originally posted by r035198x
      Have you tried going through a tutorial for these first?
      no i havent... do you know of any i can go through?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4


        A Google search of 'c++ void function tutorial' probably would have been a more efficient use of your time.

        Comment

        • socondc22
          New Member
          • Sep 2007
          • 67

          #5
          this just doesn't seem like it would be the easiest way to write the program that i have to write..

          Comment

          • socondc22
            New Member
            • Sep 2007
            • 67

            #6
            Ok... can anyone help me with how i should start this... i went through the tutorial and i didn't understand how i could do this program with a void function...

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              Really, and what would be a better way to write the program? Seriously.

              EDIT: That was a response to the second to last reply.

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                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

                • socondc22
                  New Member
                  • Sep 2007
                  • 67

                  #9
                  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

                  • socondc22
                    New Member
                    • Sep 2007
                    • 67

                    #10
                    my real question is that when i use void swap_numbers is that just going to swap the numbers... or is there something that i should use instead that is used for if im converting numbers and not swapping

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      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

                      Working...