20
More Functions CS303E: Elements of Computers and Programming

More Functions CS303E: Elements of Computers and Programming

Embed Size (px)

Citation preview

Page 1: More Functions CS303E: Elements of Computers and Programming

More Functions

CS303E: Elements of Computers and Programming

Page 2: 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"

Page 3: More Functions CS303E: Elements of Computers and Programming

Functions:Review Statements grouped under a

special name that are executed together

Advantages– Code reuse

Type once, use again and again! Easier to maintain (update, fix mistakes)

– Code readability

Page 4: More Functions CS303E: Elements of Computers and Programming

Functions:SyntaxDefinition Syntax:def functionName():statementstatement…

Call Syntax:functionName()

Page 5: More Functions CS303E: Elements of Computers and Programming

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 to you!’

main() # actually execute the main function

Page 6: More Functions CS303E: Elements of Computers and Programming

What’s Wrong with It?

Repeated code– 1st, 2nd and 4th lines are identical.– Let’s write a function that prints that

repeated line

Page 7: More Functions CS303E: Elements of Computers and Programming

Revised Happy Birthday Songdef main(): singHappy() singHappy() print ‘Happy birthday, dear Elvis,’ singHappy()

def singHappy(): print ‘Happy birthday, to you!’

main() # execute main function

Page 8: More Functions CS303E: Elements of Computers and Programming

Function Variables: Scope 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– 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– Cannot be accessed by another function

Page 9: More Functions CS303E: Elements of Computers and Programming

Variable Scope:Exampledef main(): wt=0earth()jupiter()

print wt

def earth():wt=45

print “Earth Weight:”, wt

def jupiter() wt=40 wt=wt*2.364 print “Jupiter Weight:”, wt

Page 10: More Functions CS303E: Elements of Computers and Programming

Question:Variable Scope

Variable x is defined in main(). Where can it be accessed?

A. In main() before its definitionB. In main() after its definitionC. In main() and every other function in

the fileD. Everywhere

Page 11: More Functions CS303E: Elements of Computers and Programming

Functions with Parameters Parameters are a way to pass

information into a function– Allows greater re-use

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

Page 12: More Functions CS303E: Elements of Computers and Programming

Functions with Parameters: Syntaxdef functionName(param1, param2):statementstatement…

Function parameters: how information is given to the function

These may be empty

Page 13: More Functions CS303E: Elements of Computers and Programming

To call that function:functionName(arg1, arg2)

When a function is called:param1=arg1param2=arg2statements in the body are

executed

Functions with Parameters: SyntaxRecall:def functionName(param1, param2):

Page 14: More Functions CS303E: Elements of Computers and Programming

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.

Page 15: More Functions CS303E: Elements of Computers and Programming

Happy Birthday, take 3

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

Page 16: More Functions CS303E: Elements of Computers and Programming

Happy birthday: OutputHappy birthday to you!Happy birthday to you!Happy birthday dear Elvis,Happy birthday to you!

Happy birthday to you!Happy birthday to you!Happy birthday dear Nancy,Happy birthday to you!

Page 17: More Functions CS303E: Elements of Computers and Programming

Question:Functions with Parameters

Consider this snippet of code:

… x=25 printNum(25) …

def printNum(num):

print num

When printNum is executed from this snippet, what is the output? (Assume printNum() is defined correctly)

A. -1B. 0C. 25D. No value

Page 18: More Functions CS303E: Elements of Computers and Programming

Aside:Available Functions abs() int() range() input() raw_input() round() type()

If you import math: math.sqrt() math.ceil() math.floor() math.factorial() math.log() math.pow() math.sum()

Page 19: More Functions CS303E: Elements of Computers and Programming

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.

Page 20: More Functions CS303E: Elements of Computers and Programming

Reminders

Exam is NEXT WEEK Review Monday

– Come prepared to ask questions (or send me email!)

– Sample Exam posted today (solns Monday)

Assignment 5 released today, due THURSDAY 3/1– But doing it before the exam will help

you!