def sum(a, b):   # parameters
    s = a + b
    return s
 
# function call
ans = sum(3, 4)   # arguments
 
print(ans)

output

7

Note

  • Parameters → variables in function definition
  • Arguments → values passed during function call

Question

Define a function in Python to calculate average

def avg(a, b, c):
    return (a + b + c) / 3
 
result = avg(10, 20, 30)
print("average =", result)

output

average = 20.0
  • default parameters
def sum(a, b=1):
    return a + b
 
print(sum(5))

output

6

Note

  • Default parameter → already has a value
  • Non-default must come before default
  • ✔ Correct: def sum(a, b=1)
  • ❌ Wrong: def sum(a=1, b)

Question

Write a function in Python to print factorial of n

def cal_factorial(n):
    fact = 1
    for i in range(1, n + 1):
        fact *= i
    return fact
 
n = int(input("enter n: "))
print(cal_factorial(n))

output

enter n: 5
120

Note

  • Factorial → n! = n × (n-1) × ... × 1
  • 0! = 1