Thread: using the value returned from an int type function

  1. #1
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68

    using the value returned from an int type function

    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/

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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.

  3. #3
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    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/

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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):

    Code:
    cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
    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).

    That line of code should be:

    Code:
    product = mult(x,y);
    The return value of "mult()" is assigned to 'product'.

    It's the same syntax as, say, assigning an integer to a variable; i.e.

    Code:
    int x;
    
    // ...
    
    x = 5;

  5. #5
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    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/

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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:

    Code:
    int ai = rand()% 211;
    "rand()" is a function (from stdlib.h), and you're storing its return value (modified with % 211) into a variable.

    It's good that you're eager to learn game programming, but make sure you don't gloss over the basics of the language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-16-2008, 02:49 PM
  2. Negative value returned on unsigned int type
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-10-2005, 05:01 PM
  3. Returned value with operator overloaded function???
    By silk.odyssey in forum C++ Programming
    Replies: 8
    Last Post: 05-12-2004, 11:46 AM
  4. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM
  5. Replies: 3
    Last Post: 03-23-2002, 04:20 PM