Thread: Is it possible to use a return statement to assign integer values to a function?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Is it possible to use a return statement to assign integer values to a function?

    Okay, so after failing to figure out how to do it with an array, I tried my hand at constructing a function that defines a player's location in my text adventure using an extended if statement. What I wrote follows:


    Code:
    int location(int north, int east, int up)
    {
        //okay, so the idea here is that the movement in each direction needs to modify the return value of the function such that each of the 12 rooms has a different value.
       if (north==0 && east==0 && up==0)
       {
           return = 1; //this will be tunnels room 1
       }
       else if (north==1 && east==0 && up==0)
       {
           return = 2; //this will be tunnels room 2
       }
       else if (north==1 && east==1 && up==0)
       {
           return = 3; // this will be tunnels room 3
       }
       else if (north==0 && east==1 && up==0)
       {
           return = 4; // this will be tunnels room 4
       }
       else if (north==0 && east==0 && up==1)
       {
           return = 5; //this will be the clinic
       }
       else if (north==1 && east==0 && up==1)
       {
           return = 6; //this will be the meeting room/hard knock's training room
       }
       else if (north==1 && east==1 && up==1)
       {
           return = 7; //this will be Tiller's lab
       }
       else if (north==0 && east==1 && up==1)
       {
           return = 8; // this will be the bedrooms/Blueblood's office
       }
       else if (north==0 && east==0 && up==2)
       {
           return = 9; // this will be the mountain pass
       }
       else if (north==0 && east==0 && up==2)
       {
           return = 10; // this will be the bridge
       }
       else if (north==1 && east==1 && up==2)
       {
           return = 11; // this will be the Creek
       }
       else if (north==0 && east==1 && up==2)
       {
           return = 12; // this will be the hill side
       }
    
    }
    When I try to compile it, I just end up with a bunch of error messages saying that:

    C:\Documents and Settings\Administrator\Desktop\program\First Prince Text Adventure\main.cpp|19|error: expected primary-expression before '=' token|

    for each line with a return statement. This implies to me that I may be using the return statement very wrong. I thought that in an integer function it just defined the integer that the function returned and thus I could use it to define the value of a function under certain circumstances such as the room the player was in based on the value of the three movement integers noted as arguments in the function. Am I way off base with what I'm trying to do here? I would really appreciate some assistance.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    C:\Documents and Settings\Administrator\Desktop\program\First Prince Text Adventure\main.cpp|19|error: expected primary-expression before '=' token
    Yeah so you need a lvalue on the left of the equals sign for it to work. Lvalues are memory locations: Variables not unlike north, east, and up are examples.

    Return is not an lvalue so what you have written won't work, but if you can imagine using the function like this:
    Code:
    room = location(0, 1, 0);
    Then use the return statement to complete the right hand side of this statement.
    Last edited by whiteflags; 07-13-2012 at 11:04 PM. Reason: decided to use [code]

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Ah, I see. I modified the code such that:

    Code:
    int location(int north, int east, int up, int room)
    {
        //okay, so the idea here is that the movement in each direction  needs to modify the return value of the function such that each of the  12 rooms has a different value.
       if (north==0 && east==0 && up==0)
       {
           room = 1; //this will be tunnels room 1
       }
       else if (north==1 && east==0 && up==0)
       {
           room = 2; //this will be tunnels room 2
       }
       else if (north==1 && east==1 && up==0)
       {
           room = 3; // this will be tunnels room 3
       }
       else if (north==0 && east==1 && up==0)
       {
           room = 4; // this will be tunnels room 4
       }
       else if (north==0 && east==0 && up==1)
       {
           room = 5; //this will be the clinic
       }
       else if (north==1 && east==0 && up==1)
       {
           room = 6; //this will be the meeting room/hard knock's training room
       }
       else if (north==1 && east==1 && up==1)
       {
           room = 7; //this will be Tiller's lab
       }
       else if (north==0 && east==1 && up==1)
       {
           room = 8; // this will be the bedrooms/Blueblood's office
       }
       else if (north==0 && east==0 && up==2)
       {
           room = 9; // this will be the mountain pass
       }
       else if (north==0 && east==0 && up==2)
       {
           room = 10; // this will be the bridge
       }
       else if (north==1 && east==1 && up==2)
       {
           room = 11; // this will be the Creek
       }
       else if (north==0 && east==1 && up==2)
       {
           room = 12; // this will be the hill side
       }
        return room;
    }

    and now it works fine. Thank you!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given that function, why not define a three-dimensional array (say, room_number) and simply return room_number[north][east][up]?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Or set aside a fixed set of bits for each direction and "OR" them together.

    Soma

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I wonder why your function does not take into account the current position of the player.
    If it needs to do that too, you'll end up writing a similar function for each 'block' on the map.
    Make a grid and return coordinates depending on the arguments (position and movement direction) , instead.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Quote Originally Posted by manasij7479 View Post
    I wonder why your function does not take into account the current position of the player.
    If it needs to do that too, you'll end up writing a similar function for each 'block' on the map.
    Make a grid and return coordinates depending on the arguments (position and movement direction) , instead.
    Well, the plan as far as that goes was to define the starting position and write a function for each room and embed within it functions that allowed the player to increase or decrease each function by certain amounts depending on what room they're in (so for example, the clinic would allow the player to increase the north variable and the east variable, but not decrease them, or modify the up or down variable because there are no stairs in that room.

    As for why I didn't use an array, well, I started to code that, but it just came out as the equivalent of defining a bunch of variables so that I'd end up with a pile of player cins re-defining them, which is a lot harder to keep track of. Here's the code I started for that:

    Code:
    //int  room[2][2][3]; //new plan, there will be 12 rooms total, the first  dimension of the array is north and south, the second dimmension is east  and west, the third dimension will be up and down the planned  layout  is on the following lines:
        //Middle level:
        // [Meeting Room (north west [1][0][1]) abbreviation: 2] [Tiller's Lab (north east[1][1][1]) abbreviation: 3]
        // [clinic (south west [0][0][1]) abbreviation: 1]       [Office/Beds  (south east[0][1][1]) abbreviation: 4]
        // okay, maybe the abbreviations I have above are not as useful for  tracking where the player is, but it might be useful for tracking the  condition of each room (if something has happened before, rendering it  different due to player actions)
        // Plan: 1 will be the clinic, 2 will be the meeting room, and so  on. It will be important  to update this list to keep them clear.
        //room[0][0][1]=1;
        //room[1][0][1]=2;
        //room[1][1][1]=3;
        //room[0][1][1]=4;
    Now if it was possible to embed functions within those array locations and not just a list of numbers and write a function that was capable of moving the player between locations in the three dimensional array, I'd love to, but I've yet to see a really clear description of how to do that. (Note, I would probably be eternally grateful if you could provide such a thing).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a function return three values
    By alanb in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2009, 08:51 AM
  2. function - return statement
    By chungt004 in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 08:33 PM
  3. function return values
    By ashok449 in forum C Programming
    Replies: 10
    Last Post: 12-21-2007, 02:57 AM
  4. can a function return 2 values?
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 09-27-2005, 02:35 AM
  5. Can yoou return a range of values stored into a integer?
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 04:24 PM