USER DEFINE FUNCTIONS IN PYTHON

1,376 views 22 slides Sep 24, 2020
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

syntax of user define function, how to define default functions in python .


Slide Content

FUNCTION IN PYTHON CLASS: XII COMPUTER SCIENCE -083 USER DEFINED FUNCTIONS PART-3

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

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 ()

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.

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. m yfunction () 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 m yfunction () When we execute the program Myprog.py then myfunction () called It jump inside the function at the top [header] and read all the statements insid e the function called

Types of User Defined Functions: Default function Parameterized functions Function with parameters and Return type

Default function Function without any parameter or parameters and without return type. Syntax: def function_name (): ……..statements…… …….. statements…… function_name ()

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

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 ()

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

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

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

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

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

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)

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:

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

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 123 4 12345

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

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

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 123 4 12345

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 123 4 12345