Functions
repeated step can be executed with functions
def name(argument):
Statement..
Statement..
def-define function
def functionname(contains a list of values passed to the function)
function body must be indented
this is executed each time the function is called
return values
Function has bulid-in function and user-defined functions
Eg-Calculator prgm
num1=float(input('Enter value for num1 '))
num2=float(input('Enter value for num2 '))
choice=input('Operation to be done:add/sub/mul/div/mod ')
def add(num1,num2):
add=num1+num2
print (add)
def sub(num1,num2):
sub=num1-num2
print(sub)
def mul(num1,num2):
mul=num1*num2
print(mul)
def div(num1,num2):
div=num1//num2
print(div)
if choice=='add':
add=num1+num2
print(add)
elif choice=='sub':
sub=num1-num2
print(sub)
elif choice=='mul':
mul=num1*num2
print(mul)
elif choice=='div':
div=num1//num2
print(div)
else:
print('option not available')
The above is the detailed content of Python Functions 4. For more information, please follow other related articles on the PHP Chinese website!