More Functions CS303E: Elements of Computers and Programming.

Slides:



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

Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Chapter 6: User-Defined Functions I
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
More Functions with Return Values CS303E: Elements of Computers and Programming.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
Python Functions : chapter 3
Loops and Simple Functions CS303E: Elements of Computers and Programming.
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.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
COMP Loop Statements Yi Hong May 21, 2015.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Structured Programming (COIT 29222) Term 2, 2009, Final Exam Date And Time  As advised by the university Open/Closed Book (3 hours)  Structured Programming,
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
CS1010: Programming Methodology
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Control flow Ruth Anderson UW CSE 160 Winter
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Nested Loops CS303E: Elements of Computers and Programming.
CMSC201 Computer Science I for Majors Lecture 13 – Functions
Chapter 6: User-Defined Functions I
User-Defined Functions
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Ruth Anderson UW CSE 140 Winter 2014
Michael Ernst UW CSE 140 Winter 2013
Chapter 5 Methods.
Let’s all Repeat Together
Chapter 6: User-Defined Functions I
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
CSE 231 Lab 4.
CISC101 Reminders Assignment 3 due today.
Michael Ernst UW CSE 190p Summer 2012
Control flow: Loops UW CSE 160.
Presentation transcript:

More Functions CS303E: Elements of Computers and Programming

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. - M. Fowler, "Refactoring: Improving the Design of Existing Code"

Functions: Review Statements grouped under a special name that are executed together Statements grouped under a special name that are executed together Advantages Advantages –Code reuse Type once, use again and again! Type once, use again and again! Easier to maintain (update, fix mistakes) Easier to maintain (update, fix mistakes) –Code readability

Functions: Syntax Definition Syntax: def functionName(): statementstatement… Call Syntax: functionName()

Functions: Examples Write a program that prints the happy birthday song. def main(): # give a name to statements print ‘Happy birthday to you!’ print ‘Happy birthday to you!’ print ‘Happy birthday, dear Elvis,’ print ‘Happy birthday, dear Elvis,’ print ‘Happy birthday to you!’ print ‘Happy birthday to you!’ main() # actually execute the main function

What’s Wrong with It? Repeated code Repeated code –1 st, 2 nd and 4 th lines are identical. –Let’s write a function that prints that repeated line

Revised Happy Birthday Song def main(): singHappy() singHappy() print ‘Happy birthday, dear Elvis,’ print ‘Happy birthday, dear Elvis,’ singHappy() singHappy() def singHappy(): print ‘Happy birthday, to you!’ print ‘Happy birthday, to you!’ main() # execute main function

Function Variables: Scope A variable’s scope is that part of the program where it may be accessed: A variable’s scope is that part of the program where it may be accessed: –After its definition (initialization) Local variables are those that are created inside a function Local variables are those that are created inside a function –All the variables we have created so far have been local to main() A local variable’s scope is the function where it was created A local variable’s scope is the function where it was created –Cannot be accessed by another function

Variable Scope: Example def main(): wt=0 wt=0earth()jupiter() print wt print wt def earth(): wt=45 print “Earth Weight:”, wt print “Earth Weight:”, wt def jupiter() wt=40 wt=40 wt=wt*2.364 wt=wt*2.364 print “Jupiter Weight:”, wt print “Jupiter Weight:”, wt

Question: Variable Scope Variable x is defined in main(). Where can it be accessed? A. In main() before its definition B. In main() after its definition C. In main() and every other function in the file D. Everywhere

Functions with Parameters Parameters are a way to pass information into a function Parameters are a way to pass information into a function –Allows greater re-use range() has parameters range() has parameters –The value(s) that tell it what sequence to generate Parameters are local to a function, so their scope is their function Parameters are local to a function, so their scope is their function

Functions with Parameters: Syntax def functionName(param1, param2): statementstatement… Function parameters: how information is given to the function These may be empty

To call that function: functionName(arg1, arg2) When a function is called: param1=arg1param2=arg2 statements in the body are executed Functions with Parameters: Syntax Recall: def functionName(param1, param2):

Functions with Parameters: Examples Sing happy birthday to anyone, rather than just Elvis. Solution: Write a function that uses a parameter to produce the third line of the song.

Happy Birthday, take 3 def main(): def singIt(person): singIt(“Elvis”) singHappy() singIt(“Elvis”) singHappy() print # blank line singHappy() print # blank line singHappy() singIt(“Nancy”) print ‘Happy birthday, dear’, \ singIt(“Nancy”) print ‘Happy birthday, dear’, \ person + ‘,’ person + ‘,’ singHappy() singHappy() def singHappy(): print ‘Happy birthday to you!’ print ‘Happy birthday to you!’ main() # execute main

Happy birthday: Output Happy birthday to you! Happy birthday dear Elvis, Happy birthday to you! Happy birthday dear Nancy, Happy birthday to you!

Question: Functions with Parameters Consider this snippet of code: … x=25 x=25 printNum(25) printNum(25) … def printNum(num): print num print num When printNum is executed from this snippet, what is the output? (Assume printNum() is defined correctly) A. -1 B. 0 C. 25 D. No value

Aside: Available Functions abs() abs() int() int() range() range() input() input() raw_input() raw_input() round() round() type() type() If you import math: math.sqrt() math.sqrt() math.ceil() math.ceil() math.floor() math.floor() math.factorial() math.factorial() math.log() math.log() math.pow() math.pow() math.sum() math.sum()

Exercise Write a function make10 that, given two ints a and b, prints True if one of them is 10 or if their sum is 10. Write a main() function that uses a for loop to call make10 for the values of a from 1 to 10. b has the value 5 for each call.

Reminders Exam is NEXT WEEK Exam is NEXT WEEK Review Monday Review Monday –Come prepared to ask questions (or send me !) –Sample Exam posted today (solns Monday) Assignment 5 released today, due THURSDAY 3/1 Assignment 5 released today, due THURSDAY 3/1 –But doing it before the exam will help you!