SlideShare uma empresa Scribd logo
1 de 16
FUNCTION IN PYTHON
CLASS: XII
COMPUTER SCIENCE -083
USER DEFINED FUNCTIONS
PART-4
FUNCTIONS
with
PARAMETERS
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
Example To define User Defined Functions:
def myname(nm):
print(nm)
This is function definition
To call this function in a program
myname(‘WELCOME’) This is function calling
----Output-----
WELCOME
Full python code:
def myname(nm):
print(nm)
myname(‘WELCOME’)That nm is a parameter
Display the 4 names “JAMES”, “ Blake”, “Tom”, ”John” with the help
of one function and one print only.
def display1():
print(“JAMES”)
def display4():
print(“John”)
def display3():
print(“Tom”)
def display2():
print(“Blake”)
display1()
display2()
display3()
display4()
---Output-----
JAMES
John
Tom
Blake
So what problem we face , that is four name four functions
with different function name .Then we call all these
functions.
What we need here that one function name that use to
display all four names.
Display the 4 names “JAMES”, “ Blake”, “Tom”, ”John” with the help
of one function and one print only.
def display(nm):
display(‘JAMES’)
---Output-----
JAMES
John
Tom
Blake
print(nm)
display(‘Blake’)
display(‘Tom’)
display(‘John’)
We passes nm as a
parameter that store the
values passes from
display() at the bottom.
So WHAT is important part in parameterized function it is
the parameter pass inside the function header, and what is
the work of this parameter is to store the value pass from
the calling function.
Now again we take one more example to understand
parameterized functions.
In this we pass a number 5 as a parameter through a function name add()
and display the sum of 5 with itself.
def sum(n):
print(“sum=“,n+n)
sum(5)
When we execute the program the function sum() will call and
value 5 passes to a parameter no.
This is the original value and it is called actual parameter. Actual
parameters are values that are passed to a function when it is
invoked.Here 5 is a actual parameter
This is copied value for the function. So the function
sum() uses this value that comes from actual parameter
and this value known as formal parameter. Formal
parameters are the variables defined by the function
that receives values when the function is called. So
here n is a formal parameter
So now if we need to add to numbers means we need to pass two numbers
as a parameter and display there sum. Function name sum() with two
parameters.
def sum(a,b):
print(“sum=“,a+b)
sum(5,6)
a, b are formal parameter and store the copied value
passes through actual parameter sum(5,6)
----Output----
sum=11
Program to accept the two numbers from user and display there addition
through a function sum() with two parameters .
def sum(a,b):
print(“sum=“,a+b)
sum(x,y)
a, b are formal parameter and store the copied
value passes through actual parameter sum(x,y)
Where x value passes to a and y value to b
----Output----
Enter the value x: 7
Enter the value y: 3
sum=10
x=int(input(“Enter the value x:”))
y=int(input(“Enter the value y:”))
Program to accept the radius from user and display area of circle using
function circle() with radius as a parameter.
def circle(r):
print(“Area of circle=“,3.14*r*r)
circle(rad)
r is formal parameter and store the copied value
passes through actual parameter circle(rad)
Where rad value passes to r, so rad is actual
parameter and r is a formal parameter.
----Output----
Enter radius: 3
Area of circle=28.259
rad=int(input(“Enter radius:”))
Program to accept the 5 subject marks and display total and percentage.
Function name studentmrk().
def studentmrk(m1,m2,m3,m4,m5):
total=m1+m2+m3+m4+m5
per=total/5
print(“total:”,total, “per:”,per)
studentmrk(mk1,mk2,mk3,mk4,mk5)
m1,m2,m3,m4,m5 are formal parameter and
store the copied value passes through actual
parameter
studentmrk(mk1,mk2,mk3,mk4,mk5)
----Output----
Enter marks1 : 35
Enter marks2 : 45
Enter marks3 : 55
Enter marks4 : 65
Enter marks5 : 75
total:275 per: 55.0
mk1=int(input(“Enter marks1:”))
mk2=int(input(“Enter marks2:”))
mk3=int(input(“Enter marks3:”))
mk4=int(input(“Enter marks4:”))
mk5=int(input(“Enter marks5:”))
Program to accept the number and check it’s a even number or odd number
using function name checkevod() with 1 parameter
def checkevod(n):
if( n% 2== 0):
print(“Its even”)
else:
print(“its odd”)
checkevod(no)
n is formal parameter and store the copied value
passes through actual parameter checkevod(no)
----Output----
Enter number: 3
Its oddno=int(input(“Enter number:”))
Program to define a function checkage() with age as a parameter accept the
age from the user and check its age is valid to vote if age is more than equal
to 18 otherwise print “you a not valid to vote”
Program to define a function checkPN() with n variable as parameter and
check that number pass through parameter is positive or negative.
Program to define a function checkleapyr() with year as a parameter and
check it’s a leap year or not.
How to use nested if’s inside the function.
For example: Program define function name dis_day() with 1 parameter and accept no(1-
7) and print 1 for “Monday”,2 for “Tuesday” ………
def dis_day(n):
if( n== 1):
print(“Monday”)
elif(n== 2):
print(“Tuesday”)
elif(n== 3):
print(“Wedneday”)
elif(n== 4):
print(“Thursday”)
elif(n== 5):
print(“Friday”)
elif(n== 6):
print(“Saturday”)
elif(n== 7):
print(“Sunday”)
else:
print(“Invalid”)
----Output----
Enter days(1-7): 3
Wednesday
dis_day(n)
no=int(input(“Enter days (1-7):”))
So now if we need to design calculator with the parameterized function and nested if’s.
For example: Program define function name calculate() with 3 parameters and accept two
operands and one operator (+,-,*,/,//) from the user and display the output.
def calculate(x,y,op):
if( op== ‘+’):
print(x,”+”,y,”=“,x+y)
elif(op== ‘-’):
print(x,”-”,y,”=“,x-y)
elif(op== ‘*’):
print(x,”*”,y,”=“,x*y)
elif(op== ‘/’):
print(x,”/”,y,”=“,x/y)
elif(op== ‘//’):
print(x,”//”,y,”=“,x//y)
----Output----
Enter value1: 3
Enter value2: 4
Enter operator(+,-,*,/,//): *: +
3 + 4 =7
calculate(n1,n2,opp)
n1=int(input(“Enter value1:”))
n2=int(input(“Enter value2:”))
opp=int(input(“Enter operator(+,-,*,/,//:”))

Mais conteúdo relacionado

Mais procurados (20)

Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Python Modules
Python ModulesPython Modules
Python Modules
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Python functions
Python functionsPython functions
Python functions
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
Python basic
Python basicPython basic
Python basic
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
C pointer
C pointerC pointer
C pointer
 
Recursion
RecursionRecursion
Recursion
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 
Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 

Semelhante a USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]

Semelhante a USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS] (20)

Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions.pdf
Functions.pdfFunctions.pdf
Functions.pdf
 
Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Function in c
Function in cFunction in c
Function in c
 
Function in c
Function in cFunction in c
Function in c
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Functions
FunctionsFunctions
Functions
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
 
functions
functionsfunctions
functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 

Mais de vikram mahendra

Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemvikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shopvikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMvikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Pythonvikram mahendra
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONvikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONvikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONvikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2vikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2vikram mahendra
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONvikram mahendra
 

Mais de vikram mahendra (20)

Communication skill
Communication skillCommunication skill
Communication skill
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
GREEN SKILL[PART-2]
GREEN SKILL[PART-2]GREEN SKILL[PART-2]
GREEN SKILL[PART-2]
 
GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
Entrepreneurial skills
Entrepreneurial skillsEntrepreneurial skills
Entrepreneurial skills
 
Boolean logic
Boolean logicBoolean logic
Boolean logic
 

Último

31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Último (20)

31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]

  • 1. FUNCTION IN PYTHON CLASS: XII COMPUTER SCIENCE -083 USER DEFINED FUNCTIONS PART-4 FUNCTIONS with PARAMETERS
  • 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
  • 3. Example To define User Defined Functions: def myname(nm): print(nm) This is function definition To call this function in a program myname(‘WELCOME’) This is function calling ----Output----- WELCOME Full python code: def myname(nm): print(nm) myname(‘WELCOME’)That nm is a parameter
  • 4. Display the 4 names “JAMES”, “ Blake”, “Tom”, ”John” with the help of one function and one print only. def display1(): print(“JAMES”) def display4(): print(“John”) def display3(): print(“Tom”) def display2(): print(“Blake”) display1() display2() display3() display4() ---Output----- JAMES John Tom Blake
  • 5. So what problem we face , that is four name four functions with different function name .Then we call all these functions. What we need here that one function name that use to display all four names.
  • 6. Display the 4 names “JAMES”, “ Blake”, “Tom”, ”John” with the help of one function and one print only. def display(nm): display(‘JAMES’) ---Output----- JAMES John Tom Blake print(nm) display(‘Blake’) display(‘Tom’) display(‘John’) We passes nm as a parameter that store the values passes from display() at the bottom.
  • 7. So WHAT is important part in parameterized function it is the parameter pass inside the function header, and what is the work of this parameter is to store the value pass from the calling function. Now again we take one more example to understand parameterized functions.
  • 8. In this we pass a number 5 as a parameter through a function name add() and display the sum of 5 with itself. def sum(n): print(“sum=“,n+n) sum(5) When we execute the program the function sum() will call and value 5 passes to a parameter no. This is the original value and it is called actual parameter. Actual parameters are values that are passed to a function when it is invoked.Here 5 is a actual parameter This is copied value for the function. So the function sum() uses this value that comes from actual parameter and this value known as formal parameter. Formal parameters are the variables defined by the function that receives values when the function is called. So here n is a formal parameter
  • 9. So now if we need to add to numbers means we need to pass two numbers as a parameter and display there sum. Function name sum() with two parameters. def sum(a,b): print(“sum=“,a+b) sum(5,6) a, b are formal parameter and store the copied value passes through actual parameter sum(5,6) ----Output---- sum=11
  • 10. Program to accept the two numbers from user and display there addition through a function sum() with two parameters . def sum(a,b): print(“sum=“,a+b) sum(x,y) a, b are formal parameter and store the copied value passes through actual parameter sum(x,y) Where x value passes to a and y value to b ----Output---- Enter the value x: 7 Enter the value y: 3 sum=10 x=int(input(“Enter the value x:”)) y=int(input(“Enter the value y:”))
  • 11. Program to accept the radius from user and display area of circle using function circle() with radius as a parameter. def circle(r): print(“Area of circle=“,3.14*r*r) circle(rad) r is formal parameter and store the copied value passes through actual parameter circle(rad) Where rad value passes to r, so rad is actual parameter and r is a formal parameter. ----Output---- Enter radius: 3 Area of circle=28.259 rad=int(input(“Enter radius:”))
  • 12. Program to accept the 5 subject marks and display total and percentage. Function name studentmrk(). def studentmrk(m1,m2,m3,m4,m5): total=m1+m2+m3+m4+m5 per=total/5 print(“total:”,total, “per:”,per) studentmrk(mk1,mk2,mk3,mk4,mk5) m1,m2,m3,m4,m5 are formal parameter and store the copied value passes through actual parameter studentmrk(mk1,mk2,mk3,mk4,mk5) ----Output---- Enter marks1 : 35 Enter marks2 : 45 Enter marks3 : 55 Enter marks4 : 65 Enter marks5 : 75 total:275 per: 55.0 mk1=int(input(“Enter marks1:”)) mk2=int(input(“Enter marks2:”)) mk3=int(input(“Enter marks3:”)) mk4=int(input(“Enter marks4:”)) mk5=int(input(“Enter marks5:”))
  • 13. Program to accept the number and check it’s a even number or odd number using function name checkevod() with 1 parameter def checkevod(n): if( n% 2== 0): print(“Its even”) else: print(“its odd”) checkevod(no) n is formal parameter and store the copied value passes through actual parameter checkevod(no) ----Output---- Enter number: 3 Its oddno=int(input(“Enter number:”))
  • 14. Program to define a function checkage() with age as a parameter accept the age from the user and check its age is valid to vote if age is more than equal to 18 otherwise print “you a not valid to vote” Program to define a function checkPN() with n variable as parameter and check that number pass through parameter is positive or negative. Program to define a function checkleapyr() with year as a parameter and check it’s a leap year or not.
  • 15. How to use nested if’s inside the function. For example: Program define function name dis_day() with 1 parameter and accept no(1- 7) and print 1 for “Monday”,2 for “Tuesday” ……… def dis_day(n): if( n== 1): print(“Monday”) elif(n== 2): print(“Tuesday”) elif(n== 3): print(“Wedneday”) elif(n== 4): print(“Thursday”) elif(n== 5): print(“Friday”) elif(n== 6): print(“Saturday”) elif(n== 7): print(“Sunday”) else: print(“Invalid”) ----Output---- Enter days(1-7): 3 Wednesday dis_day(n) no=int(input(“Enter days (1-7):”))
  • 16. So now if we need to design calculator with the parameterized function and nested if’s. For example: Program define function name calculate() with 3 parameters and accept two operands and one operator (+,-,*,/,//) from the user and display the output. def calculate(x,y,op): if( op== ‘+’): print(x,”+”,y,”=“,x+y) elif(op== ‘-’): print(x,”-”,y,”=“,x-y) elif(op== ‘*’): print(x,”*”,y,”=“,x*y) elif(op== ‘/’): print(x,”/”,y,”=“,x/y) elif(op== ‘//’): print(x,”//”,y,”=“,x//y) ----Output---- Enter value1: 3 Enter value2: 4 Enter operator(+,-,*,/,//): *: + 3 + 4 =7 calculate(n1,n2,opp) n1=int(input(“Enter value1:”)) n2=int(input(“Enter value2:”)) opp=int(input(“Enter operator(+,-,*,/,//:”))