24
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

Bb 11 programming

Embed Size (px)

Citation preview

Page 1: Bb 11   programming

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

Page 2: Bb 11   programming

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

Page 3: Bb 11   programming

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

Page 4: Bb 11   programming

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

Page 5: Bb 11   programming

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

Page 6: Bb 11   programming

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

Page 7: Bb 11   programming

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

Page 8: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

Refresher

Page 9: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

Writing FunctionsOur function definitions are listed under Δcode

FunctionDefinition

FunctionCall

Page 10: Bb 11   programming

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

Page 11: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

Review, Analogy

Page 12: Bb 11   programming

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

Page 13: Bb 11   programming

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)

Page 14: Bb 11   programming

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

Page 15: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

Functions Tutorial, Scoreboard

Page 16: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

Review

Page 17: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

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

Parameterized: Support re-use

Page 18: Bb 11   programming

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

Page 19: Bb 11   programming

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

Page 20: Bb 11   programming

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)

Page 21: Bb 11   programming

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

Page 22: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

Pop Quiz!

Page 23: Bb 11   programming

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

Page 24: Bb 11   programming

Microsoft Research © 2014 (With permission from Larry Snyder)

Functions Tutorial 2, Factorials