上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally The part of the program that creates a function is called a function definition. -def functionName(): When the function is used in a program,we say the definition is called or invoked. functionName() 6
6 Functions, Informally • The part of the program that creates a function is called a function definition. – def functionName(): • When the function is used in a program, we say the definition is called or invoked. – functionName()
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY The Function of Functions e So far,we've seen three different types of functions: - Our programs comprise a single function called main(). Built-in Python functions(type() -Functions from the standard libraries (string.split()) 7
7 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 (type()) – Functions from the standard libraries (string.split())
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally Happy Birthday lyrics... def main(): print"Happy birthday to you! print"Happy birthday to you!" print"Happy birthday,dear Fred..." print"Happy birthday to you!" Gives us this... >>main() Happy birthday to you! Happy birthday to you! Happy birthday,dear Fred... Happy birthday to you! 8
8 Functions, Informally • Happy Birthday lyrics… def main(): print"Happy birthday to you!" print"Happy birthday to you!" print"Happy birthday, dear Fred..." print"Happy birthday to you!" • Gives us this… >>> main() Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred... Happy birthday to you!
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally There's some duplicated code in the program! (print "Happy birthday to you!" We can define a function to print out this line: def happy(): print"Happy birthday to you!" With this function,we can rewrite our program. 9
9 Functions, Informally • There’s some duplicated code in the program! (print "Happy birthday to you!") • We can define a function to print out this line: def happy(): print"Happy birthday to you!" • With this function, we can rewrite our program
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally 。The new program- def singFred(): happy ( happy ( print"Happy birthday,dear Fred..." happy ( Gives us this output- >>singFred() Happy birthday to you! Happy birthday to you! Happy birthday,dear Fred... Happy birthday to you! 10
10 Functions, Informally • The new program – def singFred(): happy() happy() print"Happy birthday, dear Fred..." happy() • Gives us this output – >>> singFred() Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred... Happy birthday to you!