Jump to content
  • Advertisement
Sign in to follow this  
guzumba

C++ questions

This topic is 3808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Everyone! Ive got a couple of question that you guys might think is dumb but I have to ask anyways cause im stumped. 1. I am a little confused about the "void" type. In the book im reading it keeps repeating this sentence. "Void doesnt pass back a value." Im not sure why you would even use this type then. If it doesnt pass anything back why is it used? If its useful could you guys let me know how please. 2. I understand the "break" command, but im not sure how to use the "continue" command. Can you guys give me a easy example how you can use "continue" in code like this one for "break"!
int main() {
    int a=0, b=0;
    while(true) {
         cout << "Enter a number (0 to quit): ";
         cin >> a;
         if (a == 0)
            break;



Thank you everyone for your time! Andrew

Share this post


Link to post
Share on other sites
Advertisement
1) void as return type is useful when the function should not return anything.
2) A contrieved snippet that reads all lines from a file that are longer than five characters.
std::ifstream is("foo.txt");
std::string line;
std::vector<std::string> vec;

while(std::getline(is, line))
{
if(line.length() < 5)
{
continue;
}
vec.push_back(line);
}

Share this post


Link to post
Share on other sites
1: You pretty much answered your own question there. Void type means no value, so you use it when you don't want to return a value.

For instance, if you want your function to draw something to the screen:

void Draw(const GameObj& thing) {
thing.draw();
}

It wouldn't make sense to return anything (barring error codes).

2: Continue is kind of useless. In fact, I don't think I've ever used that keyword. Here's how you use it though:

for (int i = 0; i < size; i++) {
if (myArray == 0)
//do something
else
continue; //run the loop again. this would have the same effect if you left it out.
}


Pretty much any time I use continue, I can find a simpler way to set up my loop without it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drakkcon
1: You pretty much answered your own question there. Void type means no value, so you use it when you don't want to return a value.

For instance, if you want your function to draw something to the screen:

void Draw(const GameObj& thing) {
thing.draw();
}

It wouldn't make sense to return anything (barring error codes).

2: Continue is kind of useless. In fact, I don't think I've ever used that keyword. Here's how you use it though:

for (int i = 0; i < size; i++) {
if (myArray == 0)
//do something
else
continue; //run the loop again. this would have the same effect if you left it out.
}


Pretty much any time I use continue, I can find a simpler way to set up my loop without it.




Thanks you for the info. Maybe this is why he didnt really explain "continue" that well in the book because it is not that useful.

Quote:
For instance, if you want your function to draw something to the screen:

void Draw(const GameObj& thing) {
thing.draw();
}

It wouldn't make sense to return anything (barring error codes).


Im still a little confused about the "void" question. You say it doesnt return anything? So why dont we just leave this out of the code since its not gonna do anything or return anything?

Share this post


Link to post
Share on other sites
When you declare a function, you need to specify a return type so the compile knows you are defining a function. Consider the two following examples:

void function();
function2();

The first one defines a function, the second one calls a function. Even if you do not want a value returned, you need to put a type of variable out front so the compiler recognizes the line for what it is.

Sorta.

Share this post


Link to post
Share on other sites
Quote:
Original post by ender7771
When you declare a function, you need to specify a return type so the compile knows you are defining a function. Consider the two following examples:

void function();
function2();

The first one defines a function, the second one calls a function. Even if you do not want a value returned, you need to put a type of variable out front so the compiler recognizes the line for what it is.

Sorta.


So when I put this code in it doesnt work. It just gives me a bunch of warnings and wont compile.

#include<iostream>
using namespace std;

void number1(int n);

int main() {
int n;
cout << "Enter a number and press ENTER: ";
cin>>n;
cout << "If you multiple it by 2 it = " << number1(n) << endl;
system("pause");
return 0;
}

void number1(int n) {
int p;
p=n*2;
return;
}






but when I replace "void" with "int" it works just fine.


#include<iostream>
using namespace std;

int number1(int n);

int main() {
int n;
cout << "Enter a number and press ENTER: ";
cin>>n;
cout << "If you multiple it by 2 it = " << number1(n) << endl;
system("pause");
return 0;
}

int number1(int n) {
int p;
p=n*2;
return p;
}



When I replaced "void" with "int" in both spots it works. I guess I dont get in what situation would you ever use "void"? Can you give me an example with void in it and what it will return when compiled. But please keep it simple so I can understand it.

Thanks for your time everyone! This is really helpful.
Andrew

Share this post


Link to post
Share on other sites
right, it wont work because a void function does not return anything.
The cout object expects a valid type there and since the function returns nothing, it will give you an error.
Think of it this way:
When you execute a function, it can be treated as some variable you are going to get. The return type states what type of variable that is. Void means nothing is returned, so naturally you can't use it in your example because cout expects some data to be passed into it with <<

int foo(int n){
return n*2;
}

int main(){
int x = foo(10);
//makes x = 20;
return 0;
}

in your example its possible to do it with a void function a bit differently:

void foo(int& n){
n*=2;
}

int main(){
int x = 10;
foo(x);
cout << x << endl; //outputs 20
}

in this example the function directly modifies x and returns nothing.

Here's a little example that uses your code:

int main() {
int n;
cout << "Enter a number and press ENTER: ";
cin>>n;
printdouble(n);
system("pause");
return 0;
}

void printdouble(int n) {
cout << "If you multiple it by 2 it = " << n*2 << endl;
}

Share this post


Link to post
Share on other sites
Yeah if you have void for a return type you don't even need to include the return keyword in your function.

Share this post


Link to post
Share on other sites
Quote:
Original post by guzumba
Can you give me an example with void in it and what it will return when compiled. But please keep it simple so I can understand it.


Not all functions manipulate and return numbers. A function might do something like:

void WaitForKeyPress();

Where it doesnt return anything, or deal with variables, but have another function.

Share this post


Link to post
Share on other sites
Quote:
Original post by fishmd
right, it wont work because a void function does not return anything.
The cout object expects a valid type there and since the function returns nothing, it will give you an error.
Think of it this way:
When you execute a function, it can be treated as some variable you are going to get. The return type states what type of variable that is. Void means nothing is returned, so naturally you can't use it in your example because cout expects some data to be passed into it with <<

int foo(int n){
return n*2;
}

int main(){
int x = foo(10);
//makes x = 20;
return 0;
}

in your example its possible to do it with a void function a bit differently:

void foo(int& n){
n*=2;
}

int main(){
int x = 10;
foo(x);
cout << x << endl; //outputs 20
}

in this example the function directly modifies x and returns nothing.

Here's a little example that uses your code:

int main() {
int n;
cout << "Enter a number and press ENTER: ";
cin>>n;
printdouble(n);
system("pause");
return 0;
}

void printdouble(int n) {
cout << "If you multiple it by 2 it = " << n*2 << endl;
}


I understand that void does not return a value. But it still returns what was in the "printdouble" function. So why would you not just use "int printdouble(int n)" in this situation. It does the exact same thing. I dont see the point of "void" and how its useful. Can anyone please show me some code where it would be useful where "int" or "double" would not be.

Thanks Andrew

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!