上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Functions,Informally ●( Our new main program: def main(): sing ("Fred") print() sing ("Lucy") Gives us this 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! 16
16 Functions, Informally • Our new main program: def main(): sing("Fred") print() sing("Lucy") • Gives us this 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 Example def date split(ss):#return a list([mm,dd,yy]) 0年84t84 def is valid date(11) #return True of False def main () date ss raw input ("..." date 11 date split(date ss) if is valide date(date 11): print "a valid date." else: print "not a valid date." main ( 17
Example def date_split(ss): #return a list([mm,dd,yy]) ……… def is _valid_date(ll) #return True of False ……… def main(): date_ss = raw_input(“…”) date_ll = date_split(date_ss) if is_valide_date(date_ll): print “a valid date.” else: print “not a valid date.” main() 17
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Create new functions A function definition looks like this: def <name>(<formal-parameters>): <body> The name of the function must be an identifier Parameters(Formal-parameters)is a possibly empty list of variable names 18
18 Create new functions • A function definition looks like this: def <name>(<formal-parameters>): <body> • The name of the function must be an identifier • Parameters(Formal-parameters) is a possibly empty list of variable names
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Parameters and arguments Inside the function,the values that are passed get assigned to variable called parameters -def sing(person) The values that control how the function does its job called arguments (real values) -sing ("bob") 19
Parameters and arguments • Inside the function, the values that are passed get assigned to variable called parameters – def sing(person) • The values that control how the function does its job called arguments (real values) – Sing(“bob”) 19
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Return Values Return a value output of the function,but not screen output of the program -Different from print ·Example: def func add(a,b): c=a+b return c >>>res func add(7,8) Return more than one value: return a,b 20
Return Values • Return a value = output of the function, but not screen output of the program – Different from print • Example: def func_add(a,b): c=a+b return c >>>res = func_add(7,8) • Return more than one value: – return a,b 20