I want to know if this code is close to being correct?

Pages: 12
1
2
3
4
5
void Magician()
{
    std::cout << "I will now perform a math operation: "
              << 1 + 1 << std::endl;
}
ok i actually used your example in the code i created so i guess its just that my code isnt that long so there really is no need to include a void. Im guessing that a void type is saved in the library with whatever purpose the programmer has given it and is not used until needed. so for example if i use

1
2
3
4
5
6
7
8
9
void Magician()
{
    std::cout << "I will now perform a math operation: "
              << 1 + 1 << std::endl;
cout << "and now i will perform another math operation:"
<< 2*2 << endl;
cout << "and another:"
<< 2*2 << endl;
}


in a code that is 1000 lines long and i decide that i dont want to void that function at the end of my code i have to put in the main code

Magician();

and it will show it. but wouldnt i e able to do the same thing if this function was

int Magician() instead of void Magician()?





thinking about it if i were to use INT or DOUBLE or FLOAT Magician instead of VOID Magician, whatever is in INT or DOUBLE or FLOAT function would automatically show in my code. right?
No.
I think you need to take a step back Fyah. You are flailing in the process to make sense of something you do not fully understand, and are over-complicating things. Don't worry, we've all been there at some point.
Let's just forget everything we THINK we know about void and return types, ok? :)
Keep in mind, there is no one right way of doing things. Let's keep things simple for now.

What is return?

The return statement is used inside a function body and does the following things :


•Terminates the execution of a function.
•If an expression is present, it returns the expression to the caller.
•Transfers control back to calling function.


What is a return type?
A return type is a type (class, integral type, etc.) which you prepend to a function definition, to signify that you want this particular function to return an expression of said type.
Quite a bloated definition (sorry), take a look below.

Example of what I just said :
1
2
3
int returnInteger() {
	return 10;
}


In this case, returnInteger() returns 10.
It's a redundant function, but for the sake of demonstration this is how one might use this function :

1
2
3
4
int main() {
	int foo = returnInteger(); // foo is now 10.
	return 0;
}


Why does main have an integer return type? And why does it return 0?

I'll try to touch on this quickly.
When writing a function, it is up to the programmer to decide what the purpose of their function is. This means that functions must be used within a certain context where they and the expressions they return make sense to be used. For instance, I could write a function called int returnAge() which might return the age of a person, or I could write a function called int returnIndex() which would return an index to be used with a container. Both functions return an int, but each returned expression has a unique context in which an integer can mean different things.
Take everything I say with a grain of salt, I don't want to confuse you further. The actual answer to this question is that main's return expression can be used to return different error codes to the operating system when the program terminates. This is mostly useful for debugging code, but you should engrave this in your memory as a good habit.
In the example above, main returns 0 to signify that it has terminated successfully.


When would I want to use void as a return type?

void is used when you don't want to return an expression.
Example :

1
2
3
void sayHello() {
	std::cout << "Hello!" << std::endl;
}


Again, keep in mind that there are many ways to skin a cat. Sometimes you can do the same thing using different methods.
The following functions effectively do the exact same thing with a little change in syntax :

1
2
3
4
5
6
7
8
9
10
11
12
13
int input() {
	int i=0;
	std::cin >> i; //user enters number for i.
	return i; //i is returned.
}

int main() {
	int foo = input();
	std::cout << foo << std::endl;
	std::cin.sync();
	std::cin.get();
	return 0;
}


And the same function using void and references :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void input(int& i) {
	std::cin >> i; //user enters number for i.
	//there is no return statement because input() is void and...
	//i is a reference.
}

int main() {
	int foo=0;
	input(foo); //foo passed as reference and modified in input()
	std::cout << foo << std::endl;
	std::cin.sync();
	std::cin.get();
	return 0;
}


I hope this clarifies return types and how to use them. I will edit this as needed.
Last edited on
@ vlad from moscow

in your example you put

1
2
3
a=number1();
b=number2();
c=number3();


but i thought that you had to call the function using the integers like in the example of calculate

calculate (a, b, c)?

because what i did was

1
2
3
number1(a);
number2(b);
number3(c); 

obviously my way didnt work but what exactly
what exactly is the difference here? i just want to understand this 100%. i think this will be the last question i ask in this thread.

as for xismn thank you for all the help i will continue with my project and hopefully be able to do it without any more questions. beginners are annoying i know but i really want to understand this and sometimes ppl know what they r doing but dont know how to explain it that well thats why im so tangled in this web of confusion. but i know that when i get a job doing this the repetition of my work will allow me to understand this more and more.
Last edited on
Topic archived. No new replies allowed.
Pages: 12