SlideShare 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

PPTX
UNIT 3 python.pptx
PDF
functions- best.pdf
PDF
3-Python Functions.pdf in simple.........
PPTX
Functions in python slide share
PPTX
Lecture 08.pptx
PPTX
Working with functions the minumum ppt.pptx
PPTX
functions in python language to run programming
UNIT 3 python.pptx
functions- best.pdf
3-Python Functions.pdf in simple.........
Functions in python slide share
Lecture 08.pptx
Working with functions the minumum ppt.pptx
functions in python language to run programming

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

PPT
functions modules and exceptions handlings.ppt
PPTX
Python Functions.pptx
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PPT
Python programming variables and comment
PPTX
Function in Python function in python.pptx
PPTX
Functions in Python Programming Language
PPTX
_Python_ Functions _and_ Libraries_.pptx
PPT
Powerpoint presentation for Python Functions
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPT
functions _
PPTX
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
PPT
python slides introduction interrupt.ppt
PDF
functions notes.pdf python functions and opp
PPTX
Functions and Modules.pptx
PDF
Python functions
PDF
PPTX
Functions in Python with all type of arguments
PPTX
2 Functions2.pptx
functions modules and exceptions handlings.ppt
Python Functions.pptx
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
Python programming variables and comment
Function in Python function in python.pptx
Functions in Python Programming Language
_Python_ Functions _and_ Libraries_.pptx
Powerpoint presentation for Python Functions
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
functions _
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
python slides introduction interrupt.ppt
functions notes.pdf python functions and opp
Functions and Modules.pptx
Python functions
Functions in Python with all type of arguments
2 Functions2.pptx
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Lesson notes of climatology university.
PDF
Sports Quiz easy sports quiz sports quiz
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Insiders guide to clinical Medicine.pdf
VCE English Exam - Section C Student Revision Booklet
102 student loan defaulters named and shamed – Is someone you know on the list?
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
Computing-Curriculum for Schools in Ghana
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Lesson notes of climatology university.
Sports Quiz easy sports quiz sports quiz
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
Ad

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.