CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)

Slides:



Advertisements
Similar presentations
Python Programming: An Introduction to Computer Science
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
FUNCTIONS in Python.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.
CPS120: Introduction to Computer Science Functions.
CSC 110 Defining functions [Reading: chapter 6] CSC 110 G 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
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 Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Announcements Reading Read Chapter 4 (functions).
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Announcements Reading Project1 Codelab: Read Chapter 4 (functions)
CMSC201 Computer Science I for Majors Lecture 09 – Functions
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CS 1110 Introduction to Programming Spring 2017
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
FIGURE 4-10 Function Return Statements
Chapter 6 Functions.
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
CMSC201 Computer Science I for Majors Lecture 15 – For Loops
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Building Java Programs
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
CMSC201 Computer Science I for Majors Lecture 14 – Functions (Continued) Prof. Katherine Gibson Based on concepts from:
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
5. Functions Rocky K. C. Chang 30 October 2018
Chapter 6: User-Defined Functions I
15-110: Principles of Computing
答題分佈 Python Programming, 2/e.
COMPUTER PROGRAMMING SKILLS
Introduction to Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
ITM 352 Functions.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
def-ining a function A function as an execution control structure
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Defining Functions.
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)

Last Class We Covered Functions Why they’re useful When you should use them Calling functions Variable scope Passing parameters

Any Questions from Last Time?

Quick Announcement Update made to the output (but not the directions) for Homework 4, Part 2 Exclamation marks are valid if they appear anywhere in the password, not only at the end Please enter a password: sciencerules The password is all lowercase, so it must contain the character ! to be secure. Please enter a password: science!rules Thanks for picking the password science!rules

Today’s Objectives To introduce value-returning functions To better grasp how values in the scope of a function actually work To understand mutability (and immutability) To practice function calls

Review: Parts of a Function

Function Vocabulary def myFunc(year, name): # lines of code _______ ____ _____ _________ ______ ____ _______ _______ def myFunc(year, name): # lines of code # more lines of code def main(): myFunc(2015, "Xavier") main() _______ ___ _____ _________

Function Vocabulary def myFunc(year, name): # lines of code function n___ f____ p________ function b___ function d______ def myFunc(year, name): # lines of code # more lines of code def main(): myFunc(2015, "Xavier") main() function c__ a____ p________

Function Vocabulary def myFunc(year, name): # lines of code function name formal parameters function body function definition def myFunc(year, name): # lines of code # more lines of code def main(): myFunc(2015, "Xavier") main() function call actual parameters

File Layout and Constants

definitions for all functions other than main() Layout of a Python File header comment # File: gradeGetter.py # Author: Dr. Gibson MIN_GRADE = 0 MAX_GRADE = 100 def getInput(min, max): msg = "Please enter..." val = int(input( msg )) while val < min or val > max: # more code here return val def main(): grade = getInput(MIN_GRADE, MAX_GRADE) print("You got a", grade) main() constants definitions for all functions other than main() main() definition call to main()

Global Constants Globals are variables declared outside of any function (including main() ) Accessible globally in your program To all functions and code Your programs may not have global variables Your programs may use global constants In fact, constants should generally be global

Return Statements

Giving Information to 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

Getting Information from a Function We’ve already seen numerous examples of functions that return values int() , str(), input(), etc. For example, int() Takes in any string as its parameter Processes the digits in the string And returns an integer value

Functions that Return Values To have a function return a value after it is called, we need to use the return keyword def square(num1): # return the square return (num1 * num1)

Handling Return Values When Python encounters return, it Exits the function (immediately!) Even if it’s not the end of the function Returns control back to where the function was called from The value provided in the return statement is sent back to the caller as an expression result

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() Step 1: Call main()

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() Step 1: Call main() Step 2: Pass control to def main()

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() Step 1: Call main() Step 2: Pass control to def main() Step 3: Set x = 5

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() Step 1: Call main() Step 2: Pass control to def main() Step 3: Set x = 5 Step 4: See the function call to square()

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() Step 1: Call main() Step 2: Pass control to def main() Step 3: Set x = 5 Step 4: See the function call to square() Step 5: Pass control from main() to square()

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() num1: 5 Step 1: Call main() Step 2: Pass control to def main() Step 3: Set x = 5 Step 4: See the function call to square() Step 5: Pass control from main() to square() Step 6: Set the value of num1 in square() to x

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() num1: 5 Step 1: Call main() Step 2: Pass control to def main() Step 3: Set x = 5 Step 4: See the function call to square() Step 5: Pass control from main() to square() Step 6: Set the value of num1 in square() to x Step 7: Calculate num1 * num1

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() num1: 5 Step 1: Call main() Step 2: Pass control to def main() Step 3: Set x = 5 Step 4: See the function call to square() Step 5: Pass control from main() to square() Step 6: Set the value of num1 in square() to x Step 7: Calculate num1 * num1 Step 8: Return to main() and set y = return statement

