Lecture 5 Fruitful Functions

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

User Defined Functions
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
PYTHON FRUITFUL FUNCTIONS CHAPTER 6 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Q and A for Chapter 6 CS 104 Victor Norman. Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Introduction. 2COMPSCI Computer Science Fundamentals.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CPS120: Introduction to Computer Science Functions.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
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.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Methods: functions & procedures. Top-down programming Divide-and-conquer technique Divide-and-conquer technique Problem is broken down into a series of.
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.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Python Programming Module 3 Functions Python Programming, 2/e1.
Arrays Chapter 7.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Topic: Recursion – Part 2
Topics Introduction to Functions Defining and Calling a Void Function
Python Programming Module 3 Functions Python Programming, 2/e.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CMPT 120 Topic: Functions – Part 4
Control Statements Lecture 6 from Chapter 5.
Structural testing, Path Testing
CSCI 161: Introduction to Programming Function
User-Defined Functions
Bryan Burlingame 26 September 2018
Lesson #6 Modular Programming and Functions.
User Defined Functions
Bryan Burlingame 03 October 2018
Improving the Design “Can the design be better?”
Chapter 6 Methods: A Deeper Look
Bryan Burlingame Halloween 2018
Fundamentals of Programming
Lecture 2 Python Programming & Data Types
Bryan Burlingame 17 October 2018
Coding Concepts (Sub- Programs)
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Bryan Burlingame 28 November 2018
Lecture 2 Python Programming & Data Types
Bryan Burlingame 13 February 2019
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Lesson #6 Modular Programming and Functions.
Bryan Burlingame 6 March 2019
Bryan Burlingame 13 March 2019
Bryan Burlingame Halloween 2018
Loops and Simple Functions
Bryan Burlingame 17 April 2019
Bryan Burlingame 24 April 2019
Lecture 13 Teamwork Bryan Burlingame 1 May 2019.
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Standard Version of Starting Out with C++, 4th Edition
def-ining a function A function as an execution control structure
Introduction to Methods and Interfaces
Presentation transcript:

Lecture 5 Fruitful Functions Bryan Burlingame 19 September 2018

Announcements Homework 3 due up front Read Chapter 7 & 20

Learning Objectives Revisit functions and discuss return values Incremental development None as a value

Revisiting Functions Recall: Functions are named sequences of instructions which perform some action Functions accept parameters in a parameter list and return values A function call is the activation of a function Python has many built in functions and many additional useful modules Modules are collections of functions with similar purpose Example: the math module with sin, cos, tan, etc. Functions are defined with the def keyword

Fruitful Functions Allen Downey defines a fruitful function as a function which returns a value defined by the programmer The value a function returns is simply called the return value We have been using fruitful functions for some time ex: math.sin(angle) and math.cos(angle) are both fruitful functions

Fruitful Functions Allen Downey defines a fruitful function as a function which returns a value defined by the programmer The value a function returns is simply called the return value We have been using fruitful functions for some time ex: math.sin(angle) and math.cos(angle) are both fruitful functions Recall: one can use a function call anywhere the return value can be used

Defining Fruitful Functions A return value is identified using the return keyword Parameter list Function definition Return value Function call

Refactoring Refactoring is the process of restructuring some set of code without changing its function In this example, I’ve refactored both the areaRect function and __main__. Which is superior?

Return values Multiple return statements are allowed, though the first return executed ends the function and returns the return value

Return values Multiple return statements are allowed, though the first return executed ends the function and returns the return value What’s the return value if the current speed == the speed limit?

Return values Multiple return statements are allowed, though the first return executed ends the function and returns the return value None is the default return value. All void functions actually have a return value: None

Return values All branches in a function should return a value None is the default return value. All void functions actually have a

Composition Recall: a function can be called from within another function Problem: find area of a rectangle, given coordinates of opposite corners (12,12) (1,1)

Algorithm Recall: An algorithm is an ordered set of instructions defining some process What is the algorithm necessary to find the area of a rectangle, given the points of the corners?

Algorithm Recall: An algorithm is an ordered set of instructions defining some process What is the algorithm necessary to find the area of a rectangle, given the points of the corners? Obtain the two points Calculate the length of each side Multiply the lengths of the two sides together to obtain the area Return the area

Algorithm Recall: An algorithm is an ordered set of instructions defining some process What is the algorithm necessary to find the area of a rectangle, given the points of the corners? Obtain the two points Calculate the length of each side Multiply the lengths of the two sides together to obtain the area Return the area

Incremental Development Incremental development is the process of developing a program in small chunks (increments) Stub functions are functions which only implement the interfaces (parameter lists and return values) to allow for incremental development Note how areaRect and dist do not do anything, but they do accept the proper values and the do return a value of the proper type

Algorithm The distance between two points uses the distance formula 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒= ( 𝑋 1 − 𝑋 2 ) 2 − ( 𝑌 1 − 𝑌 2 ) 2

Algorithm Area of a rectangle is height * width Note how each function is being tested independently

Incremental Development By building up the program in increments we can test each function separately This allows us to focus on one part at a time Get one thing working before moving on to the next

Recursion Revisited Recursion becomes useful, once each call can return values to the previous call What’s the general algorithm to calculate a factorial n == 0? Return 1 otherwise return n * factorial(n- 1)

Recursion Revisited Recursion becomes useful, once each call can return values to the previous call What’s the general algorithm to calculate a factorial n == 0? Return 1 otherwise return n * factorial(n- 1) How good is this? What is fact(1.5)? What is fact(-1)

Recursion Revisited Better Test for invalid values and then return None when something is invalid, the text calls this a guardian Data validation, bounds check None is not a number, it is the absence of an answer Use the is operator to test for None (== works in the simple cases, is is better) Note that we now have multiple base cases for this function Note the isinstance function, which can be used to check datatype

Resources Downey, A. (2016) Think Python, Second Edition Sebastopol, CA: O’Reilly Media (n.d.). 3.7.0 Documentation. 6. Expressions — Python 3.7.0 documentation. Retrieved September 11, 2018, from http://docs.python.org/3.7/reference/expressions.html