0% found this document useful (0 votes)
3 views

Function 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Function 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Praveen Kumar A

• Working with Functions

• In this module we work with Python functions.


• We learn some common built-in functions, and how to
call or run these functions.
• We also learn how to define or create our own Python
functions.
• Then we coordinate multiple functions to perform more
complex tasks, and learn modularization concepts.
What Is a Function? (1 of 2)
• A function is a group of Python statements that work together
to perform a task.
• Examples :
– A function that calculate volume of a box
– A function that prints text to screen
• Properties of a function:
– Contains multiple statements
– Has a name Input
– Can accept one or more input data Function
Statement 1
to work with them Statement 2
– Can produce one output data …
Statement N

Output
• Some of the functions that we have worked with are:

function input

function name

output is saved in a variable


• When we type the function name followed by ( ) in a Python
statement, we call the function and cause the function to run.
• When a function runs, the statements that make up the
function are executed by the interpreter line by line.
• We can give the input to the function by typing input data
inside the ( ) that follows the function name.
• If a function has an output, we save the output by assigning it
to a variable.
# Example 3
#Python Program to find Square of a Number
#Using Function

Praveen Kumar A
• We are not limited to using just the built-in functions. Python
also allows us to create our own functions to use.
• To define or create a function (in 5 easy steps):

1. Start with the 2. Next add the 3. Declare input


keyword def function name parameters inside ( ),
(for define) and end with :

4. Optional
but highly
recommended:
Add comments
to describe the
function
!! Important !! 5. Add statements
Indent and line up all that do the task of
statements after the the function
first header statement
Function Definition (1 of 2)
• The function definition is the block of code that makes up the
function.
header

• A function definition starts with a function header, which is the


first line and has 3 parts:
– The keyword def, which tells Python that we’re defining this
block of statement with a name.
– A function name, which should be descriptive.
– A list of input parameters, which are variables that will store
input data of the function. The list is separated by comma.
– The line ends with a colon ( : ).
Function Definition (2 of 2)

function body

Following the header is the function body:


• Indent the function body with the exact same spacing.
• Highly recommended: start with a docstring, which is a
comment block to describe the function purpose, the expected
input, the output. The docstring starts with 3 single quotes or 3
double quotes, and must end with 3 matching type of quotes.
• After the docstring is the list of Python statements that make
up the function. Together they do the work of the function.
• If the function has an output, use the return keyword for the
output value.
Function IO (2 of 2)
• Example of a function that has no input argument and 1 return
value
Praveen Kumar A

You might also like