Defining Functions. Chapter 6. Wednesday, January 23, Why functions?

Similar documents
Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors

Python Programming: An Introduction to Computer Science

CHAPTER 6 DEFINING FUNCTIONS Python Programming: An Introduction to Computer Science

6. Defining Functions

Python Programming: An Introduction to Computer Science

Lecture. Defining Functions. Richard E Sarkis CSC 161: The Art of Programming

CMSC201 Computer Science I for Majors


Graph Coverage for Source Code. Data Flow Graph Coverage for Source Code

CMSC201 Computer Science I for Majors

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

DECOMPOSITION, ABSTRACTION, FUNCTIONS

6.1 The Function of Functions

Lecture 04 FUNCTIONS AND ARRAYS

Python: Functions. Thomas Schwarz, SJ Marquette University

Week 6: Introduction to Programming for GIS and Spatial Data Analysis GEO GEO A

Lecture 7: Functions. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork

Chapter 1. Fundamentals of Higher Order Programming

More Loop Examples Functions and Parameters

C Programming for Engineers Functions

3. The domain of a function of 2 or 3 variables is a set of pts in the plane or space respectively.

Statements 2. a operator= b a = a operator b

Functions and abstraction. Ruth Anderson UW CSE 160 Winter 2017

Programming II (CS300)

Functions: Decomposition And Code Reuse

Intro. to Computing. Lecture 7 Page1

Introduction to Software Testing Chapter 2.3 Graph Coverage for Source Code. Paul Ammann & Jeff Offutt

Flow of execution! The flow of execution is the order in which program statements are executed.

Organization of Programming Languages CS 3200/5200N. Lecture 09

61A Lecture 2. Wednesday, September 4, 2013

Introduction to Functions. Intro to Computer Science CS1510 Dr. Sarah Diesburg

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

Functions: Decomposition And Code Reuse

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

CSCE 120: Learning To Code

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

RECAP: HIGHER ORDER FUNCTIONS

Programming Languages: Lecture 11

FUNCTION PARAMETER AND ARGUMENT

Programming for Engineers in Python

ENVIRONMENT DIAGRAMS AND RECURSION 2

Lecture #3: Environments

Functions. Learning Outcomes 10/1/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 9 Subprograms

CSE 230 Intermediate Programming in C and C++ Functions

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Methods. CSE 114, Computer Science 1 Stony Brook University

Lecture 5: Methods CS2301

UCT Department of Computer Science Computer Science 1017F. Functions. Lighton Phiri April 2015

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Functions. Structuring Your Python Programs

CS110: PROGRAMMING LANGUAGE I

Functional programming

Functions!!! Why functions? Functions provide good way to design systems!

CS61A Lecture 5 Applications of Higher Order Functions. Jon Kotker and Tom Magrino UC Berkeley EECS June 25, 2012

61A Lecture 2. Friday, August 28, 2015

Overview Graph Coverage Criteria

Here n is a variable name. The value of that variable is 176.

9. Subprograms. 9.2 Fundamentals of Subprograms

Introducing Python Modules

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Introduction to Programming in Python (2)

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

CONTROL AND ENVIRONMENTS 1

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16

UNIT 6. Functions and Structured Programming

CONTROL AND HIGHER ORDER FUNCTIONS 1

Ch.3: Functions and branching

Functions: Decomposition And Code Reuse

Announcements. Topics: To Do:

CS 190C: Introduction to Computational Thinking

15 FUNCTIONS IN C 15.1 INTRODUCTION

Scope and Parameter Passing 1 / 19

MATH 142 Business Mathematics II

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

Semester 2. Structured Programming. Modular Programming Top-down design. Essentials covered. More complex programming topics.

Language Fundamentals

John Perry. Spring 2016

CSI33 Data Structures

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

61A Lecture 9. Friday, September 20

Intro. Scheme Basics. scm> 5 5. scm>

Transformations with Fred Functions- Packet 1

Ch.3: Functions and branching

Using IDLE for

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

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 is going to get really

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Functions. Python Part 3

CS61A Lecture 15. Amir Kamil UC Berkeley February 25, 2013

MATH 22 MORE ABOUT FUNCTIONS. Lecture M: 10/14/2003. Form follows function. Louis Henri Sullivan

Textbook. Topic 6: Functions. Motivation. What is a Function? What s a function? How can we use functions to write better software?

Transcription:

Defining Functions Chapter 6 1 Why functions? Functions allow decomposition of programs into manageable, reusable, components Functions are similar to mathematical functions, but also differ: parameter passing side effects 2

The function of functions What you ve already seen: Programs having a main function Built-in Python functions: abs, min, max,... http://docs.python.org/lib/built-in-funcs.html Library functions: math.sqrt,... Functions capture canned computations that can be reused 3 The function of functions Functions avoid drawbacks of duplicating similar/identical code in different places: writing same code twice maintaining separate copies Functions make code more readable 4

Functions, informally A function is like a subprogram: a small program inside a larger program A function gives a name to that subprogram The subprogram can be executed by referring to the name 5 Functions, informally Functions are first defined The definition can then be used: called / invoked 6

Functions, informally Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred..." Happy birthday to you! 7 Functions, informally Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred..." Happy birthday to you! 8

Functions, informally def happy(): Happy birthday to you! Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred..." Happy birthday to you! 9 Functions, informally def happy(): Happy birthday to you! Happy birthday, dear Fred..." 10

