Fundamentals of Python: First Programs. Chapter 6: Design with Functions

Similar documents
[CHAPTER] 1 INTRODUCTION 1

Computer Hardware 6. The

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Run-time Environments

Run-time Environments

Writing Functions. Reading: Dawson, Chapter 6. What is a function? Function declaration and parameter passing Return values Objects as parameters

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Computing Fundamentals Advanced functions & Recursion

6.001 Notes: Section 4.1

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Functions: Decomposition And Code Reuse

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

142

Repetition Through Recursion

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Trees, Part 1: Unbalanced Trees

Chapter 6 Introduction to Defining Classes

Methods. CSE 114, Computer Science 1 Stony Brook University

6. C++ Subprograms David Keil CS I slides 7/03 1

C & Data Structures syllabus

Zend Studio 3.0. Quick Start Guide

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Implementing Subprograms

Course: Operating Systems Instructor: M Umair. M Umair

Chapter 3: Modules. Starting Out with Programming Logic & Design. Second Edition. by Tony Gaddis

Programming II (CS300)

Iteration Part 1. Motivation for iteration. How does a for loop work? Execution model of a for loop. What is Iteration?

User-defined Functions. Conditional Expressions in Scheme

Software Development. Modular Design and Algorithm Analysis

ENGR 102 Engineering Lab I - Computation

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Chapter 3:: Names, Scopes, and Bindings

Answers to review questions from Chapter 2

Programming Languages Third Edition. Chapter 7 Basic Semantics

Short Notes of CS201

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming II (CS300)

Topic IV. Block-structured procedural languages Algol and Pascal. References:

CS201 - Introduction to Programming Glossary By

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Run-Time Environments/Garbage Collection

Functions: Decomposition And Code Reuse

Programming Language Implementation

COMP-520 GoLite Tutorial

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Functions: Decomposition And Code Reuse

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace

Functional programming with Common Lisp

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

G Programming Languages - Fall 2012

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

A Brief Introduction to Scheme (I)

Structure of Programming Languages Lecture 10

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

Module 1. Introduction:

Programming II (CS300)

Code Generation & Parameter Passing

Process size is independent of the main memory present in the system.

Table of Contents. Preface... xxi

CS558 Programming Languages

Chapter 1. Fundamentals of Higher Order Programming

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Code Generation. Lecture 12

INTRODUCTION TO ALGORITHMS

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

6.001 Notes: Section 15.1

Dynamic Programming. Other Topics

Chapter 10. Implementing Subprograms

Names, Scope, and Bindings

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 18: Naming, Directories, and File Caching

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

VALLIAMMAI ENGINEERING COLLEGE

Chapter 16. Burroughs' B6500/B7500 Stack Mechanism 1. E. A. Hauck / B. A. Dent. Introduction

6.001 Notes: Section 8.1

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical


CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 18: Naming, Directories, and File Caching

LAB: INTRODUCTION TO FUNCTIONS IN C++

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

MAPILab Statistics for SharePoint User Guide

Garbage Collection. Steven R. Bagley

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Lecture 15 Garbage Collection

Introduction to Scheme

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Chapter 6: Methods. Objectives 9/21/18. Opening Problem. Problem. Problem. Solution. CS1: Java Programming Colorado State University

School of Informatics, University of Edinburgh

PROBLEM SOLVING TECHNIQUES SECTION - A. 1. Answer any ten of the following

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Chapter 1: Principles of Programming and Software Engineering

Accelerated Library Framework for Hybrid-x86

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

CS301 - Data Structures Glossary By

Optimizing Closures in O(0) time

Transcription:

Fundamentals of Python: First Programs Chapter 6: Design with Functions

Objectives After completing this chapter, you will be able to: Explain why functions are useful in structuring code in a program Employ top-down design to assign tasks to functions Define a recursive function Fundamentals of Python: First Programs 2

Objectives (continued) Explain the use of the namespace in a program and exploit it effectively Define a function with required and optional parameters Use higher-order functions for mapping, filtering, and reducing Fundamentals of Python: First Programs 3

Functions as Abstraction Mechanisms An abstraction hides detail Allows a person to view many things as just one thing We use abstractions to refer to the most common tasks in everyday life For example, the expression doing my laundry Effective designers must invent useful abstractions to control complexity Fundamentals of Python: First Programs 4

Functions Eliminate Redundancy Functions serve as abstraction mechanisms by eliminating redundant, or repetitious, code Fundamentals of Python: First Programs 5

Functions Hide Complexity Functions serve as abstraction mechanisms is by hiding complicated details For example, consider the previous sum function The idea of summing a range of numbers is simple; the code for computing a summation is not A function call expresses the idea of a process to the programmer Without forcing him/her to wade through the complex code that realizes that idea Fundamentals of Python: First Programs 6

Functions Support General Methods with Systematic Variations An algorithm is a general method for solving a class of problems The individual problems that make up a class of problems are known as problem instances What are the problem instances of our summation algorithm? Algorithms should be general enough to provide a solution to many problem instances A function should provide a general method with systematic variations Fundamentals of Python: First Programs 7

Functions Support the Division of Labor In a well-organized system, each part does its own job in collaborating to achieve a common goal In a computer program, functions can enforce a division of labor Each function should perform a single coherent task Example: Computing a summation Each of the tasks required by a system can be assigned to a function Including the tasks of managing or coordinating the use of other functions Fundamentals of Python: First Programs 8

Problem Solving with Top-Down Design Top-down design starts with a global view of the entire problem and breaks the problem into smaller, more manageable subproblems Process known as problem decomposition As each subproblem is isolated, its solution is assigned to a function As functions are developed to solve subproblems, solution to overall problem is gradually filled out Process is also called stepwise refinement Fundamentals of Python: First Programs 9

The Design of the Text-Analysis Program Fundamentals of Python: First Programs 10

The Design of the Sentence-Generator Program Fundamentals of Python: First Programs 11

The Design of the Doctor Program Fundamentals of Python: First Programs 12

Design with Recursive Functions In top-down design, you decompose a complex problem into a set of simpler problems and solve these with different functions In some cases, you can decompose a complex problem into smaller problems of the same form Subproblems can be solved using the same function This design strategy is called recursive design Resulting functions are called recursive functions Fundamentals of Python: First Programs 13

Defining a Recursive Function A recursive function is a function that calls itself To prevent function from repeating itself indefinitely, it must contain at least one selection statement Statement examines base case to determine whether to stop or to continue with another recursive step To convert displayrange to a recursive function: You can replace loop with a selection statement and assignment statement with a recursive call Fundamentals of Python: First Programs 14

Defining a Recursive Function (continued) Making displayrange recursive (continued): Most recursive functions expect at least one argument Another example: Recursive version of sum Fundamentals of Python: First Programs 15

Tracing a Recursive Function Fundamentals of Python: First Programs 16

Using Recursive Definitions to Construct Recursive Functions Recursive functions are frequently used to design algorithms that have a recursive definition A recursive definition consists of equations that state what a value is for one or more base cases and one or more recursive cases Example: Fibonacci sequence 1 1 2 3 5 8 13... Fundamentals of Python: First Programs 17

Recursion in Sentence Structure Recursive solutions can often flow from the structure of a problem Example: Structure of sentences in a language A noun phrase can be modified by a prepositional phrase, which also contains another noun phrase Indirect recursion Fundamentals of Python: First Programs 18

Infinite Recursion Infinite recursion arises when programmer fails to specify base case or to reduce size of problem in a way that terminates the recursive process In fact, the Python virtual machine eventually runs out of memory resources to manage the process Fundamentals of Python: First Programs 19

The Costs and Benefits of Recursion PVM reserves an area of memory for the call stack For each call of a function, the PVM must allocate on the call stack a stack frame, which contains: Values of the arguments Return address for the particular function call Space for the function call s return value When a call returns, return address is used to locate the next instruction, and stack frame is deallocated Amount of memory needed for a loop does not grow with the size of the problem s data set Fundamentals of Python: First Programs 20

The Costs and Benefits of Recursion (continued) Fundamentals of Python: First Programs 21

Case Study: Gathering Information from a File System Request: Write a program that allows the user to obtain information about the file system Analysis: File systems are tree-like structures At the top of the tree is the root directory Under the root are files and subdirectories Each directory in the system except the root lies within another directory called its parent Example of a path (UNIX-based file system): /Users/KenLaptop/Book/Chapter6/Chapter6.doc Fundamentals of Python: First Programs 22

Case Study: Gathering Information from a File System (continued) Fundamentals of Python: First Programs 23

Case Study: Gathering Information from a File System (continued) When user enters a number, program runs command; then, displays CWD and menu again An unrecognized command produces an error message Fundamentals of Python: First Programs 24

