0% found this document useful (0 votes)
2 views17 pages

Understanding Python Functions Basics

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

Understanding Python Functions Basics

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

Chapter 3.

Functions
Outline
1. Function Basics
2. Local and Global Variables
3. Nested Function
4. Lambda Function
5. Functions as Arguments to Functions

2
1. Functions

Function is a sequence of instructions that performs a specific task.

A function is a block of code that can run when it is called. A function can have input
arguments, which are made available to it by the user, the entity calling the function.

Functions also have output parameters, which are the results of the function that the user
expects to receive once the function has completed its task.

Example: the function [Link]() has one input argument, an angle in radians, and one
output argument, an approximation to the sin function computed at the input angle
(rounded to 16 digits).
1. Define your own function

We can define our own functions. A function can be specified in several ways. Here we will
introduce the most common way to define a function which can be specified using the
keyword def, as showing in the following:

Definition of a function
1. Define your own function

We could see that defining a Python function need the following two components:

1. Function header: A function header starts with a keyword def, followed by a pair of
parentheses with the input arguments inside, and ends with a colon (:)

2. Function Body: An indented (usually four white spaces) block to indicate the main
body of the function. It consists 3 parts:
● Descriptive string: A string that describes the function. It could be multiple
lines.
● Function statements: These are the step by step instructions the function will
execute. ‘#’ starts a comment line, which means that the function will not
execute it.
● Return statements (Optional): A function could return some parameters after
the function is called.
1. Define your own function
1. Call your Function

Use your function my_adder to compute the sum of a few numbers. Verify that the result is
correct. Try calling the help function on my_adder.

The value of variable d is 6.


2. Local and Global Variables

A function also has its own memory block that is reserved for variables created
within that function.

Function's blocks of memory are not shared with the whole notebook memory block.
Therefore, a variable with a given name can be assigned within a function without
changing a variable with the same name outside of the function.

The memory block associated with the function is opened every time a function is
used.
2. Local and Global Variables

- Local Variables: Variables like out in the function my_adder are local to the
function. They only exist within the function’s scope and do not affect variables
outside of it.

- Memory Blocks: When my_adder is called, Python creates a new memory block for
the function’s variables. This includes a new out variable, which is separate from
any out variable defined outside the function.

- No Impact on External Variables: Assignments to out inside the function do not


affect the out variable outside, even if they have the same name, because they are
in different memory blocks.
2. Local and Global Variables
3. Nested Functions

A nested function is a function that is defined within another


function - parent function. Only the parent function is able to
call the nested function. However, the nested function
retains a separate memory block from its parent function.

11
3. Nested Functions

12
3. Lambda Functions

Sometimes, we don’t want to use the normal way to define a function, especially if our
function is just one line. In this case, we can use anonymous function in Python, which is a
function that is defined without a name. This type of functions also called lambda function,
since they are defined using the lambda keyword. A typical lambda function is defined:

13
3. Lambda Functions

14
2.2 Functions as Arguments
Sometimes it is useful to be able to pass a function as a variable to another function. In
other words, the input to some functions may be other functions. In last section, we did
see the lambda function returns a function object to the variable. In this section, we will
continue to see how the function object can be used as the input to another function.

15
2.2 Functions as Arguments

16
Exercises

17

You might also like