Example :function program in Python is used to show the default parameter.
def msg(name="World"):
    return f"Hello, {name}!"

print(msg())        # Output: Hello, World!  Default Parameter.
print(msg("India")) # Output: Hello, India!

NB: Here, the default value is 'World'. When the parameter value is not supplied to the function then default value works.
Example : A user-defined function(UDF) program in Python is used to show the addition result of two user-accepted values.
# No Argument No Return UDF
 
x=int(input("Enter first value ="))
y=int(input("Enter second value ="))
def Addition():
    z=x+y
    print("The addition result is = ",z)    
Addition()

Output:
Enter first value =10
Enter second value =20
The addition result is =  30

------------  OR  ------------

def Addition():
    x=int(input("Enter first value = "))
    y=int(input("Enter second value = "))
    
    z=x+y
    print("The addition result is = ",z)
    
Addition()

Output:
Enter first value = 10
Enter second value = 20
The addition result is = 30

------------  OR  ------------

def Addition():
    x=50
    y=75
    
    z=x+y
    print("The addition result is = ",z)
    
Addition()

Output:
The addition result is =  125

# With Argument No Return UDF

def Addition(x,y):
    a=x 
    b=y
    c=a+b
    print("The addition result is = ",c)    
Addition(10,20)

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    print("The addition result is = ",z)    
Addition(10,20)

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    print("The addition result is = ",z)
    
m=int(input("Enter first value = "))
n=int(input("Enter second value = "))
Addition(m,n)

Output:
Enter first value = 100
Enter second value = 200
The addition result is =  300

# No Argument With Return UDF

def Addition():
    x=20
    y=50
    
    z=x+y
    return z
# print("The addition result is = ", Addition())

k=Addition()
print("The addition result is = ", k)

Output:
The addition result is =  70

------------  OR  ------------

def Addition():
    x=int(input("Enter first value = "))
    y=int(input("Enter second value = "))
    
    z=x+y
    return z
# print("The addition result is = ", Addition())

k=Addition()
print("The addition result is = ", k)

Output:
Enter first value = 20
Enter second value = 30
The addition result is =  50

# With Argument With Return UDF

def Addition(x,y):
    z=x+y
    return z
k=Addition(10,20)
print("The addition result is = ",k)

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    return z
Addition(10,20)
print("The addition result is = ", Addition(10,20))

Output: The addition result is =  30

------------  OR  ------------

def Addition(x,y):
    z=x+y
    return z
    
m=int(input("Enter first value = "))
n=int(input("Enter second value = "))
print("The addition result is = ",Addition(m,n))

Enter first value = 20
Enter second value = 30
The addition result is =  50
Example : A UDF program in Python to show the accepted single value is Odd or Even.
# No Argument No Return

def OddEven():
    x=int(input("Enter your value = "))
    if x%2==0:
        print("Value is Even")
    else:
        print("Value is Odd")
    
OddEven()

Output:
Enter your value = 20
Value is Even

# No Argument With Return

def OddEven():
    x=int(input("Enter first value = "))
    if x%2==0:
        return "Value is Even"
    else:
        return "Value is Odd"
    
result = OddEven()
print(result)

Output:
Enter first value = 41
Value is Odd

# With Argument No Return

def OddEven(x):
    if x%2==0:
        print ("Value is Even")
    else:
        print ("Value is Odd")
        
y=int(input("Enter your value = "))    
OddEven(y)

# With Argument With Return

def OddEven(x):
    if x%2==0:
        return "Value is Even"
    else:
        return "Value is Odd"
        
y=int(input("Enter your value = "))    
result = OddEven(y)
print(result)
Example : A UDF program in Python is used to show the table’s values of the accepted single value.
# No Argument No Return

def Addition():
    x=int(input("Enter table value = "))
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    
Addition()

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

-------------  OR  -------------

def Addition():
    x=int(input("Enter table value = "))
    k=x
    while x<=k*10:
        print(x,end=" ")
        x=x+k
    
Addition()

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

# No Argument With Return

def Addition():
    x=int(input("Enter table value = "))
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    return i
result=Addition()
print (result)

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

# With Argument No Return

def Addition(x):
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    
y=int(input("Enter table value = "))
Addition(y)

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

# With Argument With Return

def Addition(x):
    k=x
    for i in range(x,k*10+1,k):
        print(i,end=" ")
    return i
    
y=int(input("Enter table value = "))
result=Addition(y)
print (result)

Output:
Enter table value = 7
7 14 21 28 35 42 49 56 63 70

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.