Bb 11 programming

Preview:

Citation preview

Layering: Building Functions out of Functions

Functional Abstraction Reduces Complexity

Based on Lawrence Snyder’s slides for UW-Seattle. This notice serves as permission for their use for teaching in public or private (not for profit) schools. © 2014

Microsoft Research © 2014 (With permission from Larry Snyder)

Today’s lessonTwo 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 ReturnDefining a function: writing a functionCalling a function: using a functionArguments to parameters

Two reasons for function:Abstraction + Re-use

Microsoft Research © 2014 (With permission from Larry Snyder)

Let’s reviewFunctions in Lightbot 2.0: “ “ in Moonwalk Excercise …

Recall that functions have two parts:

Function Definition• A statement of how

it worksFunction Call• A request to have it

preformed

Functions• Procedure

s• Methods• Actions

Microsoft Research © 2014 (With permission from Larry Snyder)

Functions In TouchDevelop<name> ( <param list> ) ( <return list> ) {

<body> }

as inaction 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

or

Microsoft Research © 2014 (With permission from Larry Snyder)

Functions: ResultVoid?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: ParametersParameters are the values used as input to the function; parameters are not required, but the parentheses are

The type of each parameter must be givenaction 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 resultThe 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 FunctionsOur function definitions are listed under Δcode

FunctionDefinition

FunctionCall

Microsoft Research © 2014 (With permission from Larry Snyder)

Using FunctionsOnce defined, functions can be called repeatedly … it’s 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 ParametersNotice that if the DEFINITION has n parameters, the CALL needs n argumentsThe parameters and arguments correspond

Microsoft Research © 2014 (With permission from Larry Snyder)

Arguments Become ParametersNotice that if the DEFINITION has n parameters, the CALL needs n argumentsThe 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)

ParametersAutomatically declared (& initialized) on callThey remain in existence as long as the function remains unfinished

When the function ends: The parameters vanishWill be recreated on the next call

Choose meaningful parameter namesxPos correlates to x position of the bullseye centerEverytime 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 readableShow your thinking/Hide the details

Parameterized: Support re-use

Microsoft Research © 2014 (With permission from Larry Snyder)

Functional Abstraction Powers LayersReview What We Did

The computation ONLY deals circles, but we don’t think of it that way … to us, it’s a ball

Bunch of circles

Bullseye as pong ball

Create Reset Move Collide

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 ParametersNotice that if the DEFINITION has n parameters, the CALL needs n argumentsThe 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)

ParametersAutomatically declared (& initialized) on callThey remain in existence as long as the function remains unfinished

When the function ends: The parameters vanishWill be recreated on the next call

Choose meaningful parameter namesxPos correlates to x position of the bullseye centerEverytime 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, bodyName is critical: it names the “concept” created by the functionParameters are critical: they customize a function to many casesBody 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 useArguments are critical: says how this case handled

Microsoft Research © 2014 (With permission from Larry Snyder)

Functions Tutorial 2, Factorials

Recommended