Passing functions question

Hi everyone! Here is my code:
1
2
3
4
5
  		for (int i = 1; i <= spacesMoved; i++) {
		 // PUT PROPER DIRECTION FUNCTION HERE ON THIS LINE
			boundsCheck(y, x);                              // Check if turtle is within the bounds of the program, terminate program if not
			grid[y][x] = penDown ? 1 : 0;
		}


What I need to do, where you see the comment about putting the proper direction function, is to put a specific command on that line depending on the direction that the turtle in my program is facing. For example, if he is facing east and moves forward, that line must have on it the following: x += 1 . If he were facing a different direction, it would need to have a different directional command there. Can anybody help with how I can pass one of four functions to that line, depending upon which function needs to be passed? (each function contains movement commands based on the direction the turtle is facing)
Hello User55009,

The questions are:

Do you have a function that could be used there or do you need to make a function for this or do you even need a function?

If you already have a direction how do you want to deal with it?

Any way you go with this it will be more than just one line. It could be a series of if/else if statements. Or use a return value of a function with either if/else if statements or a switch.

Just seeing the for loop here I can not tell what you have to work with. Line 3 may work better in a function so that the function returns the correct direction of movement.

As your comment says PUT PROPER DIRECTION FUNCTION HERE ON THIS LINE. So ask yourself the questions what will this function do and what might it return. Followed by how would I use this?

I wish I could do more right now, but like any program need more input.

Hope that helps,

Andy
I have a function for each movement command, because I am required to use functions in this program. In the functions for the movement commands, the proper code for making the turtle move is there. Basically what I am asking is if I can somehow pass a function directly onto that line where I have my comment. Like if the direction the turtle is facing is east, then the program sends the move east function to that line, if it is north then the program sends the move north function onto that line, and so on. I will do what you have suggested to work through the problem further. Thank you for your help.
Hello User55009,

You either have a single move function and pass the direction to that function. Or you could use a switch to call the correct function based on the direction.

With out seeing the move function or more of the program it is only a guess right now.

On the other hand there is no way to write code for a line one the program has started. Hence the switch or if/else statements.

Hope that helps,

Andy
Registered users can post here. Sign in or register to post.
<rt id="pwjBlLZ"><small id="pwjBlLZ"></small></rt>
<acronym id="pwjBlLZ"></acronym><rt id="pwjBlLZ"></rt>
<tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr><tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr><acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym>
<acronym id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></acronym>
<option id="pwjBlLZ"></option>
<tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr>
<acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym><acronym id="pwjBlLZ"></acronym>
<acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym>
  • 6512882421 2018-04-19
  • 4659652420 2018-04-19
  • 2967832419 2018-04-19
  • 8339042418 2018-04-19
  • 8147112417 2018-04-19
  • 2774752416 2018-04-19
  • 4316132415 2018-04-19
  • 6265742414 2018-04-19
  • 1875142413 2018-04-19
  • 4146552412 2018-04-19
  • 8205662411 2018-04-19
  • 959982410 2018-04-19
  • 7153742409 2018-04-19
  • 9349932408 2018-04-18
  • 6024052407 2018-04-18
  • 2113432406 2018-04-18
  • 7629172405 2018-04-18
  • 163882404 2018-04-18
  • 3515922403 2018-04-18
  • 5047802402 2018-04-18