I've successfully written code inside the while loop in main that makes the ball bounce correctly. However, first I tried to call a function from main (just to make it easier to read), called ballMovement() (which itself called move()). ballMovement() had the exact same code that I now have in main, but it didn't work.

I think I know why this is, but my question is how I can get around it and use a function rather than having the code in main.

I've figured that it doesn't work because my variables xvelocity and yvelocity that were passed into ballMovement are just copies of the variables, so when ballMovement returns (void) and then is called AGAIN (in the while loop in main), the variables xvelocity and yvelocity are unchanged. But how can I get around this? The variables need to be declared OUTSIDE the while loop, so I can't declare them inside my function. And if I have the while loop in the function then it will just execute forever and the rest of main won't execute.

I hope this makes sense. It's hard for me to explain the situation: 1. without fully understanding it myself! and 2. without showing all my code.

Just in case you need it, my declaration was:

void ballMovement(GWindow window, GOval ball, float xvelocity, float yvelocity)

I've now got to week 5 - my question is answered by using malloc and pointers, ie. &xvelocity

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .