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

• 07-13-2012
chaucer345
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:

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.
• 07-13-2012
whiteflags
Quote:

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.
• 07-13-2012
chaucer345
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!
• 07-13-2012
grumpy
Given that function, why not define a three-dimensional array (say, room_number) and simply return room_number[north][east][up]?
• 07-13-2012
phantomotap
Or set aside a fixed set of bits for each direction and "OR" them together.

Soma
• 07-14-2012
manasij7479
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.
• 07-14-2012
chaucer345
Quote:

Originally Posted by manasij7479
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).