When a function returns a value, it gives back a result to the caller using the return
statement. This is useful when you want to use the result later or assign it to a variable.
जब कोई function value return करता है, तो वह return
statement का उपयोग करके result वापस देता है। यह तब उपयोगी होता है जब आप उस result को आगे use करना चाहते हैं या किसी variable में store करना चाहते हैं।
def square(n):
return n * n
print("Square:", square(4))
Output:
Square: 16
def add(a, b):
return a + b
result = add(3, 7)
print("Sum:", result)
Output:
Sum: 10
def max_num(a, b):
return a if a > b else b
print("Max:", max_num(5, 10))
Output:
Max: 10
def full_name(fname, lname):
return fname + " " + lname
print(full_name("John", "Doe"))
Output:
John Doe
def average(a, b, c):
return (a + b + c) / 3
print("Average:", average(10, 20, 30))
Output:
Average: 20.0
def is_even(n):
return n % 2 == 0
print("Is 4 even?", is_even(4))
Output:
Is 4 even? True
def squares(n):
return [i*i for i in range(1, n+1)]
print(squares(5))
Output:
[1, 4, 9, 16, 25]
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print("Factorial:", factorial(5))
Output:
Factorial: 120
def is_palindrome(s):
return s == s[::-1]
print("Radar is palindrome:", is_palindrome("radar"))
Output:
Radar is palindrome: True
def get_grade(marks):
if marks >= 90:
return "A"
elif marks >= 75:
return "B"
elif marks >= 60:
return "C"
else:
return "F"
print("Grade:", get_grade(82))
Output:
Grade: B