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.
  • 452471514 2018-02-18
  • 5299351513 2018-02-18
  • 3036511512 2018-02-18
  • 4224301511 2018-02-18
  • 6975481510 2018-02-18
  • 8259761509 2018-02-18
  • 7007851508 2018-02-18
  • 7796301507 2018-02-18
  • 567811506 2018-02-18
  • 2107511505 2018-02-18
  • 8923811504 2018-02-17
  • 6039601503 2018-02-17
  • 8788931502 2018-02-17
  • 4539021501 2018-02-17
  • 661851500 2018-02-17
  • 9152461499 2018-02-17
  • 4757661498 2018-02-17
  • 981131497 2018-02-17
  • 4385321496 2018-02-17
  • 5046771495 2018-02-17