Passing data through a void function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Passing data through a void function

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Passing data through a void function

    Hey, first time posting here. I am new to C++ (day 2 since I started learning it) so bear with me.

    Ok so my question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?

    For instance:

    Code:
    // example.cpp -- poorly written, just trying to learn
    #include <iostream>
    
    void format()
    {
           using namespace std;
           cout << "Time: " << min << hrs << endl;
    };
    
    int main()
    {
          using namespace std;
          int hrs;
          cin hrs;
          cin min;
          format();   // call the void format to display them in a certain way
          return 0;
    }
    Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.

    Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?

    Please excuse my English if it is hard to understand - it's not my mother tongue.

    - Heew

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Passing data through a void function

    My main goal is to learn how to pass through values to a void function.
    This should help you understand how its done

    http://www.learncpp.com/cpp-tutorial...-at-functions/

    Code:
    cin >> hrs;
    cin >> min;
    Also for cin you need the >> operator

    If you didn't have 'using namespace std;' in your code then the program would look like (which compiles and runs!)

    Code:
    // example.cpp -- poorly written, just trying to learn
    #include <iostream>
    
    int hrs,
         min;
    
    void format()
    {
           std::cout << "Time: " << min << hrs << std::endl;
    };
    
    int main()
    {
          std::cin >> hrs;
          std::cin >> min;
          format();   // call the void format to display them in a certain way
          return 0;
    }
    As cout and cin are defined as part of what is called the std namespace you need to tell the compiler every time you use them where they are located using the scope operator :: To avoid having to do this everytime, you can tell the compiler to default to using a specific namespace (usually std) by 'using namespace std'. If this is used, it's normally placed after the #include statements.

    Code:
    // example.cpp -- poorly written, just trying to learn
    #include <iostream>
    using namespace std;
    
    int hrs,minut;
    void format()
    {
    	cout << "Time: " << minut << hrs << endl;
    };
    
    int main()
    {
    	cin >>hrs;
    	cin >>minut;
          format();   // call the void format to display them in a certain way
          return 0;
    }
    Note that I've had to change your variable min to minut as min is already defined in the std namespace. Having 'using' in your program does not make it slower etc as its just for the benefit of the compiler. At this stage of your c++ education, don't get too hung up on 'using namespace'. Its use should become clearer much later when you learn about namespaces.

    Hope this helps. Good luck in learning c++ and welcome to the c++ community.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,053

    Re: Passing data through a void function

    Quote Originally Posted by heew View Post
    Hey, first time posting here. I am new to C++ (day 2 since I started learning it) so bear with me.

    Ok so my question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?

    For instance:

    Code:
    // example.cpp -- poorly written, just trying to learn
    #include <iostream>
    
    void format()
    {
           using namespace std;
           cout << "Time: " << min << hrs << endl;
    };
    
    int main()
    {
          using namespace std;
          int hrs;
          cin hrs;
          cin min;
          format();   // call the void format to display them in a certain way
          return 0;
    }
    Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.

    Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?

    Please excuse my English if it is hard to understand - it's not my mother tongue.

    - Heew
    Passing arguments as parameters is typically a better way to do it than setting up globals.

    Here's a tutorial that explains it pretty well.
    http://www.learncpp.com/cpp-tutorial...and-arguments/

    Also, void is the return type and has nothing to do with the values you pass as arguments.

  4. #4
    Join Date
    Mar 2013
    Posts
    2

    Re: Passing data through a void function

    Yeah both of you were helpful, I managed to fix it now.


    As for the missing operators it was a silly mistake which I noticed shortly after posting this (I found no edit button), anyhow thanks both of you. I was tired at the time of posting this the most crucial part of this post (which was to pass my question/visualize it) was successful. Again thanks!

    - Heew
    Last edited by heew; March 25th, 2013 at 05:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center