2. Syntax to define User Defined Functions:
def Function_name (parameters or arguments):
[statements……]
…………………………….
…………………………….
Its keyword and
used to define the
function in python
Statements inside the def begin after four
spaces, this is called Indentation.
These parameters
are optional
3. Example To define User Defined Functions:
def myname():
print(“JAMES”)
This is function definition
To call this function in a program
myname() This is function calling
----Output-----
JAMES
Full python code:
def myname():
print(“JAMES”)
myname()
4. Example to define: User Defined Functions:
Above shown is a function definition that consists of the following components.
Keyword def that marks the start of the function header.
A function name to uniquely identify the function. Function naming follows the same
rules of writing identifiers in Python.
Parameters (arguments) through which we pass values to a function. They are
optional.
A colon (:) to mark the end of the function header.
One or more valid python statements that make up the function body. Statements
must have the same indentation level (usually 4 spaces).
An optional return statement to return a value from the function.
5. Keyword def that marks the start of the
function header.
def
Myprog.py
A function name to uniquely identify the
function. Function naming follows the same
rules of writing identifiers in Python.
myfunction()
Parameters (arguments) through which we
pass values to a function. They are optional.
A colon (:) to mark the end of the function
header.
:
One or more valid python statements that
make up the function body. Statements must
have the same indentation level (usually 4
spaces).
statement1
statement2
statement3
myfunction()
When we execute the program Myprog.py
then myfunction() called
It jump inside the function at the top
[header] and read all the statements inside
the function called
6. Types of User Defined Functions:
Default function
Parameterized functions
Function with parameters and Return type
7. Default function
Function without any parameter or parameters and without return type.
Syntax:
def function_name():
……..statements……
……..statements……
function_name()
8. Example:To display the message “Hello World” five time without loop.
def mymsg():
print(“Hello World”)
mymsg()
mymsg()
mymsg()
mymsg()
mymsg()
----OUTPUT------
Hello World
Hello World
Hello World
Hello World
Hello World
9. Example:To display the message “Hello World” five time without loop.
Full python code:
def mymsg():
print(“Hello World”)
mymsg()
mymsg()
mymsg()
mymsg()
mymsg()
But if you do want to use to write so many
time the function name , but want to print the
message “Hello World” 15 or 20 or 40 times
then use loop
def mymsg():
print(“Hello World”)
for x in range(1,31):
mymsg()
10. Example: To define function add() to accept two number and display sum.
def add():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
sum=x + y
print(“x+y=“,sum)
add()
---Output-----
Enter value of x: 10
Enter value of y: 20
x+y=30
Declaration and definition part
Calling function
11. Example: To define function add() to accept two number and display sum.
def add():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
sum=x + y
print(“x+y=“,sum)
add()
---Output-----
Enter value of x: 10
Enter value of y: 20
x+y=30
Declaration and definition part
Calling function
12. Example: To define function sub() to accept two number and display subtraction.
def sub():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
s=x - y
print(“x-y=“,s)
sub()
---Output-----
Enter value of x: 20
Enter value of y: 10
x-y=10
Declaration and definition part
Calling function
13. Example: To define function mult() to accept two number and display Multiply.
def mult():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
s=x * y
print(“x*y=“,s)
mult()
---Output-----
Enter value of x: 20
Enter value of y: 10
x*y=200
Declaration and definition part
Calling function
14. Example: To define function div() to accept two number and display Division.
def div():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
s=x / y
print(“x/y=“,s)
div()
---Output-----
Enter value of x: 15
Enter value of y: 2
x/y=7.5
Declaration and definition part
Calling function
15. Now how we combine all the function in one program and ask user to enter
the choice and according to choice add, subtract, multiply and divide.
----Main Menu--------
1- Addition
2- Subtraction
3- Multiplication
4- Division
5- To exit from Program
Please enter the choice
Program should work like the output given below on the basis of users choice:
def add():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x+y=“,x+y)
def sub():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x-y=“,x-y)
def mult():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x*y=“,x*y)
def div():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x//y=“,x//y)
16. def add():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x+y=",x+y)
def sub():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x-y=",x-y)
def mul():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x*y=",x*y)
def div():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x//y=",x//y)
ch=0
while True:
print("1- Addition")
print("2- Subtraction")
print("3- Multiplication")
print("4- Division")
print("5- To exit from Program")
ch=int(input("Please enter the choice"))
if ch==1:
add()
elif ch==2:
sub()
elif ch==3:
mul()
elif ch==4:
div()
else:
break
Full python code for the previous question:
17. Example:To create a function checkevenodd() , that accept the number
and check its an even or odd
def checkevenodd():
no=int(input(“Enter the value:”))
if no%2 == 0:
print(“Its an even no”)
else:
print(“Its an odd number”)
----Output----
Enter the value: 12
Its an even no
----Output----
Enter the value: 17
Its an Odd no
18. Example:To display number from 1 to 20 using function display1to20()
Example:To display number table of 3 using function display3()
Example:To display even number between 2 to 40 using function
displayeven()
Example:Program to print triangle using loops and display it through
function displaytri()
1
12
123
1234
12345
19. Example:To display number from 1 to 20 using function display1to20()
def display1to20():
for x in range(1,21):
print(x,sep=“,”,end=“”)
display1to20()
---Output----
1,2,3,4,5,6……….,20
20. Example:To display number table of 3 using function display3()
def display3():
for x in range(1,11):
print(x*3)
display3()
---Output----
3
6
9
12
15
.
.
.
30
21. Example:To display number from 1 to 20 using function display1to20()
Example:To display number table of 3 using function display3()
Example:To display even number between 2 to 40 using function
displayeven()
Example:Program to print triangle using loops and display it through
function displaytri()
1
12
123
1234
12345
22. Example:To display number from 1 to 20 using function display1to20()
Example:To display number table of 3 using function display3()
Example:To display even number between 2 to 40 using function
displayeven()
Example:Program to print triangle using loops and display it through
function displaytri()
1
12
123
1234
12345