Presentation is loading. Please wait.

Presentation is loading. Please wait.

Layering: Building Functions out of Functions

Similar presentations


Presentation on theme: "Layering: Building Functions out of Functions"— Presentation transcript:

1 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

2 Today’s 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

3 Let’s review Functions in Lightbot 2.0: “ “ in Moonwalk Excercise …
Recall that functions have two parts: Functions Procedures Methods Actions Function Definition A statement of how it works Function Call A request to have it preformed

4 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 action pink ( ) returns (myColor:Color) myColor := Color(255, 200, 200); or

5 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);

6 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);

7 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);

8 Refresher

9 Writing Functions Our function definitions are listed under Δcode
Call Function Definition

10 Using Functions Once defined, functions can be called repeatedly … it’s the point of writing them! Function Call Function Definition

11 Review, Analogy

12 Arguments Become Parameters
Notice that if the DEFINITION has n parameters, the CALL needs n arguments The parameters and arguments correspond

13 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 Then, the definition uses it, e.g. ◳a-> setpos(400, 300)

14 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

15 Functions Tutorial, Scoreboard

16 Review

17 Function: why? Makes your code more readable
Show your thinking/Hide the details Parameterized: Support re-use

18 Functional Abstraction Powers Layers
Review 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

19 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

20 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 Then, the definition uses it, e.g. ◳a-> setpos(400, 300)

21 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

22 Pop Quiz!

23 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

24 Functions Tutorial 2, Factorials


Download ppt "Layering: Building Functions out of Functions"

Similar presentations


Ads by Google