Building function
- We create a new function using the def keyword follow by optional parameters in parentheses
- We indent the body of the function
def print_lyrics():
print("hear is a little song i wrote")
print('please listen carefullly note to note')
print('don''t worry be happy')
The code inside the def is not calling ,it’s remembering it’s figuring out and it will ready when we call it
x = 5
#if int(x) == 5:
print('hello')
def print_lyrics():
print("hear is a little song i wrote")
print('please listen carefullly note to note')
print('don''t worry be happy')
print('yo')
#print_lyrics()
x = x+4
print(x)
Output
hello
yo
9
in this case it will not print out the function print_lyrics be cuz we just define it but doesn’t mean we invoke it or call it
x = 5
print('hello')
def print_lyrics():
print("hear is a little song i wrote")
print('please listen carefullly note to note')
print('don''t worry be happy')
print('yo')
print_lyrics()
x = x+4
print(x)
Output
hello
yo
hear is a little song i wrote
please listen carefullly note to note
dont worry be happy
9
we invoke it using its name
- Once we defined a function we can all or invoke it as many time as we like
- this is the store and reuse pattern
>>> def great(lang):
if lang == 'es':
print('hola')
elif lang == 'fr':
print('hello')
>>> great('es')
hola
>>> great('fr')
hello
this is written on shell
this code like a translator
Return Values
Questions:
-
What will the following Python program print out?:
def fred(): print("Zap") def jane(): print("ABC") jane() fred() jane() -
Ans
ABC Zap ABC
# Real Exercise
-
1 make function using def
def computepay(hours,rate): print ('in computepay') sh = input('Enter Hours: ') sr = input ('Enter Rate:') fh = float (sh) fr = float(sr) if fh > 40: reg = fr*fh otp = (fh - 40.0) *(fr * 0.5) xp = reg +otp else: xp = fh * fr print ('pay:',xp)### Output
Enter Hours: 40 Enter Rate:10 pay: 400.0in this case it doesn’t run the function computepay because we just define the function but we didn’t invoke it so it doesn’t show up
def computepay(hours,rate): print ('in computepay') sh = input('Enter Hours: ') sr = input ('Enter Rate:') fh = float (sh) fr = float(sr) computepay(fh,fr) if fh > 40: reg = fr*fh otp = (fh - 40.0) *(fr * 0.5) xp = reg +otp else: xp = fh * fr print ('pay:',xp)### Output
Enter Hours: 40 Enter Rate:10 in computepay pay: 400.0it shown up when we invoke it
-
2 put something more into a function
def computepay(hours,rate): print ('in computepay', hours,rate) if fh > 40: reg = hours*rate otp = (hours - 40.0) *(rate * 0.5) pay = reg +otp else: pay = hours * rate print('Returning',pay) return pay sh = input('Enter Hours: ') sr = input ('Enter Rate:') fh = float (sh) fr = float(sr) xp = computepay(fh,fr) print ('pay:',xp)Now we put the function , we move the if and else to def then we change something in that function but it still work the same just put some step of calculating into the function
### Output
Enter Hours: 40 Enter Rate:10 in computepay 40.0 10.0 Returning 400.0 pay: 400.0
-