User Defined Functions
M3-R5.1 · Chapter 6: Functions · 5 min read
User Defined Functions: The functions those are defined by the user are called user defined functions and These functions are created by using the
The syntax to define a function is:
Function Body: Statements that define what the function does. And the body can contain one or more statements
Return value : Optional Parameter: Optional
Q: Every function must have a return value-> Ans : -> False
Point to remember…………….
- Keyword def marks the beginning(start) of the functions header
- Function Name to uniquely identify it. And must follow the naming rules same as for identifiers
- Functions may or may not contain arguments.
- A colon(: ) to marks the end of function header
- Functions can take one or more statements to perform a specific task. And statements must have same indentation level
- An optional return statement to return a value from the function.
- Function must be called /invoked to execute its code.
- All statements within the same block must have same indentation
- Parameters (arguments) through which we pass values to a function. They are optional.
Calling the function:
- Once we have defined a function, we can call it from another function, program or even the Python prompt. To call a function we simply type the function name with appropriate parameters.
Syntax: function-name(parameter) # parameter is optional
Example: Add (10,20) #here 10 and 20 is a parameter for add () function
The return statement: The return statement is used to exit a function and go back to the place from where it was called.
There are two types of functions according to return statement:
a. Function returning some value (non-void function)
b. Function not returning any value (void function)
Example:
|
def Display(): print(“Never Give Up !!!”) Display() |
def Display(): print(“Good Morning…..”) print(“***************”) Display() |
|
def Display1(): print("Good Morning…..") print("***************") def Display2(): print("Good Afternoon…..") print("###############") def Display3(): print("Good Evening…..") print("-------------------") Display1() Display2() Display3() |
Q: Write a program to add two number using function def Add(): num1=int(input("Enter First Number:")) num2=int(input("Enter Second Number")) sum=num1+num2 print("Addition =",sum) Add() OUTPUT: Enter First Number: 14 Enter Second Number: 25 Addition=39 |
|
Q: Write a program to calculate sinple Interest using function def simple_interest(): p=int(input('Enter The principal:')) t=int(input('Enter The time:')) r=int(input('Enter The rate:')) si = (p * t * r)/100 print('The Simple Interest is', si) simple_interest() |
def simple_interest(p,t,r): print('The principal is', p) print('The time period is', t) print('The rate of interest is',r) si = (p * t * r)/100 print('The Simple Interest is', si) simple_interest(1000, 2, 3) |
|
def simple_interest(p,t,r): print('The principal is', p) print('The time period is', t) print('The rate of interest is',r) si = (p * t * r)/100 print('The Simple Interest is', si) P = int(input("Enter the principal amount :")) T = int(input("Enter the time period :")) R = int(input("Enter the rate of interest :")) simple_interest(P,T,R) |
Q: Write a program to check odd or even number using function def odd(): num = int(input("Enter a number: ")) if (num % 2) == 0: print("{0} is Even number".format(num)) else: print("{0} is Odd number".format(num)) odd() |
|
Q: Write a program to print the factorial of a number using function def fact(): num = int(input("Enter a number: ")) factorial = 1 if num < 0: print(" Factorial does not exist for -ve numbers") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1,num + 1): factorial = factorial*i print("The factorial of",num,"is",factorial) fact() |
#Python program to print the power of 1st number to the 2nd number using function def power(x, n): result = 1 for i in range(n): result=result*x return result a = int(input("Enter number ")) b = int(input("Raise to power ")) pw = power(a, b) print(a, "raise to power", b, "is", pw) |