Code Trace: Return from square() Let’s follow the flow of the code def square(num1): return num1 * num1 def main(): x = 5 y = square(x) print(y) main() Step 1: Call main() Step 2: Pass control to def main() Step 3: Set x = 5 Step 4: See the function call to square() Step 5: Pass control from main() to square() Step 6: Set the value of num1 in square() to x Step 7: Calculate num1 * num1 Step 8: Return to main() and set y = return statement Step 9: Print value of y

Testing: Return from square() >>> print(square(3)) 9 >>> print(square(4)) 16 >>> x = 5 >>> y = square(x) >>> print(y) 25 >>> print(square(x) + square(3)) 34

Island Example

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() square() 5 https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ X = 5 main() USS Actual Params www.umbc.edu Images from pixabay.com

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ 5 X = 5 main() USS Actual Params square() Images from pixabay.com

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ 5 X = 5 main() USS Actual Params square() Images from pixabay.com

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() Assign value to num1 https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ 5 num1 = 5 X = 5 main() USS Actual Params square() Images from pixabay.com

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() Assign value to num1 Execute num1 * num1 (No name for it yet) 25 https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ num1 = 5 X = 5 main() square() Images from pixabay.com

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() Assign value to num1 Execute num1 * num1 (No name for it yet) Return calculated value square() 25 https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ num1 = 5 25 X = 5 USS Return Values main() www.umbc.edu Images from pixabay.com

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() Assign value to num1 Execute num1 * num1 (No name for it yet) Return calculated value https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ 25 X = 5 USS Return Values main() Images from pixabay.com

Function square() is called Make copy of x’s value (no name yet) Pass value of x to square() Assign value to num1 Execute num1 * num1 (No name for it yet) Return calculated value Assign value to y y = 25 25 https://pixabay.com/en/box-crate-moving-package-wooden-41658/ https://pixabay.com/en/boat-flag-pirates-rowboat-307603/ https://pixabay.com/en/coconut-tree-palm-tree-dune-tree-1892861/ X = 5 USS Return Values main() Images from pixabay.com

None and Common Problems

Every Function Returns Something All Python functions return a value Even if they don’t have a return statement Functions without an explicit return pass back a special object, called None None is the absence of a value

Common Errors and Problems Writing a function that returns a value but… Forgetting to include the return statement >>> def multiply(num1, num2): ... print("doing", num1, "*", num2) ... answer = num1 * num2 >>> product = multiply(3, 5) doing 3 * 5 >>> print(product) None Variable assigned to the return value will be None

Common Errors and Problems Writing a function that returns a value but… Forgetting to assign that value to anything >>> def multiply(num1, num2): ... print("doing", num1, "*", num2) ... return num1 * num2 >>> product = 0 >>> multiply(7, 8) doing 7 * 8 >>> print(product) The variable product was not updated; the code should have read product = multiply(7, 8)

Common Errors and Problems If your value-returning functions produce strange messages, check to make sure you used the return correctly! TypeError: 'int' object is not iterable TypeError: 'NoneType' object is not iterable

“Modifying” Parameters

Bank Interest Example Suppose you are writing a program that manages bank accounts One function we would need to create is one to accumulate interest on the account def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance

Bank Interest Example We want to set the balance of the account to a new value that includes the interest amount def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def main(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount) main() What is the output of this code? https://pixabay.com/en/finance-dollar-financial-world-634901/ 1000 Is this what we expected? Image from pixabay.com

What’s Going On? It was intended that the 5% would be added to the amount, returning $1050 Was $1000 the expected output? No – so what went wrong? This is a very common mistake to make! Let’s trace through the code and figure it out

Tracing the Bank Interest Code First, we create two variables that are local to main() def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def main(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount) main() local variables of main()

Tracing the Bank Interest Code Second, we call addInterest() and pass the values of the local variables of main() as actual parameters def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def main(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount) main() Passing the values stored in amount and rate, which are local variables Call to addInterest()

Tracing the Bank Interest Code Third, when control is passed to addInterest(), the formal parameters of (balance and rate) are set to the actual parameters of (amount and rate) def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def main(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount) main() Control passes to addInterest() balance = amount = 1000 rate = rate = 0.05

Tracing the Bank Interest Code Even though the parameter rate appears in both main() and addInterest(), they are two separate variables because of scope def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def main(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount) main() Even though rate exists in both main() and addInterest(), they are in two separate scopes

Scope In other words, the formal parameters of a function only receive the values of the actual parameters The function does not have access to the original variable in main()

Updating Bank Interest Example

New Bank Interest Code def addInterest(balance, rate): newBalance = balance * (1 + rate) return newBalance def main(): amount = 1000 rate = 0.05 amount = addInterest(amount, rate) print(amount) main()

These are the only parts we changed New Bank Interest Code def addInterest(balance, rate): newBalance = balance * (1 + rate) return newBalance def main(): amount = 1000 rate = 0.05 amount = addInterest(amount, rate) print(amount) main() These are the only parts we changed

Announcements HW 4 is out on Blackboard now All assignments will be available only on Blackboard until after the due date Complete the Academic Integrity Quiz to see it Due by Friday (March 3rd) at 8:59:59 PM Project 1 will come out this weekend Read it closely, but do not start on it yet! We will discuss the (required) design in class