User-Defined Functions
There are 2 types of functions in Python Program:
1. Pre-defined or Built-in or Library
2. User-defined functions
Definition of any function contains 2 parts:
1. Header: consists of identifications of function
2. body: contains executable part of function to be executed when it is invoked.
Header of the function have 3 parts:
1. The ‘def’ keyword
2. name of the function
3. parameters or arguments (if any)
Return value:
Value to be returned back by the calling function after completion of its job to its caller. A function can be defined to return or to not return a value.
When we want to define a function to return a value, then ‘return’ keyword to be used. The return statement must be the last statement of function body.
syntax of return statement:
return
return val
return var
return expr
when we want to define a function to not return any value, we can close function definition without return statement or with ‘return’ to indicate end of function.
#definition of function not returning any
def NoRet():
print("I am not returning any")
return
#function definition that returns a value
def RetVal():
return 5
#function definition that returns a value of a variable
def RetVar():
num=10
return num
#function definition that returns
#a evaluation result of an expression
def RetExp():
return 5+2
#main program
NoRet()
RetVal()
#print("res=",res)
res=RetVar()
print("res=",res)
res=RetExp()
print("res=",res)
Practice Questions:
1. Write a Python function getStr() that returns the string “Hello, Python!” and print the returned value from caller function
2. Write a function named getNumber() that returns the integer 42. Call the function and store the result in a variable, then print the variable.
3. Write a function is_even() that returns True if a hardcoded number inside the function is even, otherwise returns False. Print the function’s return value using its caller.
4. Write a Python function return_none() that does not explicitly return any value. Call the function and print its return value to verify its return type.
5. Write a function pi_value() that returns the mathematical value of Pi (3.14159). Call the function and store the result in a variable, then print it.
6. Write a function get_person() that returns two values: a hardcoded name (string) and an age (integer). Store the returned values in separate variables and print them.
7. Write a function get_marks() that returns three hardcoded marks (integers). Call the function and store the values in a tuple. Print the tuple.
8. Write a function get_status() that returns True and “Success” as a tuple. Unpack the returned values and print them separately.
9. Write a function is_positive() that returns “Positive” if a hardcoded number is greater than zero, “Negative” if less than zero, and “Zero” otherwise. Print the return value.
10. Write a function is_vowel() that returns True if a hardcoded character is a vowel and False otherwise. Call the function and print the result.
11. Write a function square() that returns the square of a hardcoded number. Print the function’s return value.
12. Write a function calculate_area() that returns the area of a rectangle with hardcoded width and height values. Print the returned area.
13. Write a function convert_to_fahrenheit() that returns the Fahrenheit equivalent of a hardcoded Celsius temperature. Print the function’s return value.
14. Write a function count_down() that prints numbers from 5 to 1 using a loop and then returns “Done!”. Print the function’s return value.
15. Write a function sum_first_n_numbers() that returns the sum of the first 10 natural numbers. Call the function and print the result.
16. Write a function outer_function() that returns another function inner_function(). Call outer_function() and execute the returned function.
17. Write a function get_multiplier() that returns a function that multiplies a given number by 3. Call get_multiplier(), store the returned function in a variable, and use it to multiply 5. Print the result.