0

Just one quick question, what should I return in the InitPaddle function? I made to put it on screen without returning anything, but in the step to step tips ain the pset`s page says that I have to return GRect... Just dont know how.

4

The paddle is nothing more than a GRect. We have already a GRect variable named paddle declared in main() and it's initialized with the value returned by initPaddle().

This means that initPaddle() must have a return type of a GRect (and it's already does) or you'll get a compilation error (because you're trying to initialize a GRect variable with a non-GRect value).

What you should do is to just declare and initialize a GRect variable within initPaddle() and return this variable using a return statement.

Assuming x, y, width and height are declared and initialized beforehand, you may write something like this in your code

// create a new GRect
GRect rect = newGRect(x, y, width, height); 
// do something to rect
return rect;

Unfortunately, the reference to the GRect that we just created within initPaddle() (i.e., rect) goes out of scope as the function ends execution. That is, you can't use anymore rect after initPaddle() returns.

And that's why we should return rect to get the value it refers to assigned to the variable named paddle (remember that paddle is initialized with the value returned by initPaddle()?) that's declared in main() so that we can use it after initPaddle() returns.

Also, remember that initPaddle() accepts an argument (i.e., a GWindow). So you should utilize this! ;)

5
  • But I can only return numbers or true or false if it is a boolean. So I cant return the paddle, maybe returning 0? Jun 23 '14 at 13:45
  • I just edited my answer! Each function must have a return type. Unless you your function's return type is void, you must return a value of this function's return type. For example, because main() returns an int, you must return an int and you can NOT return a string for example. Similarly, because initPaddle() returns a GRect, you must either return a GRect or NULL.
    – kzidane
    Jun 23 '14 at 13:47
  • Ok, thank you, I was returning the word GRect itself, not the name i gave to it: paddle, thats what was wrong, thank you for the help Jun 23 '14 at 14:49
  • I have a question on this 'return' issue too. When I tried to return newGRect instead of rect in this case and compiled my whole program, I got the message of Segmentation fault. May I know why? May someone tell me where to find a comprehensive note on 'return'?
    – Alan
    Jul 9 '15 at 16:05
  • hi, @Alan! if you have a new question, you may start a new thread and ask your question!
    – kzidane
    Jul 9 '15 at 17:16

You must log in to answer this question.

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