上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally Creating this function saved us a lot of typing! e What if it's Lucy's birthday?We could write a new singLucy function! def singLucy(): happy ( happy ( print"Happy birthday,dear Lucy..." happy ( 11
11 Functions, Informally • Creating this function saved us a lot of typing! • What if it’s Lucy’s birthday? We could write a new singLucy function! def singLucy(): happy() happy() print"Happy birthday, dear Lucy..." happy()
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally We could write a main program to sing to both Lucy and Fred def main(): singFred() print() singLucy ( This gives us this new output >>main() Happy birthday to you! Happy birthday to you! Happy birthday,dear Fred.. Happy birthday to you! Happy birthday to you! Happy birthday to you! Happy birthday,dear Lucy... Happy birthday to you! 12
12 Functions, Informally • We could write a main program to sing to both Lucy and Fred def main(): singFred() print() singLucy() • This gives us this new output >>> main() Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred.. Happy birthday to you! Happy birthday to you! Happy birthday to you! Happy birthday, dear Lucy... Happy birthday to you!
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally This is working great!But...there's still a lot of code duplication. The only difference between singFred and singLucy is the name in the third print statement. These two routines could be collapsed together by using a parameter. 13
13 Functions, Informally • This is working great! But… there’s still a lot of code duplication. • The only difference between singFred and singLucy is the name in the third print statement. • These two routines could be collapsed together by using a parameter
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally The generic function sing def sing(person): happy ( happy ( print"Happy birthday,dear",person " happy ( This function uses a parameter named person. A paramater is a variable that is initialized when the function is called. 14
14 Functions, Informally • The generic function sing def sing(person): happy() happy() print"Happy birthday, dear", person + ".“ happy() • This function uses a parameter named person. A paramater is a variable that is initialized when the function is called
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally 。Our new output- >>sing("Fred") Happy birthday to you! Happy birthday to you! Happy birthday,dear Fred. Happy birthday to you! We can put together a new main program! 15
15 Functions, Informally • Our new output – >>> sing("Fred") Happy birthday to you! Happy birthday to you! Happy birthday, dear Fred. Happy birthday to you! • We can put together a new main program!