Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.

Slides:



Advertisements
Similar presentations
Lecture 4 Basic Scripting. Administrative  Files on the website will be posted in pdf for compatibility  Website is now mirrored at:
Advertisements

 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
CS 201 Functions Debzani Deb.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Structured programming 4 Day 34 LING Computational Linguistics Harry Howard Tulane University.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 CS161 Introduction to Computer Science Topic #9.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
CSC 108H: Introduction to Computer Programming
The Pseudocode Programming Process
Topic: Functions – Part 2
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
Chapter 8: Introduction to High-Level Language Programming
CISC101 Reminders Assn 3 due Friday, this week.
Functions Inputs Output
Call Stacks, Arguments and Returns
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Creating Functions with Parameters
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter 2016CISC101 - Prof. McLeod1

2 What is a Function? A function contains code that is isolated from other functions except for a designed interface: –The interface consists of parameters that will contain supplied arguments to be used in a function and a return value that comes out. –You can have as many parameters as you want (or none at all) and can return nothing or a single thing. –The single “thing” can be a tuple of multiple values. Winter 2016

CISC101 - Prof. McLeod3 The Advantages Each function is a building block for your program. Construction, testing and design is easier. Functions avoid code duplication. Functions make re-use of your code more likely. Well written functions reduce the need for internal comments. Winter 2016

CISC101 - Prof. McLeod4 Designing a Function A function should only do one thing. If you describe the function and need to use the word “and”, then it is probably doing more than one thing. Try to keep the parameter list short – 3 or fewer parameters, if possible. Or take advantage of default arguments (more on this later). The function should be short: in the range of 1 to 15 lines ideally. Be prepared to re-structure a working program to get a better design. Winter 2016

CISC101 - Prof. McLeod5 Designing a Function, Cont. Choose good, descriptive function and parameter names, so it is obvious what the function is doing. If you only need to add a bit more code to make your function more universally applicable – do it! Try to always check all your parameter values for legality. Winter 2016

CISC101 - Prof. McLeod6 Designing a Function, Cont. Try to get your function to return something rather than print something. Often main() will have to worry about all the console output. By convention, main() should always be the starting point of your program. Winter 2016

CISC101 - Prof. McLeod7 Program Construction You can start from a functional decomposition of the problem. Write function headers and add parameters. Put the return value in a comment for now. Choose function names that describe what the function does. Make sure each function does one thing only. You may find a need for additional functions as you fill in the code for each function. Don’t be afraid to further decompose a function if it is getting too big or doing too many things. Winter 2016

CISC101 - Prof. McLeod8 Testing and Debugging How can you test a one function program? In this case you need to wait until the entire program is complete before you can start testing! Or, you can choose to test one function at a time by: –Putting a pass command in other empty (for the moment) functions. –Add temporary code to main() to invoke your test function with test values and then display its return value. –You know a failure is from the function under test! Small functions are much easier to debug!! Winter 2016

CISC101 - Prof. McLeod9 You Decide! Multi-function PROs: –Easier to design. –Easier to construct. –Easier to read. –Requires fewer comments. –Easier to test and fix. –Easier to re-use. CONs: –Longer program overall. –Slower? (not much…) Winter 2016

Functional Decomposition A common-sense idea: break a large problem down into smaller pieces! Keep breaking down the pieces until they are small enough to solve. Each piece will represent a function if the problem is a programming problem. CISC101 - Prof. McLeod10Winter 2016

CISC101 - Prof. McLeod11 Example – Washing Machine Control Top-most level: 1.Connect wasther to 110V AC, hot/cold water supply and drain. 2.Place clothes and soap in washer. 3.Washer removes dirt from clothes. 4.Remove clothes containing a minimal amount of water and dirt. We are interested in step 3.

Winter 2016CISC101 - Prof. McLeod12 Example, Cont. – Second Level Step 3 on the previous slide decomposed: 1.Agitate clothes in soapy water. 2.Remove soapy water. 3.Rinse with water. 4.Remove as much water as possible from clothes.

Winter 2016CISC101 - Prof. McLeod13 Example, Cont. – Third Level Step 1 (only) from the previous slide: 1.Lock washer door. 2.Fill machine with water of specified temperature. 3.Activate agitator motor to specified rate. 4.Agitate for specified time period.

Winter 2016CISC101 - Prof. McLeod14 Example, Cont. – Fourth Level Step 2 (only) from the previous slide: 1.Query front panel settings for water temperature. 2.Depending on settings, open cold valve only, hot valve only or both. 3.If water is not flowing activate alarm. 4.Close water input valves when level sensor indicates desired water level has been obtained.

Winter 2016CISC101 - Prof. McLeod15 Example, Cont. – Fourth Level, Pseudocode desiredTemp = getTempSetting(frontpanel) if desiredTemp == hot openValve(hot) else if desiredTemp == cold openValve(cold) else openValve(hot) openValve(cold) if not waterFlowing() sendAlarm(waterError) while not waterFull(desiredLevel) if waterFull(desiredLevel) closeValves()

Winter 2016CISC101 - Prof. McLeod16 Example, Cont. And so on… Continue the process until you have sufficient detail to write pseudocode. Put together a list of functions. List: –The name of the function. –The return type, if any. –The required parameters. –What the function does. –What the function does if input is illegal.

CISC101 - Prof. McLeod17 Two Ways to Use import To tell the interpreter that we wish to use module functions, you need to import the module as in: import math But, if you don’t want to have to say math.sqrt() (ie. you don’t want to have to name the module all the time), you can use a different kind of import statement: from math import * Or, to just import the sqrt() function for example: from math import sqrt Winter 2016

CISC101 - Prof. McLeod18 Module Functions, Cont. If you have used from math import *, for example, then you can invoke any of the math functions directly: print(sqrt(2)) Instead of: print(math.sqrt(2)) Winter 2016

Example – The myInput Module Put our robust input function in a code module and import it into other programs, just like we have been doing with the turtle and math modules. Create myInput.py, which contains only the getInt() and getFloat() functions and does not need to have a main() function. In another program use “import myInput”. Note the creation of a byte code version (*.pyc) of the new module… See myInput.py and FunctionalMortgageCalculatorV2.py. CISC101 - Prof. McLeod19Winter 2016

Aside – Coding Modules Our new module could certainly contain other useful input functions (file input, for example). If you did write a main() in the module, as we have been doing and put in a call to main() in column 1, then this function would run every time the module is imported. CISC101 - Prof. McLeod20Winter 2016

Another Functional Program See the game of pig assignment solution as a functional program. FunctionalGameOfPig.py Could you break this up any further? Winter 2016CISC101 - Prof. McLeod21