Vous êtes sur la page 1sur 24

Layering: Building

Functions out of
Functions
Functional Abstraction
Reduces Complexity

Based on Lawrence Snyders slides for UW-Seattle. This notice


serves as permission for their use for teaching in public or
private (not for profit) schools. 2014

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

Two reasons for function:


Abstraction + Re-use

Microsoft Research 2014 (With permission from Larry Snyder)

Lets review
Functions in Lightbot 2.0:
in Moonwalk Excercise

Functions
Procedure
s
Methods
Actions

Recall that functions have two parts:


Function
Definition
A statement of how
it works

Function Call
A request to have it
preformed

Microsoft Research 2014 (With permission from Larry Snyder)

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

action pink ( ) returns (myColor:Color)


myColor := Color(255, 200, 200);
end

Microsoft Research 2014 (With permission from Larry Snyder)

Functions: Result
Void?
Functions that do something, but do not return a value, have void
as their <return type>

Functions that return a value must


say its type
action MoveBallBy(x_dist: Number, y_dist: Number)returns
void
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)

Functions: Parameters
Parameters are the values used as input to the
function;
parameters are not required, but the parentheses are

The type of each parameter must be given


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)

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

Microsoft Research 2014 (With permission from Larry Snyder)

Writing Functions
Our function definitions are listed under code

Function
Call
Function
Definition

Microsoft Research 2014 (With permission from Larry Snyder)

Using Functions
Once defined, functions can be called repeatedly
its the point of writing them!
Function
Call

Function
Call

Function
Definition

Microsoft Research 2014 (With permission from Larry Snyder)

Review, Analogy

Microsoft Research 2014 (With permission from Larry Snyder)

Arguments Become Parameters


Notice that if the DEFINITION has n parameters,
the CALL needs n arguments
The parameters and arguments correspond

Microsoft Research 2014 (With permission from Larry Snyder)

Arguments Become Parameters


Notice that if the DEFINITION has n parameters,
the CALL needs n arguments
The parameters and arguments correspond

Inside of the function, the


parameter, e.g. xPos, is declared
and initialized to the
corresponding argument, e.g.
400. Then, the definition uses it,
e.g.
a-> setpos(400, 300)
Microsoft Research 2014 (With permission from Larry Snyder)

Parameters
Automatically declared (& initialized) on call
They remain in existence as long as the function remains unfinished

When the function ends:


The parameters vanish
Will be recreated on the next call

Choose meaningful parameter names


xPos correlates to x position of the bullseye center
Everytime you see xPos you know the meaning

Microsoft Research 2014 (With permission from Larry Snyder)

Functions Tutorial, Scoreboard

Microsoft Research 2014 (With permission from Larry Snyder)

Review

Microsoft Research 2014 (With permission from Larry Snyder)

Function: why?
Makes your code more readable
Show your thinking/Hide the details

Parameterized: Support re-use

Microsoft Research 2014 (With permission from Larry Snyder)

Functional Abstraction Powers Layers


Review What We Did
Bullseye as pong ball
Create

Reset

Move

Collide

Bunch of circles

The computation ONLY deals circles, but we dont


think of it that way to us, its a ball
Microsoft Research 2014 (With permission from Larry Snyder)

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

Microsoft Research 2014 (With permission from Larry Snyder)

Arguments Become Parameters


Notice that if the DEFINITION has n parameters,
the CALL needs n arguments
The parameters and arguments correspond

Inside of the function, the


parameter, e.g. xPos, is declared
and initialized to the
corresponding argument, e.g.
400. Then, the definition uses it,
e.g.
a-> setpos(400, 300)
Microsoft Research 2014 (With permission from Larry Snyder)

Parameters
Automatically declared (& initialized) on call
They remain in existence as long as the function remains unfinished

When the function ends:


The parameters vanish
Will be recreated on the next call

Choose meaningful parameter names


xPos correlates to x position of the bullseye center
Everytime you see xPos you know the meaning

Microsoft Research 2014 (With permission from Larry Snyder)

Pop Quiz!

Microsoft Research 2014 (With permission from Larry Snyder)

What Are Your Questions?


We say that a function definition has 3 parts:
name, parameters, body
Name is critical: it names the concept created by the function
Parameters are critical: they customize a function to many cases
Body is critical: it defines how the function works

Function uses (calls) have 2 parts: name,


arguments (args)
Name is critical: says what concept you will use
Arguments are critical: says how this case handled

Microsoft Research 2014 (With permission from Larry Snyder)

Functions Tutorial 2, Factorials

Microsoft Research 2014 (With permission from Larry Snyder)

Vous aimerez peut-être aussi