Hello all
I have this int type function that returns a number. It returns the value 2 for now but later it will return more variety of values. How do I use the value it returned? I'm not sure of the proper syntax.
Hello all
I have this int type function that returns a number. It returns the value 2 for now but later it will return more variety of values. How do I use the value it returned? I'm not sure of the proper syntax.
UPDATE! As of 10/6/2014
https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
Don't play this crappy update. Wait for the newest one which is far more impressive.
Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
http://blogs.sega.com/2015/08/28/the...down-heats-up/
You can assign the returned value to a variable, or even use the function call as an expression.
Functions in C and C++ - Cprogramming.com
Last edited by Matticus; 08-28-2014 at 10:25 AM.
This is the code from the tutorial link you showed me. The function Multi returns x * y. How would I use what it returned?
I would need an extra variables to equal what "int mult (int x, int y)"returns.
What the tutorial does.
Code:#include <iostream> using namespace std; int mult ( int x, int y ); int main() { int x; int y; cout<<"Please input two numbers to be multiplied: "; cin>> x >> y; cin.ignore(); cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n"; cin.get(); } int mult ( int x, int y ) { return x * y; }
What I need to do
Code:#include <iostream> using namespace std; int mult ( int x, int y ); int main() { int x; int y; int product; cout<<"Please input two numbers to be multiplied: "; cin>> x >> y; cin.ignore(); mult(x,y) = product; cout<<"The product of your two numbers is "<< product <<"\n"; cin.get(); } int mult ( int x, int y ) { return x * y; }
UPDATE! As of 10/6/2014
https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
Don't play this crappy update. Wait for the newest one which is far more impressive.
Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
http://blogs.sega.com/2015/08/28/the...down-heats-up/
In the tutorial code, the following line prints the value returned from the function (i.e. uses the function as an expression, but does not store the return value):
In your example, you're trying store the return value in the variable. However, you have the assignment backwards. (Also, 'x' and 'y' aren't initialized).Code:cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
That line of code should be:
The return value of "mult()" is assigned to 'product'.Code:product = mult(x,y);
It's the same syntax as, say, assigning an integer to a variable; i.e.
Code:int x; // ... x = 5;
It worked;
I tried in reverse at first which is why my program failed. I need sleep or my attention to details is getting sloppy either way thanks. I would have tried 8 new concepts, if you haven't noticed what I did wrong.
UPDATE! As of 10/6/2014
https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
Don't play this crappy update. Wait for the newest one which is far more impressive.
Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
http://blogs.sega.com/2015/08/28/the...down-heats-up/
You're welcome. The funny thing is, you've done this before. For instance, on your "tic tac toe" thread from April, I saw this line of code:
"rand()" is a function (from stdlib.h), and you're storing its return value (modified with % 211) into a variable.Code:int ai = rand()% 211;
It's good that you're eager to learn game programming, but make sure you don't gloss over the basics of the language.