DEV Community

Arokya Naresh
Arokya Naresh

Posted on

Python Functions 17.07.2024

Functions

repeated step can be executed with functions

Funtion Structure:

def name(argument):
Statement..
Statement..

return value

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')

Top comments (0)