Layering: Building Functions out of Functions

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CS 201 Functions Debzani Deb.
Wed/Fri Week 2 Functions! What are they? What do they look like in JavaScript? What are they good for? How do I use them? Some examples… Mini-Lab 1!!!
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
 Functions package up computation … when do we use them? All the time.  Write some simple code to achieve a goal … 12/20/2015© 2010 Larry Snyder, CSE1.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
Methods.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
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.
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.
Basic coding… with TouchDevelop!!
Information and Computer Sciences University of Hawaii, Manoa
Andrew(amwallis) Classes!
User-Written Functions
CSE / ENGR 142 Programming I
Unit C: Expressions Lesson 03: Mathematical Properties with Variables
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Functions in Processing CSE 120 Spring 2017
by Tony Gaddis and Godfrey Muganda
Week 8 - Friday CS 121.
Light Bot Solutions: an analysis
CompSci 230 Software Construction
Functions in Processing CSE 120 Winter 2018
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Functions Inputs Output
Chapter Topics Chapter 5 discusses the following main topics:
Static Methods 14-Nov-18.
Starting Out with Java: From Control Structures through Objects
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
Let’s Learn Understand the Problem:
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Lesson 3.2 Review: Identifying concepts
Testing and Repetition
Procedures Brent M. Dingle Texas A&M University
Chapter 9: Value-Returning Functions
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Topics Introduction to Functions Defining and Calling a Function
Two Paths Diverge in the Lectures
In C Programming Language
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Announcements: Questions: Names on the board? Why?
A Methodical Approach to Methods
Lawrence Snyder University of Washington
Methods/Functions.
Chapter (3) - Procedures
Announcements Survey feedback: summary Quiz: HW 7:
Corresponds with Chapter 5
Tutorial 11 Using and Writing Visual Basic for Applications Code
Today: to list: Syllabus change: Grade on-line
Parameters and Arguments
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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

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

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

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

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

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

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

Refresher

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

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

Review, Analogy

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

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)

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

Functions Tutorial, Scoreboard

Review

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

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

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

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)

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

Pop Quiz!

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

Functions Tutorial 2, Factorials