際際滷

際際滷Share a Scribd company logo
Python Functions
In Python, a function is a group of related statements that performs a specific
task.
Functions help break our program into smaller and modular chunks.
They take inputs, do some specific computation and produces output.
These are logical functional blocks of code that can be reused independently
from the main program.
Python provides built-in functions like print(), etc. but we can also create
your own functions. These functions are called user-defined functions.
Benefits of Modularizing a Program with Functions
 The benefits of using functions include:
 Simpler code
 Code reuse
 write the code once and call it multiple times
 Better testing and debugging
 Can test and debug each function individually
 Faster development
 Easier facilitation of teamwork
 Different team members can write different functions
Void Functions and Value-Returning Functions
 A void function:
 Simply executes the statements it contains and then terminates.
 A value-returning function:
 Executes the statements it contains, and then it returns a value
back to the statement that called it.
 The input, int, and float functions are examples of value-
returning functions.
Defining and Calling a Function
 Functions are given names
 Function naming rules:
 Cannot use key words as a function name
 Cannot contain spaces
 First character must be a letter or underscore
 All other characters must be a letter, number or underscore
 Uppercase and lowercase characters are distinct
Here are simple rules to define a function in Python:
 Function blocks begin with the keyword def followed by the function
name and parentheses ( ( ) ).
def functionname():
statements
def my_function():
print("Hello World!")
Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these
parentheses. These are optional.
def function_name(parameter):
statements
return
def greet(name):
print("Hello, " + name + ". Good morning!")
>>> greet(Naman')
The first statement of a function can be an optional statement  called the
documentation string of the function or docstring.
It is briefly used to explain what a function does.
It is an optional statement, documentation is a good programming practice.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back
an expression to the caller.
A return statement with no arguments is the same as return None.
Required arguments
Required arguments are the arguments passed to a function in correct
positional order.
Here, the number of arguments in the function call should match exactly
with the function definition.
To call the function hello(), you definitely need to pass one argument,
otherwise it gives a syntax error
Def hello(str):
print (str)
A default argument is an argument that assumes a default value if a value is
not provided in the function call for that argument.
We can provide a default value to an argument by using the assignment
operator (=).
Default Arguments:
Keyword arguments
Keyword arguments are related to the function calls.
When you use keyword arguments in a function call, the caller identifies the
arguments by the parameter name.
This allows us to place them out of order because the Python interpreter is
able to use the keywords provided to match the values with parameters.
Arbitrary Arguments
Sometimes, we do not know in advance the number of arguments that
will be passed into a function.
Python allows us to handle this kind of situation through function calls
with an arbitrary number of arguments.
In the function definition, we use an asterisk (*) before the parameter
name to denote this kind of argument.
Learn more about the concepts Functions of Python
Pass by Reference or pass by value?
One important thing to note is, in Python every variable name is a
reference.
When we pass a variable to a function, a new reference to the object is
created.
It means if you change what a parameter refers to within a function, the
change also reflects back in the calling function.
Scope of a variable
Scope of a variable is the portion of a program where the variable is recognized.
Parameters and variables defined inside a function are not visible from outside
the function. Hence, they have a local scope.
Global variables are the one that are defined and declared outside a function
and we need to use them inside a function.
If there is no local num, the value
from the global num will be
used.
If a variable with the same name is defined inside the scope of function as
well then it will print the value given inside the function only and not the
global value.
Learn more about the concepts Functions of Python
Split Function
Calculate the length of the word using len() function. If the length is
even, then print the word.

More Related Content

Similar to Learn more about the concepts Functions of Python (20)

FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Krushal Kakadia
use of Functions to write python program.pptx
use of Functions to write python program.pptxuse of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
bhuvanalakshmik2
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
Hattori Sidek
Functions Programming in Python Language
Functions Programming in Python LanguageFunctions Programming in Python Language
Functions Programming in Python Language
BalaSubramanian376976
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
AXL Computer Academy
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........
mxdsnaps
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
Decided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the numberDecided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the number
harshoberoi2050
C functions
C functionsC functions
C functions
University of Potsdam
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam
use of Functions to write python program.pptx
use of Functions to write python program.pptxuse of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
Functions Programming in Python Language
Functions Programming in Python LanguageFunctions Programming in Python Language
Functions Programming in Python Language
BalaSubramanian376976
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........3-Python Functions.pdf in simple.........
3-Python Functions.pdf in simple.........
mxdsnaps
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
Decided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the numberDecided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the number
harshoberoi2050
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
"The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G..."The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G...
neelottama
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
AituzazKoree
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷sPurchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Celine George
BUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptx
BUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptxBUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptx
BUSINESS QUIZ | THE QUIZ CLUB OF PSGCAS | 17TH MARCH 2025 .pptx
Quiz Club of PSG College of Arts & Science
The Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptxThe Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptx
SamarHosni3
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
Nguyen Thanh Tu Collection
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
UNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptxUNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptx
HARIHARAN A
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions
leigh441292
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation FourthStrategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
keileyrazawi
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
Pushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptx
Pushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptxPushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptx
Pushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
NC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-BeingNC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-Being
Mebane Rash
Unit No 4- Chemotherapy of Malignancy.pptx
Unit No  4- Chemotherapy of Malignancy.pptxUnit No  4- Chemotherapy of Malignancy.pptx
Unit No 4- Chemotherapy of Malignancy.pptx
Ashish Umale
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
"The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G..."The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G...
neelottama
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
AituzazKoree
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷sPurchase Analysis in Odoo 17 - Odoo 際際滷s
Purchase Analysis in Odoo 17 - Odoo 際際滷s
Celine George
The Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptxThe Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptx
SamarHosni3
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
TI LI畛U CHUYN SU L畛P 12 THEO CH働NG TRNH M畛I DNG CHO C畉 3 B畛 SCH N THI...
Nguyen Thanh Tu Collection
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
UNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptxUNIT 1 Introduction to communication.pptx
UNIT 1 Introduction to communication.pptx
HARIHARAN A
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions2. ES: Application of Number: Units of measure & conversions
2. ES: Application of Number: Units of measure & conversions
leigh441292
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation FourthStrategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
keileyrazawi
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
NC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-BeingNC Advisory Council on Student Safety and Well-Being
NC Advisory Council on Student Safety and Well-Being
Mebane Rash
Unit No 4- Chemotherapy of Malignancy.pptx
Unit No  4- Chemotherapy of Malignancy.pptxUnit No  4- Chemotherapy of Malignancy.pptx
Unit No 4- Chemotherapy of Malignancy.pptx
Ashish Umale

Learn more about the concepts Functions of Python

  • 2. In Python, a function is a group of related statements that performs a specific task. Functions help break our program into smaller and modular chunks. They take inputs, do some specific computation and produces output. These are logical functional blocks of code that can be reused independently from the main program. Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are called user-defined functions.
  • 3. Benefits of Modularizing a Program with Functions The benefits of using functions include: Simpler code Code reuse write the code once and call it multiple times Better testing and debugging Can test and debug each function individually Faster development Easier facilitation of teamwork Different team members can write different functions
  • 4. Void Functions and Value-Returning Functions A void function: Simply executes the statements it contains and then terminates. A value-returning function: Executes the statements it contains, and then it returns a value back to the statement that called it. The input, int, and float functions are examples of value- returning functions.
  • 5. Defining and Calling a Function Functions are given names Function naming rules: Cannot use key words as a function name Cannot contain spaces First character must be a letter or underscore All other characters must be a letter, number or underscore Uppercase and lowercase characters are distinct
  • 6. Here are simple rules to define a function in Python: Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). def functionname(): statements def my_function(): print("Hello World!")
  • 7. Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses. These are optional. def function_name(parameter): statements return def greet(name): print("Hello, " + name + ". Good morning!") >>> greet(Naman')
  • 8. The first statement of a function can be an optional statement called the documentation string of the function or docstring. It is briefly used to explain what a function does. It is an optional statement, documentation is a good programming practice.
  • 9. The code block within every function starts with a colon (:) and is indented. The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
  • 10. Required arguments Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition. To call the function hello(), you definitely need to pass one argument, otherwise it gives a syntax error Def hello(str): print (str)
  • 11. A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. We can provide a default value to an argument by using the assignment operator (=). Default Arguments:
  • 12. Keyword arguments Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name. This allows us to place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters.
  • 13. Arbitrary Arguments Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls with an arbitrary number of arguments. In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument.
  • 15. Pass by Reference or pass by value? One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.
  • 16. Scope of a variable Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function are not visible from outside the function. Hence, they have a local scope. Global variables are the one that are defined and declared outside a function and we need to use them inside a function. If there is no local num, the value from the global num will be used.
  • 17. If a variable with the same name is defined inside the scope of function as well then it will print the value given inside the function only and not the global value.
  • 20. Calculate the length of the word using len() function. If the length is even, then print the word.