SlideShare a Scribd company logo
1 of 21
Download to read offline
Functions
Ali Vaisifard
Stored and reused steps
We call these reusable pieces of
code “Functions”
Python Functions
• There are two kind of functions in Python
✓Built-in functions that are provided as part of Python –
print(), input(), type(), float(), int() …
✓Functions that we define ourselves and then use
• We treat the built-up function names as new reserved words
(i.e., we avoid them as variable names)
Function Definition
•In Python a function is some reusable code that takes
argument(s) as input, does some computation, and
then return a result or results.
•We define a function using the def reserved word
•We call / invoke the function by using the function
name, parentheses, and arguments in an expression
Max Function A function is some stored
code that we use.
A function takes some input
and produces some output
Max Function A function is some stored
code that we use.
A function takes some input
and produces some output
Type Conversions
• When you put an integer
and floating point in an
expression, the integer is
complicity converted to a
float
• You can control this with
the build-in functions
int() and float()
String Conversions
• You can also use int()
and float() to convert
between strings and
integers
• You will get an error if
the string does not
contain numeric
characters
Building our own functions
• We create a new function using the def keyword followed by optional
parameters in parentheses
• we indent the body of the function
• This defines the function but does not execute the body of the
function
Definition and Uses
•Once we have defined a function, We can
call / invoke it as many time as we like
•This is the store and reuse pattern
Arguments
• An argument is a value we pass into the functions as its input
when we call the function
• We use arguments so we can direct the function to do
different kinds of work when we call it at different times
• We put the arguments in parentheses after the name of the
function
Parameters
A parameters is a variable
which we use in the
function definition. It is a
“handle” that allows the
code in the function to
access the arguments for a
particular function
invocation.
Return Values
Often a function will take its arguments, do some computation, and
return a value to be used as the value of the function call in the calling
expression. The return keyword is used for this.
Return Values
• A “fruitful” function is one that
produces a results (or return
values)
• The return statement ends the
function execution and “sends
back” the result of the function
Arguments, Parameters, and Results
Multiple Parameters / Arguments
• We can define more than
one parameter in the
function definition
• We simply add more
arguments when we call
the function
• We match the number and
order of arguments and
parameters
Void (non-fruitful) Functions
•When a function does not return a
value, we call it a void function
•Functions that return values are
“fruitful” functions
•Void functions are “not fruitful”
To function or not to function…
• Organize your code into “paragraphs” –capture a complete thought
and “name it”
• Do not repeat yourself –make it work once and then reuse it
• If something gets too long or complex, break it up into logical chunks
and put those chunks in functions
• Make a library of common stuff that you do over and over perhaps
share this with your friends

More Related Content

Similar to An Introduction on Functions in python 3.pdf

Similar to An Introduction on Functions in python 3.pdf (20)

UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
Python functions
Python functionsPython functions
Python functions
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Function
Function Function
Function
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Function
FunctionFunction
Function
 
Python functions
Python   functionsPython   functions
Python functions
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 

An Introduction on Functions in python 3.pdf

  • 2. Stored and reused steps We call these reusable pieces of code “Functions”
  • 3. Python Functions • There are two kind of functions in Python ✓Built-in functions that are provided as part of Python – print(), input(), type(), float(), int() … ✓Functions that we define ourselves and then use • We treat the built-up function names as new reserved words (i.e., we avoid them as variable names)
  • 4. Function Definition •In Python a function is some reusable code that takes argument(s) as input, does some computation, and then return a result or results. •We define a function using the def reserved word •We call / invoke the function by using the function name, parentheses, and arguments in an expression
  • 5.
  • 6. Max Function A function is some stored code that we use. A function takes some input and produces some output
  • 7. Max Function A function is some stored code that we use. A function takes some input and produces some output
  • 8. Type Conversions • When you put an integer and floating point in an expression, the integer is complicity converted to a float • You can control this with the build-in functions int() and float()
  • 9. String Conversions • You can also use int() and float() to convert between strings and integers • You will get an error if the string does not contain numeric characters
  • 10. Building our own functions • We create a new function using the def keyword followed by optional parameters in parentheses • we indent the body of the function • This defines the function but does not execute the body of the function
  • 11.
  • 12. Definition and Uses •Once we have defined a function, We can call / invoke it as many time as we like •This is the store and reuse pattern
  • 13.
  • 14. Arguments • An argument is a value we pass into the functions as its input when we call the function • We use arguments so we can direct the function to do different kinds of work when we call it at different times • We put the arguments in parentheses after the name of the function
  • 15. Parameters A parameters is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation.
  • 16. Return Values Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this.
  • 17. Return Values • A “fruitful” function is one that produces a results (or return values) • The return statement ends the function execution and “sends back” the result of the function
  • 19. Multiple Parameters / Arguments • We can define more than one parameter in the function definition • We simply add more arguments when we call the function • We match the number and order of arguments and parameters
  • 20. Void (non-fruitful) Functions •When a function does not return a value, we call it a void function •Functions that return values are “fruitful” functions •Void functions are “not fruitful”
  • 21. To function or not to function… • Organize your code into “paragraphs” –capture a complete thought and “name it” • Do not repeat yourself –make it work once and then reuse it • If something gets too long or complex, break it up into logical chunks and put those chunks in functions • Make a library of common stuff that you do over and over perhaps share this with your friends