0% found this document useful (0 votes)
6 views25 pages

Week7 Funtions

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)
6 views25 pages

Week7 Funtions

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/ 25

Ceng 240

Functions
Functions
• A function (or sub-routine, procedure or sub-
program)
• Is a grouping of actions under a given name that
performs a specific task
• len(), print(), abs(), type(): are functions
that we have seen before
• Pyhon adopts a syntax that remebles mathematical
functions, e.g.
• f(x, y) = 3x + 4y + 1
Defining Functions

• The 𝑆𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡 can make use of the 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑖 s to obtain


the actual arguments sent to the function at call-time.

• If the function is going to give a value in return to the call,


this value is provided by a return statement.

• 𝐹𝑢𝑛𝑐𝑡𝑖𝑜𝑛𝑁𝑎𝑚𝑒 is the name of the function being defined.


• Same naming conventions to name variables are used
Defining Functions

• Above is an example of a mathematical function


• The following is the python definition of the same function:

• def F_gravity(m_1, m_2, r):


return G * m_1 * m_2 / (r * r)

• The function F_gravity returns a value.


• m_1, m_2, r are function parameters.
Defining Functions
• def F_gravity(m_1, m_2, r):
return G * m_1 * m_2 / (r * r)

• But what about G?

• It is not provided in the arguments and used in the return


expression for its value.

• Since G is not defined inside the function, Python must seek


G in the global environment.
Defining Functions
• Therefore, when we call F_gravity, we must make sure that a
value for a global variable G (which apparently is the
Gravitational Constant) is set

• When we cover the scope of variables, we will be handling


the rules of this to a greater extent.
def F_gravity(m_1, m_2, r):
return G * m_1 * m_2 / (r * r)

G = 6.67408E-11
force = F_gravity(1000, 20, 0.5)
print(force, “Newton”)
Passing parameters to functions

• Arguments are evaluated before function is called:


Passing parameters to functions

• This is a legitimate code which performs the following in


order:
• Defines the function F_gravity
• Set the value of the global variables G, S, and Q
• Calls the print function which receives one argument.
• This argument is a function call to F_gravity.
• So, in order to have a value to print, first, this function call has to be
performed.
Passing parameters to functions

• A call to F_gravity is prepared.


• It has three arguments, each has to be evaluated and boiled
down to a value.
• So, a left-to-right order the arguments are evaluated:
• Q+S is avaluated, and the result is stored into parameter m_1
• Q-S is evaluateed, and the result is stored into parameter m_2
• Euclidean distance, is evaluated and the
result is stored into parameter r.
Passing parameters to functions

• Since all arguments are evaluated, the statement defining the


function F_gravity can be executed.
• The statement “return G * m_1 * m_2 / (r * r)”, is evaluated, and its
result is set as the return value of the function call.
• Now, the print function has got its parameter evaluated.
• The defining action of the print function can be carried out (print is
an internally defined function, we cannot see its definition).
Passing parameters to functions

• The bottom line is:


• Before the function is called, there is a preparation phase where all
arguments are evaluated and hence converted to a value.
Default parameters
• While defining a function, we can provide default
values to parameters as follows:

• Where Expi is an expression.


• When calling the function, we can omit providing
the parameters for which the function has default
values.
Default parameters: A simple
Example
Default parameters: A simple
Example
You should notice the following crucial points:
•We can call a function without providing any value for the
parameters that have default values (CASE 1, .., CASE 4).

•We can swap the order of default parameters when we are calling
the function (CASE 5 and CASE 6).

•When calling a function, we can give values to non-default


parameters by using their names (CASE 6).

•The parameters after the default parameters need to be given


names as well, both while defining the function and calling the
function (CASE 7).
Scope of Variables
Scope of varibles
• We extensively use variables while programming
• Not all variables are accessible from everywhere
• There is a set of rules that determine which parts of
a code can access a variable
• The code part from which a variable is accessible is
called the scope of that variable.
Scope of varibles
• In Python, there are four categories of scopes

• They are abbreviated with their initials LEGB:

• [𝐿ocal < 𝐸nclosing < 𝐺lobal < 𝐵uilt-in]


Scope of varibles: Local scope
• a and b are local variables which can be accessed
from their corresponding local scopes:
Scope of varibles: Local scope
• You can’t access variable “a” from func2()!
Scope of varibles: Local scope
• We can have two different local variaables with the
same name in two different functions:
Scope of varibles: Enclosing Scope
• If a variable is not defined in the local scope,
Python will search for it in the enclosing scope

We demonstrate the
enclosing scope using
nested functions in
this example
Scope of varibles: Global Scope
• In both examples: variable b is defined in the global
scope
Scope of varibles: Built-In Scope
• The variable e is defined in the math package. It is
built-in
Scope of varibles: Built-In Scope
• If another variable e is defined in the global or local
scope, it is used instead of the built-in version
Lecture summary
• Functions
• How to define functions
• Passing default parameters to functions
• Scope of variables

You might also like