Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

User Defined Functions
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Python Programming: An Introduction to Computer Science
Introduction to Computers and Programming Introduction to Methods in Java.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Introduction to Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
CSC 110 Defining functions [Reading: chapter 6] CSC 110 G 1.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Vahé Karamian Python Programming CS-110 CHAPTER 6 Defining Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Building java programs, chapter 3 Parameters, Methods and Objects.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Programming Fundamentals Enumerations and Functions.
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CMSC201 Computer Science I for Majors Lecture 13 – Functions
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Python Programming: An Introduction to Computer Science
Chapter 6 Functions.
CS 115 Lecture 8 Structured Programming; for loops
Functions CIS 40 – Introduction to Programming in Python
CSCI 161: Introduction to Programming Function
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
User-Defined Functions
CMSC201 Computer Science I for Majors Lecture 14 – Functions (Continued) Prof. Katherine Gibson Based on concepts from:
5. Functions Rocky K. C. Chang 30 October 2018
Elements of a Python Program
Chapter 6: User-Defined Functions I
15-110: Principles of Computing
答題分佈 Python Programming, 2/e.
Introduction to Computer Science
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
def-ining a function A function as an execution control structure
Defining Functions.
Presentation transcript:

Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions

Python Programming, 2/e2 Objectives To understand why programmers divide programs up into sets of cooperating functions. To be able to define new functions in Python. To understand the details of function calls and parameter passing in Python.

Python Programming, 2/e3 Objectives (cont.) To write programs that use functions to reduce code duplication and increase program modularity.

Python Programming, 2/e4 The Function of Functions So far, we ’ ve seen three different types of functions: Our programs comprise a single function called main(). Built-in Python functions (round, abs) Functions from the standard libraries (math.sqrt, math.sin)

Python Programming, 2/e5 The Function of Functions Having similar or identical code in more than one place has some drawbacks. Issue one: writing the same code twice or more. Issue two: This same code must be maintained in two separate places. Functions can be used to reduce code duplication and make programs more easily understood and maintained.

Python Programming, 2/e6 Functions, Informally A function is like a subprogram, a small program inside of a program. The basic idea – we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name.

Python Programming, 2/e7 Functions, Informally The part of the program that creates a function is called a function definition. When the function is used in a program, we say the definition is called or invoked.

Python Programming, 2/e8 Functions, Informally Example of function (intentional bug) # Using a main method like the book # This code has an intentional bug def main(): a, b, c = eval(input("Enter 3 numbers: ")) if(a >= b >= c): max = a; if(b >= a >= c): max = b; if(c >= b >= a): max = a; print('Max: ', max) main()

Python Programming, 2/e9 Functions, Informally Let's put the buggy ifs in a new function def maxOf3(a, b, c): if(a >= b >= c): max = a; if(b >= a >= c): max = b; if(c >= b >= a): max = a; return max def main(): a, b, c = eval(input("Enter 3 numbers: ")) print('Max: ', maxOf3(a, b, c)) main()

Python Programming, 2/e10 Functions, Informally Change main to test the new function def main(): # All permutations of three unique numbers print('3? ', maxOf3(1, 2, 3)) print('3? ', maxOf3(1, 3, 2)) print('3? ', maxOf3(2, 3, 1)) print('3? ', maxOf3(2, 1, 3)) # The other two?

Python Programming, 2/e11 Functions, Informally Creating this function captured the logic If we can get this function working, it would be nice. Debug and test it now.

Python Programming, 2/e12 Functions and Parameters: The Details Each function is its own little subprogram. The variables used inside of a function are local to that function, even if they happen to have the same name as variables that appear inside of another function. The only way for a function to see a variable or to get a value from another place is for that variable to be passed as a parameter.

Python Programming, 2/e13 Functions and Parameters: The Details A function definition looks like this: def ( ): The name of the function must be an identifier Formal-parameters is a possibly empty list of variable names

Python Programming, 2/e14 Functions and Parameters: The Details Formal parameters, like all variables used in the function, are only accessible in the body of the function. Variables with identical names elsewhere in the program are distinct from the formal parameters and variables inside of the function body.

Python Programming, 2/e15 Functions and Parameters: The Details A function is called by using its name followed by a list of actual parameters or arguments 1. ( ) When Python comes to a function call, it initiates a four-step process. 1 Rick will use the term arguments, John uses the term actual parameters

Python Programming, 2/e16 Functions and Parameters: The Details The calling program suspends execution at the point of the call. The formal parameters of the function get assigned the values supplied by the actual parameters in the call. The body of the function is executed. Control returns to the point just after where the function was called.

Python Programming, 2/e17 Functions and Parameters: The Details Trace through the current program with a main method that tests maxOf3 Then move maxOf3 into another file named myFunctions.py Leave the main method to test, but we need to now import that function from myFunctions import maxOf3

Python Programming, 2/e18 Functions and Paramters: The Details One thing not addressed in this example was multiple parameters. In this case the formal and actual parameters are matched up based on position, e.g. the first actual parameter is assigned to the first formal parameter, the second actual parameter is assigned to the second formal parameter, etc.

Python Programming, 2/e19 Getting Results from a Function Passing parameters provides a mechanism for initializing the variables in a function. Parameters act as inputs to a function. We can call a function many times and get different results by changing its parameters.

Python Programming, 2/e20 Functions That Return Values We ’ ve already seen numerous examples of functions that return values to the caller. discRt = math.sqrt(b*b – 4*a*c) The value b*b – 4*a*c is the actual parameter of math.sqrt. We say sqrt returns the square root of its argument.

Python Programming, 2/e21 Functions That Return Values This function returns the square of a number: def square(x): return x*x When Python encounters return, it exits the function and returns control to the point where the function was called. In addition, the value(s) provided in the return statement are sent back to the caller as an expression result.

Python Programming, 2/e22 Functions That Return Values >>> square(3) 9 >>> print(square(4)) 16 >>> x = 5 >>> y = square(x) >>> print(y) 25 >>> print(square(x) + square(3)) 34

Consider CodingBats In class, write the answers to those shown Python Programming, 2/e23