Case Study: Gathering Information from a File System (continued) Fundamentals of Python: First Programs 25

Case Study: Gathering Information Design: from a File System (continued) Fundamentals of Python: First Programs 26

Case Study: Gathering Information from a File System (continued) Fundamentals of Python: First Programs 27

Managing a Program s Namespace A program s namespace is the set of its variables and their values You can control it with good design principles Fundamentals of Python: First Programs 28

Module Variables, Parameters, and Temporary Variables doctor.py file (module name is doctor): A module variable A parameter name A temporary variable A method name Module variables and temporary variables receive their values as soon as they are introduced Parameters behave like a variable and are introduced in a function or method header Do not receive a value until the function is called Fundamentals of Python: First Programs 29

Scope Scope: Area in which a name refers to a given value Temporary variables are restricted to the body of the functions in which they are introduced Parameters are invisible outside function definition The scope of module variables includes entire module below point where they are introduced A function can reference a module variable, but can t under normal circumstances assign a new value to it Fundamentals of Python: First Programs 30

Lifetime Variable s lifetime: Period of time when variable has memory storage associated with it When a variable comes into existence, storage is allocated for it; when it goes out of existence, storage is reclaimed by the PVM Module variables come into existence when introduced and generally exist for lifetime of program that introduces or imports them Parameters and temporary variables come into existence when bound to values during call, but go out of existence when call terminates Fundamentals of Python: First Programs 31

Default (Keyword) Arguments Arguments provide the function s caller with the means of transmitting information to the function Programmer can specify optional arguments with default values in any function definition: Following the required arguments are one or more default or keyword arguments When function is called with these arguments, default values are overridden by caller s values Fundamentals of Python: First Programs 32

Default (Keyword) Arguments (continued) Fundamentals of Python: First Programs 33

Default (Keyword) Arguments (continued) The default arguments that follow can be supplied in two ways: By position By keyword Fundamentals of Python: First Programs 34

Higher-Order Functions (Advanced Topic) A higher-order function expects a function and a set of data values as arguments Argument function is applied to each data value and a set of results or a single data value is returned A higher-order function separates task of transforming each data value from logic of accumulating the results Fundamentals of Python: First Programs 35

Functions as First-Class Data Objects Functions can be assigned to variables, passed as arguments, returned as the values of other functions, and stored in data structures Fundamentals of Python: First Programs 36

Functions as First-Class Data Objects (continued) Passing a function as an argument is no different from passing any other datum: Fundamentals of Python: First Programs 37

Mapping Mapping applies a function to each value in a sequence and returns a new sequence of the results Fundamentals of Python: First Programs 38

Mapping (continued) Fundamentals of Python: First Programs 39

Filtering When filtering, a function called a predicate is applied to each value in a list If predicate returns True, value is added to a new list; otherwise, value is dropped from consideration Fundamentals of Python: First Programs 40

Reducing When reducing, we take a list of values and repeatedly apply a function to accumulate a single data value Fundamentals of Python: First Programs 41

Using lambda to Create Anonymous Functions A lambda is an anonymous function When the lambda is applied to its arguments, its expression is evaluated and its value is returned Fundamentals of Python: First Programs 42

Creating Jump Tables A jump table is a dictionary of functions keyed by command names Fundamentals of Python: First Programs 43

Summary A function serves as abstraction mechanism and eliminates redundant patterns of code Top-down design is strategy that decomposes complex problem into simpler subproblems and assigns their solutions to functions A structure chart is diagram of relationships among cooperating functions Recursive design is special case of top-down design, in which complex problem is decomposed into smaller problems of the same form Fundamentals of Python: First Programs 44

Summary (continued) A recursive function is a function that calls itself Parts: Base case and recursive step Can be computationally expensive Programmers must avoid infinite recursion Program namespace structured in terms of module variables, parameters, and temporary variables Scope can be used to control the visibility of names in a namespace The lifetime of a variable is duration of program execution during which it uses memory storage Fundamentals of Python: First Programs 45

Summary (continued) Functions are first-class data objects Higher-order functions can expect other functions as arguments and/or return functions as values A mapping function expects a function and a list of values as arguments A predicate is a Boolean function A filtering function expects a predicate and a list of values as arguments A reducing function expects a function and a list of values as arguments Fundamentals of Python: First Programs 46