Functions, informally def happy(): Happy birthday to you! Happy birthday, dear Fred..." 11 Functions, informally def happy(): Happy birthday to you! def singfred(): Happy birthday, dear Fred..." 12

Functions, informally def happy(): Happy birthday to you! def singfred(): Happy birthday, dear Fred..." 13 Functions, informally def happy(): Happy birthday to you! def singfred(): Happy birthday, dear Fred..." 14

Functions, informally def happy(): Happy birthday to you! def singfred(): Happy birthday, dear Fred..." def singlucy(): Happy birthday, dear Lucy..." 15 Functions, informally! singfred()! singlucy() 16

Functions, informally Happy birthday dear, person\!! +...! singfred()! singlucy() 17 Functions, informally Happy birthday dear, person\!! +...! singfred()! singlucy() 18

Functions, informally Happy birthday dear, person\!! +... 19 Functions, informally A parameter is a variable that is initialized to the value of the corresponding argument when the function is called 20

Functions, informally Functions use bound variables and unbound variables This is tricky! x = 10 def f(x): return x+1 def g(): return x+1 21 Functions & parameters: Variables are bound to scopes Different scopes can use the same name for different variables Variables can be globally scoped Variables can be scoped locally within a function 22

Functions & parameters: x = 10 def f(x):! x = x+1! return x x = 11! y=f(x) x,y x = 12 main() 23 Functions & parameters: x = 10 def f(x):! x = x+1! return x x = 11! y=f(x) x,y x = 12 main() What does this program print out? A. 10 11 B.12 13 C. 11 11 D. 12 12 24

Functions & parameters: x = 10 def f(x):! x = x+1! return x x = 11! y=f(x) x,y x = 12 main() What does this program print out? A. 10 11 B.12 13 C. 11 11 D. 12 12 25 Functions & parameters: Parameters, like all variables defined within a function, are only accessible in the body of their function Variables with the same names elsewhere in the program are distinct from parameters and variables defined in a function 26

Functions & parameters: A function is defined with a possibly empty list of parameters: def name(parameters): body A function is called by using its name followed by a list of arguments, one for each parameter: name(arguments) 27 Functions & parameters: At a call: Execution of the caller is suspended The arguments to the call are assigned to the parameters of the called function The body of the called function is executed Execution of the caller is resumed 28

Functions & parameters: 29 Functions & parameters: Happy birthday, dear, person+... 30

Functions & parameters: person = Fred Happy birthday, dear, person+... 31 Functions & parameters: person = Fred Happy birthday, dear, person+... 32

Functions & parameters: person = Fred Happy birthday, dear, person+... def happy(): Happy birthday to you! 33 Functions & parameters: person = Fred Happy birthday, dear, person+... def happy(): Happy birthday to you! 34

Functions & parameters: person = Fred Happy birthday, dear, person+... def happy(): Happy birthday to you! 35 Functions & parameters: person = Fred Happy birthday, dear, person+... def happy(): Happy birthday to you! 36

Functions & parameters: person = Fred Happy birthday, dear, person+... 37 Functions & parameters: person = Fred Happy birthday, dear, person+... 38

Functions & parameters: person = Fred Happy birthday, dear, person+... 39 Functions & parameters: person = Fred Happy birthday, dear, person+... 40

Functions & parameters: 41 Functions & parameters: 42

Functions & parameters: Happy birthday, dear, person+... 43 Functions & parameters: person = Lucy Happy birthday, dear, person+... 44

Functions & parameters: person = Lucy Happy birthday, dear, person+... 45 Functions & parameters: person = Lucy Happy birthday, dear, person+... 46

Functions & parameters: person = Lucy Happy birthday, dear, person+... 47 Functions & parameters: person = Lucy Happy birthday, dear, person+... 48

Functions & parameters: person = Lucy Happy birthday, dear, person+... 49 Functions & parameters: 50

Functions & parameters: Multiple parameters are matched up by position: arguments are assigned one by one to their parameters in the order they appear Consider: def f(x,y): return x+y f(1,2) assigns 1 to x then 2 to y 51 Getting results from functions Arguments are inputs to functions, initializing their parameters Calling the same function with different arguments can produce different results We ve already seen use of the return statement to provide a result 52

Example def square(x):! return x*x def distance(p1, p2):!! d = math.sqrt(!! square(p2[0] - p1[0]) + square(p2[1] - p1[1]))! return d 53 Getting results from functions Functions can return multiple results Get multiple results from call using multiple assignment def sum_diff(x,y):! sum = x+y! diff = x-y! return sum, diff s,d = sum_diff(a,b) 54

Getting results from functions Be careful not to forget the return for functions that have a result Functions without a return actually return a result None 55 Functions that modify parameters Assigning to a parameter is a local change to the local parameter, not the argument def f(x):! x = x+1! return x x=10 y=f(x) This leaves x=10 and y=11 56

Functions that modify parameters Python passes arguments by assigning their values to the parameters But, some values are actually references to structures : lists, arrays, sequences Assigning to the element of a structure does change the value of that element 57 Functions that modify parameters Assigning to an element of a parameter changes that element This leaves def f(x):! x[0] += 1 x=[1,2] f(x) x=[2,2] 58

Functions & modularity Functions reduce code duplication They also enhance program modularity by allowing complex programs to be broken down into manageable subprograms that can be understood independently More to come later in Chapter 9 59