-
March 11th, 2013, 02:26 PM
#1
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
-
March 11th, 2013, 03:29 PM
#2
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 11th, 2013, 04:21 PM
#3
Re: Passing data through a void function
Originally Posted by heew
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.
-
March 12th, 2013, 11:23 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|