12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

22 Chapter 3. FunctionsInside the function, the arguments are assigned to variables called parameters. Here is an exampleof auser-defined function that takes an argument:def print_twice(bruce):print bruceprint bruceThis function assigns the argument to a parameter named bruce. When the function is called, itprintsthe value ofthe parameter (whatever itis)twice.This function works withany value that can beprinted.>>> print_twice('Spam')SpamSpam>>> print_twice(17)1717>>> print_twice(math.pi)3.141592653593.14159265359The same rules of composition that apply to built-in functions also apply to user-defined functions,sowecan use any kind of expression asan argument forprint_twice:>>> print_twice('Spam '*4)Spam Spam Spam SpamSpam Spam Spam Spam>>> print_twice(math.cos(math.pi))-1.0-1.0The argument is evaluated before the function is called, so in the examples the expressions'Spam '*4andmath.cos(math.pi)areonly evaluated once.You can alsouse avariable as an argument:>>> michael = 'Eric, the half a bee.'>>> print_twice(michael)Eric, the half a bee.Eric, the half a bee.Thenameofthevariablewepassasanargument (michael)hasnothing todowiththenameoftheparameter (bruce). It doesn’t matter what the value was called back home (in the caller); here inprint_twice,we call everybodybruce.3.9 Variablesand parametersarelocalWhen you create a variable inside a function, it is local, which means that it only exists inside thefunction. For example:def cat_twice(part1, part2):cat = part1 + part2print_twice(cat)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!