22
loading...
This website collects cookies to deliver better user experience
User-defined functions are functions that you use to organize your code in the body.
def
keyword. Then we need to declare input parameters to the function. Finally we put a semicolon at the end. After that, we put the code to be run in an indent. This is done as shown-def fun(...,...,...):
...
...
...
def fun(a,b,c):
print(a+b+c)
fun(2,3,4)
fun(4,9,-1)
9
12
fun(2,3,4)
def fun(a,b,c):
print(a+b+c)
fun(4,9,-1)
Traceback (most recent call last):
File "main.py", line 1, in <module>
fun(2,3,4)
NameError: name 'fun' is not defined
def compare(a,b):
if(a<b):
print(a,"<",b)
elif(a>b):
print(a,">",b)
else:
print(a,"=",b)
compare(3,5)
compare(-2,-4)
compare(4,4)
3 < 5
-2 > -4
4 = 4
def prod(a):
temp=1
for i in a:
temp*=i
print(temp)
prod([1,2,3,4])
b=[5,7,4,-2]
prod(b)
24
-280