Académique Documents
Professionnel Documents
Culture Documents
Functions out of
Functions
Functional Abstraction
Reduces Complexity
Todays lesson
Two threads of class merge again as we
introduce functions in TouchDevelop and use
them to build a pong game with bullseye ball!
Function Syntax:
FunctionName, Parameters, and Return
Defining a function: writing a function
Calling a function: using a function
Arguments to parameters
Lets review
Functions in Lightbot 2.0:
in Moonwalk Excercise
Functions
Procedure
s
Methods
Actions
Function Call
A request to have it
preformed
Functions In TouchDevelop
<name> ( <param list> ) ( <return list> ) {
<body>
}
as in
action MoveBallBy(x_dist: Number, y_dist: Number)
ball->setX (ball->x + x_dist)
ball->setY (ball->y + y_dist)
end
or
Functions: Result
Void?
Functions that do something, but do not return a value, have void
as their <return type>
Functions: Parameters
Parameters are the values used as input to the
function;
parameters are not required, but the parentheses are
Functions: Return
A function returns its value with the return
statement
The stuff following return is the result
The returned item must be defined before the function ends
action MoveBallBy(x_dist: Number, y_dist: Number)
ball->setX (ball->x + x_dist)
ball->setY (ball->y + y_dist)
end
action pink ( ) returns (myColor:Color)
myColor := Color(255, 200, 200);
end
Microsoft Research 2014 (With permission from Larry Snyder)
Refresher
Writing Functions
Our function definitions are listed under code
Function
Call
Function
Definition
Using Functions
Once defined, functions can be called repeatedly
its the point of writing them!
Function
Call
Function
Call
Function
Definition
Review, Analogy
Parameters
Automatically declared (& initialized) on call
They remain in existence as long as the function remains unfinished
Review
Function: why?
Makes your code more readable
Show your thinking/Hide the details
Reset
Move
Collide
Bunch of circles
More On Parameters
Return to the two slides on the topic of
parameters
Parameters: Customize each function call
to a specific situation they are the input to
the function
Parameters are the names of the input
values used inside of the procedure body
Arguments are the values from outside to
be used for each of the parameters
Parameters
Automatically declared (& initialized) on call
They remain in existence as long as the function remains unfinished
Pop Quiz!