### Passing functions question

Hi everyone! Here is my code:
 ``12345`` `````` 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
Topic archived. No new replies allowed.