Computer Science Canada

Function

Author:  Hunter007 [ Mon Jan 19, 2004 6:26 pm ]
Post subject:  Function

I need to have an understanding of the "function" command (creates function) I have to explain it tommorow and I'm just not getting it-Any help would be greatly appreciated.

Author:  Tony [ Mon Jan 19, 2004 6:38 pm ]
Post subject: 

function is a set of operations that return a value. The value is obtrained by ether calculating it from the parameters passed to it, or by fetching the value from else where (such as from a database).

basically function is like a procedure (techincally procedure is a void function) that returns a value. Just like in math really

Author:  Andy [ Mon Jan 19, 2004 6:42 pm ]
Post subject: 

think of a function as the black box

you shove something in
and something else comes out

you can shove multiple things in, but only onething comes out

Author:  Hunter007 [ Mon Jan 19, 2004 6:45 pm ]
Post subject: 

Ok thanks. One more question though. How does this part of my code relate to another if.

code:

function collided (x1, x2, y1, y2, radius : int) : boolean
    for xCheck : x1 - radius .. x1 + radius
        if xCheck >= x2 - radius and xCheck <= x2 + radius then
            for yCheck : y1 - radius .. y1 + radius
                if yCheck >= y2 - radius and yCheck <= y2 + radius then
                    result (true)
                end if
            end for
        end if
    end for
    result (false)
end collided


code:

if collided (x1, x2, y1, y2, 20) then
        tempx := dx1
        tempy := dy1
        dx1 := -dx1
        dy1 := -dy1
        dx1 := round (dx1 / 2 + dx2 / 2)
        dy1 := round (dy1 / 2 + dy2 / 2)
        dx2 := -dx2
        dy2 := -dy2
        dx2 := round (dx2 / 2 + tempx / 2)
        dy2 := round (dy2 / 2 + tempy / 2)
        count := count + 1
    end if


I don't understand how if result is true in this case, it goes down to this if. By the way I want it to do that, I just don't understand how it was done.

Author:  Andy [ Mon Jan 19, 2004 6:50 pm ]
Post subject: 

ur collided function returns true or false...
now if the ball has collided, it will become true so ur if statement becomes

if true=true then
tempx := dx1
tempy := dy1
dx1 := -dx1
dy1 := -dy1
dx1 := round (dx1 / 2 + dx2 / 2)
dy1 := round (dy1 / 2 + dy2 / 2)
dx2 := -dx2
dy2 := -dy2
dx2 := round (dx2 / 2 + tempx / 2)
dy2 := round (dy2 / 2 + tempy / 2)
count := count + 1
end if

now if the balls dont collide ur function will return false
and the if statement becomes
if false=true then
tempx := dx1
tempy := dy1
dx1 := -dx1
dy1 := -dy1
dx1 := round (dx1 / 2 + dx2 / 2)
dy1 := round (dy1 / 2 + dy2 / 2)
dx2 := -dx2
dy2 := -dy2
dx2 := round (dx2 / 2 + tempx / 2)
dy2 := round (dy2 / 2 + tempy / 2)
count := count + 1
end if

so the first time it will work and the second wont since false not=true

Author:  Hunter007 [ Mon Jan 19, 2004 7:21 pm ]
Post subject: 

OK in that last part of code if you just clarify something for me that would help a lot. Why does the "if" have "tempx" and "tempy"? Also what does dx1,dx1,dy1,dy2 do? I thought that was the speed of the ball, but it looks different when used here.

Author:  Andy [ Mon Jan 19, 2004 7:33 pm ]
Post subject: 

it is the speed of the ball, but since ur bouncing off the other ball, the speed changes

Author:  Hunter007 [ Mon Jan 19, 2004 7:36 pm ]
Post subject: 

Sorry, but is that why tempx and tempy are used? Because when the balls hit the speed changes?

Author:  Andy [ Mon Jan 19, 2004 7:38 pm ]
Post subject: 

since i have to change one variable at a time, tempx and tempy are used to store the values of the speed of ball one before the changes occured


: