SlideShare a Scribd company logo
1
Functions
Python enables its programmers to break up a program into segments commonly known as functions, each
of which can be written more or less independently of the others. Every function in the program is
supposed to perform a well-defined task.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Need for Functions
2
Each function to be written and tested separately.
• Understanding, coding and testing multiple separate functions is far easier.
Without the use of any function, then there will be countless lines in the code and maintaining it will be a big
mess.
• Programmers use functions without worrying about their code details.This speeds up program development,
by allowing the programmer to concentrate only on the code that he has to write.
Different programmers working on that project can divide the workload by writing different functions.
• Like Python libraries, programmers can also make their functions and use them from different point in the
main program or any other program that needs its functionalities.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
3
Function Declaration and Definition
• A function, f that uses another function g, is known as the calling function and g is known as the called
function.
• The inputs that the function takes are known as arguments/parameters.
• When a called function returns some result back to the calling function, it is said to return that result.
• The calling function may or may not pass parameters to the called function. If the called function accepts
arguments, the calling function will pass parameters, else not.
• Function declaration is a declaration statement that identifies a function with its name, a list of arguments
that it accepts and the type of data it returns.
• Function definition consists of a function header that identifies the function, followed by the body of the
function containing the executable code for that function.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
4
Function Definition
Function blocks starts with the keyword def.
• The keyword is followed by the function name and parentheses (( )).
• After the parentheses a colon (:) is placed.
• Parameters or arguments that the function accept are placed within parentheses.
• The first statement of a function can be an optional statement - the docstring describe what the function does.
• The code block within the function is properly indented to form the block code.
• A function may have a return[expression] statement.That is, the return statement is optional.
• You can assign the function name to a variable. Doing this will allow you to call same function using the name
of that variable.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
5
Function Call
The function call statement invokes the function. When a function is invoked the program control jumps to
the called function to execute the statements that are a part of that function. Once the called function is
executed, the program control passes back to the calling function.
Function Parameters
A function can take parameters which are nothing but some values that are passed to it so that the function
can manipulate them to produce the desired result. These parameters are normal variables with a small
difference that the values of these variables are defined (initialized) when we call the function and are then
passed to the function.
Function name and the number and type of arguments in the function call must be same as that given in the
function definition.
If the data type of the argument passed does not matches with that expected in function then an error is
generated.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Example:
6
Examples
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
7
Local and GlobalVariables
A variable which is defined within a function is local to that function. A local variable can be accessed from
the point of its definition until the end of the function in which it is defined. It exists as long as the function is
executing. Function parameters behave like local variables in the function. Moreover, whenever we use the
assignment operator (=) inside a function, a new local variable is created.
Global variables are those variables which are defined in the main body of the program file.They are visible
throughout the program file.As a good programming habit, you must try to avoid the use of global variables
because they may get altered by mistake and then result in erroneous output.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
8
Local and GlobalVariables
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Example:
9
The Return Statement
The syntax of return statement is,
return [expression]
The expression is written in brackets because it is optional. If the expression is present, it is evaluated and the
resultant value is returned to the calling function. However, if no expression is specified then the function will
return None.
The return statement is used for two things.
• Return a value to the caller
• To end and exit a function and go back to its caller
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
10
Required Arguments
In the required arguments, the arguments are passed to a function in correct positional order. Also, the
number of arguments in the function call should exactly match with the number of arguments specified in
the function definition
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
11
Keyword Arguments
When we call a function with some values, the values are assigned to the arguments based on their position.
Python also allow functions to be called using keyword arguments in which the order (or position) of the
arguments can be changed.The values are not assigned to arguments according to their position but based
on their name (or keyword).
Keyword arguments are beneficial in two cases.
• First, if you skip arguments.
• Second, if in the function call you change the order of parameters.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
12
Variable-length Arguments
In some situations, it is not known in advance how many arguments will be passed to a function. In such cases,
Python allows programmers to make function calls with arbitrary (or any) number of arguments.
When we use arbitrary arguments or variable length arguments, then the function definition use an asterisk
(*) before the parameter name.The syntax for a function using variable arguments can be given as,
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
13
Default Arguments
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Example:
Python allows users to specify function arguments that can have default values.This means that a function can be
called with fewer arguments than it is defined to have.That is, if the function accepts three parameters, but function
call provides only two arguments, then the third parameter will be assigned the default (already specified) value.
The default value to an argument is provided by using the assignment operator (=). Users can specify a
default value for one or more arguments.
14
Recursive Functions
A recursive function is defined as a function that calls itself to solve a smaller version of its task until a final
call is made which does not require a call to itself. Every recursive solution has two major cases, which are as
follows:
• base case, in which the problem is simple enough to be solved directly without making any further calls to
the same function.
• recursive case, in which first the problem at hand is divided into simpler sub parts.
Recursion utilized divide and conquer technique of problem solving.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Example:

More Related Content

DOCX
Topic_11_Functions in C+__ language.docx
PPTX
Learn more about the concepts Functions of Python
PDF
3-Python Functions.pdf in simple.........
PPTX
FUNCTION CPU
PPTX
Python Functions.pptxhgtyuhffdrthnvfdssu
PPTX
Functions and modular programming.pptx
Topic_11_Functions in C+__ language.docx
Learn more about the concepts Functions of Python
3-Python Functions.pdf in simple.........
FUNCTION CPU
Python Functions.pptxhgtyuhffdrthnvfdssu
Functions and modular programming.pptx

Similar to functions in python language to run programming (20)

PPTX
Functions in Python and its types for beginners
PDF
Python functions
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PDF
functionnotes.pdf
PDF
Dive into Python Functions Fundamental Concepts.pdf
PPTX
use of Functions to write python program.pptx
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PPT
Powerpoint presentation for Python Functions
PPTX
Presentation1.pptx
PPTX
Functions in Python Syntax and working .
PPTX
UNIT 3 python.pptx
PPTX
functions in the c programming and the examples
PPTX
Chapter 4-database backup and recoveryProgramming Functions.pptx
PDF
Python functions
PPT
python slides introduction interrupt.ppt
PPT
Python programming variables and comment
PPT
358 33 powerpoint-slides_2-functions_chapter-2
Functions in Python and its types for beginners
Python functions
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
functionnotes.pdf
Dive into Python Functions Fundamental Concepts.pdf
use of Functions to write python program.pptx
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Powerpoint presentation for Python Functions
Presentation1.pptx
Functions in Python Syntax and working .
UNIT 3 python.pptx
functions in the c programming and the examples
Chapter 4-database backup and recoveryProgramming Functions.pptx
Python functions
python slides introduction interrupt.ppt
Python programming variables and comment
358 33 powerpoint-slides_2-functions_chapter-2
Ad

Recently uploaded (20)

PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Sunset Boulevard Student Revision Booklet
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Congenital Hypothyroidism pptx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
Open folder Downloads.pdf yes yes ges yes
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
How to Manage Loyalty Points in Odoo 18 Sales
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Software Engineering BSC DS UNIT 1 .pptx
Open Quiz Monsoon Mind Game Final Set.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
Week 4 Term 3 Study Techniques revisited.pptx
Onica Farming 24rsclub profitable farm business
Sunset Boulevard Student Revision Booklet
Renaissance Architecture: A Journey from Faith to Humanism
Congenital Hypothyroidism pptx
Introduction and Scope of Bichemistry.pptx
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
Ad

functions in python language to run programming

  • 1. 1 Functions Python enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function in the program is supposed to perform a well-defined task. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 2. Need for Functions 2 Each function to be written and tested separately. • Understanding, coding and testing multiple separate functions is far easier. Without the use of any function, then there will be countless lines in the code and maintaining it will be a big mess. • Programmers use functions without worrying about their code details.This speeds up program development, by allowing the programmer to concentrate only on the code that he has to write. Different programmers working on that project can divide the workload by writing different functions. • Like Python libraries, programmers can also make their functions and use them from different point in the main program or any other program that needs its functionalities. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 3. 3 Function Declaration and Definition • A function, f that uses another function g, is known as the calling function and g is known as the called function. • The inputs that the function takes are known as arguments/parameters. • When a called function returns some result back to the calling function, it is said to return that result. • The calling function may or may not pass parameters to the called function. If the called function accepts arguments, the calling function will pass parameters, else not. • Function declaration is a declaration statement that identifies a function with its name, a list of arguments that it accepts and the type of data it returns. • Function definition consists of a function header that identifies the function, followed by the body of the function containing the executable code for that function. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 4. 4 Function Definition Function blocks starts with the keyword def. • The keyword is followed by the function name and parentheses (( )). • After the parentheses a colon (:) is placed. • Parameters or arguments that the function accept are placed within parentheses. • The first statement of a function can be an optional statement - the docstring describe what the function does. • The code block within the function is properly indented to form the block code. • A function may have a return[expression] statement.That is, the return statement is optional. • You can assign the function name to a variable. Doing this will allow you to call same function using the name of that variable. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 5. 5 Function Call The function call statement invokes the function. When a function is invoked the program control jumps to the called function to execute the statements that are a part of that function. Once the called function is executed, the program control passes back to the calling function. Function Parameters A function can take parameters which are nothing but some values that are passed to it so that the function can manipulate them to produce the desired result. These parameters are normal variables with a small difference that the values of these variables are defined (initialized) when we call the function and are then passed to the function. Function name and the number and type of arguments in the function call must be same as that given in the function definition. If the data type of the argument passed does not matches with that expected in function then an error is generated. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Example:
  • 6. 6 Examples © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 7. 7 Local and GlobalVariables A variable which is defined within a function is local to that function. A local variable can be accessed from the point of its definition until the end of the function in which it is defined. It exists as long as the function is executing. Function parameters behave like local variables in the function. Moreover, whenever we use the assignment operator (=) inside a function, a new local variable is created. Global variables are those variables which are defined in the main body of the program file.They are visible throughout the program file.As a good programming habit, you must try to avoid the use of global variables because they may get altered by mistake and then result in erroneous output. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 8. 8 Local and GlobalVariables © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Example:
  • 9. 9 The Return Statement The syntax of return statement is, return [expression] The expression is written in brackets because it is optional. If the expression is present, it is evaluated and the resultant value is returned to the calling function. However, if no expression is specified then the function will return None. The return statement is used for two things. • Return a value to the caller • To end and exit a function and go back to its caller © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 10. 10 Required Arguments In the required arguments, the arguments are passed to a function in correct positional order. Also, the number of arguments in the function call should exactly match with the number of arguments specified in the function definition © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 11. 11 Keyword Arguments When we call a function with some values, the values are assigned to the arguments based on their position. Python also allow functions to be called using keyword arguments in which the order (or position) of the arguments can be changed.The values are not assigned to arguments according to their position but based on their name (or keyword). Keyword arguments are beneficial in two cases. • First, if you skip arguments. • Second, if in the function call you change the order of parameters. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 12. 12 Variable-length Arguments In some situations, it is not known in advance how many arguments will be passed to a function. In such cases, Python allows programmers to make function calls with arbitrary (or any) number of arguments. When we use arbitrary arguments or variable length arguments, then the function definition use an asterisk (*) before the parameter name.The syntax for a function using variable arguments can be given as, © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 13. 13 Default Arguments © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Example: Python allows users to specify function arguments that can have default values.This means that a function can be called with fewer arguments than it is defined to have.That is, if the function accepts three parameters, but function call provides only two arguments, then the third parameter will be assigned the default (already specified) value. The default value to an argument is provided by using the assignment operator (=). Users can specify a default value for one or more arguments.
  • 14. 14 Recursive Functions A recursive function is defined as a function that calls itself to solve a smaller version of its task until a final call is made which does not require a call to itself. Every recursive solution has two major cases, which are as follows: • base case, in which the problem is simple enough to be solved directly without making any further calls to the same function. • recursive case, in which first the problem at hand is divided into simpler sub parts. Recursion utilized divide and conquer technique of problem solving. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Example: