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

Lecture 2 2 Functions

Uploaded by

Shubham Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 2 2 Functions

Uploaded by

Shubham Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Understanding functions

formally and informally

Sukrit Gupta

January 17, 2024

Sukrit Gupta Intro to Computing and Data Structures 1/36


Acknowledgement and disclaimer
All mistakes (if any) are mine.
I have used several sources which I have referred to in the appropriate
places.

Sukrit Gupta Intro to Computing and Data Structures 2/36


Outline

1 The intuition behind functions

2 Formal definitions

3 Types of functions

4 Real world functions

5 Functions in Python

6 Variable Scope

Sukrit Gupta Intro to Computing and Data Structures 3/36


Section 1

The intuition behind functions

Sukrit Gupta Intro to Computing and Data Structures 4/36


Functions around us ...

Let go of all your preconceived notions about the topic. Follow me.

Imagine that I ask you to differentiate between the following two


vehicles using the given images:

Figure: Hyundai Santro and Hyundai Verna (credit: Autocar India)

What factors do you consider for this?

Let’s say: Color, Weight, Length?

Sukrit Gupta Intro to Computing and Data Structures 5/36


Being aware of the process

What we are trying to do? From the given image, understand how the
two cars look like. Let’s try to summarize the process:
Using visual aids (our eyes), we see the two images. (Input:
Images of vehicles.)
Our brain determines important differentiating factors for the two
vehicles. (Features = ?)
We assign certain value of the features to the two vehicles. For
example: if feature color = red, then vehicle = Verna.

Sukrit Gupta Intro to Computing and Data Structures 6/36


Formalizing it

What we have done is, we have involuntarily, subconsciously learnt


a function that maps the images and cars.
Formally:
f :X →Y
where X is the set of input values, and Y is the set of output
values.
Can anyone tell me what X and Y are in our case?
In our case, X = {(silver, 900kg, 3600mm), (red, 1100kg, 4440mm)}
and Y = {Santro, Verna}
So, f (silver, 900kg, 3600mm) = Santro and
f (red, 1100kg, 4440mm) = Verna

Sukrit Gupta Intro to Computing and Data Structures 7/36


Section 2

Formal definitions

Sukrit Gupta Intro to Computing and Data Structures 8/36


Functions
Definition
A function is a rule that assigns each input exactly one output. So
what output do you assign to an input, x, needs to be determined by a
single rule.

Figure: Function f (x) = 2x + 1.

Sukrit Gupta Intro to Computing and Data Structures 9/36


Parts of a Function

Domain: The set of inputs, X .


Codomain: The set of allowable outputs.
Range: The actual set of outputs of a function.
Mapping: The description of output value y ∈ Y in terms of
input value x ∈ X .
We would write f : X → Y to describe a function with name f ,
domain X and codomain Y.

Sukrit Gupta Intro to Computing and Data Structures 10/36


A closer look at functions
For example, consider the function f : R → R, f (x) = 0.1x2 + 5.

What is the domain and codomain of this function?

What will this function look like? Any guesses?

Figure: f (x) = 0.1x2 + 5.

Sukrit Gupta Intro to Computing and Data Structures 11/36


Figure: f (x) = 0.1x2 + 5.

f (1) = 0.1 × 1 + 5
f (2) = 0.1 × 4 + 5
f (3) = 0.1 × 9 + 5

Same domain and codomain?


Same codomain and range?
Not every real number actually is an output (there is no way to
get values ∈ [0, 5))

Sukrit Gupta Intro to Computing and Data Structures 12/36


By the way ...

Look at this classifier that distinguishes between images of digits.


What does it do?

Figure: Classifying hand-written digits.

Sukrit Gupta Intro to Computing and Data Structures 13/36


Section 3

Types of functions

Sukrit Gupta Intro to Computing and Data Structures 14/36


Surjective/Onto functions

When range equals codomain, the function is called surjective or


onto.
The terminology should make sense: the function puts the range
(entirely) on top of the codomain.
Is f : R → R, f : x → x2 an onto function?
What about f : R → [0, ∞), f : x → x2 ?

Sukrit Gupta Intro to Computing and Data Structures 15/36


Injective/One-to-one functions

When each element of the codomain is mapped to at most one


element of the domain, the function is called one-to-one or
injective.
Is f : R → R, f : x → x2 a one-to-one function?
What about f : R → R, f : x → x + 1?

Sukrit Gupta Intro to Computing and Data Structures 16/36


Bijective/Both Surjective and Injective

It should be clear that there are functions which are surjective,


injective, both, or neither.
A function that is both one-to-one and onto (an injection and
surjection), is called a bijective function.
Is f : R → R, f : x → x + 1 a bijective function?

Sukrit Gupta Intro to Computing and Data Structures 17/36


Q: What type of fn. is f : R+ → R, f : x → log(x)

Sukrit Gupta Intro to Computing and Data Structures 18/36


Why know about different types of functions?

When discussing functions, we go from element in the domain (say


x) to its corresponding element in the codomain (denoted by f (x)).
What if we want to go the other way?
Start with an element from the codomain (say y) and understand
which element or elements (if any) from the domain it is mapped
to.
Suppose f : X → Y, for y ∈ Y, f −1 (y) represents the set of all
elements in the domain X that are mapped to y. That is,
f −1 (y) = {x ∈ X , f (x) = y}.
Although f −1 (y) is defined (but may not exist) for any function f ,
inverse functions only exist for bijections.

Sukrit Gupta Intro to Computing and Data Structures 19/36


Section 4

Real world functions

Sukrit Gupta Intro to Computing and Data Structures 20/36


Why do you need functions in Python

Imagine you have a car.


The car has multiple components.
All the components coordinate with each other and also work
independently.
Imagine that your car has an issue and you take it to a mechanic.
The mechanic does not open all the components in your car, but
instead tries to locate the specific component that has an issue.
This is possible because the car has a modular design.

Sukrit Gupta Intro to Computing and Data Structures 21/36


Why functions?

Real-world systems are modular.


Functions help reduce the amount of code. The more code a
program contains, the more chance there is for something to go
wrong, and the harder the code is to maintain.
Also functions provide a level of abstraction.

Sukrit Gupta Intro to Computing and Data Structures 22/36


Q: Can you think of more modular systems?

Uber mobile application.


ATM machine.
Mobile phone.

Sukrit Gupta Intro to Computing and Data Structures 23/36


Section 5

Functions in Python

Sukrit Gupta Intro to Computing and Data Structures 24/36


Syntax

def name_of_function (list of formal parameters):


body of function

def is a reserved word that tells Python that a function is about to be


defined.

The function name is simply a name that is used to refer to the


function.

Sukrit Gupta Intro to Computing and Data Structures 25/36


Example

Suppose you have the function: y = f (x) = x2 . How to code it?

def f(x):
return x**2

y = f(3)

Sukrit Gupta Intro to Computing and Data Structures 26/36


Example

def max_(x, y):


if x > y:
return x
else:
return y

z = max_(3, 4)

x, y in the function definition are the formal parameters of the


function.
During function call the formal parameters are bound to the
actual parameters (or arguments) of the function call. For
example, the function call max (3, 4) binds x to 3 and y to 4.

Sukrit Gupta Intro to Computing and Data Structures 27/36


Function call

To recapitulate, when a function is called:


1 The actual parameters are evaluated, and the formal parameters
of the function are bound to the resulting values.
2 The point of execution (the next instruction to be executed)
moves from the point of invocation to the first statement in the
body of the function.
3 The code in the body of the function is executed until either a
return statement is encountered (value of function invocation is
the value of the expression following the return) or there are no
more statements to execute (function returns the value None).
4 The value of the invocation is the returned value.
5 The point of execution is transferred back to the code immediately
following the invocation.

Sukrit Gupta Intro to Computing and Data Structures 28/36


Functions with/without a return statement
Take two examples of functions:

def max1(x, y):


if x > y:
return x
else:
return y

def max2(x, y):


if x > y:
print(x)
else:
print(y)

def max3(x, y):


if x > y:
print(x)
else:
print(y)
return None

Sukrit Gupta Intro to Computing and Data Structures 29/36


Section 6

Variable Scope

Sukrit Gupta Intro to Computing and Data Structures 30/36


Variable scope
def f(x): #name x used as formal parameter
y = 1
x = x + y
print ('x =', x) #
print ('y =', y) #
return x
x = 3
y = 2
z = f(x) #value of x used as actual parameter
print ('z =', z)
print ('x =', x) #
print ('y =', y) #

Each function defines a new name space, also called a scope.


The formal parameter x and local variable y used in f exist only
within the scope of f. Assignments in f have no effect on the
bindings of the names x and y that exist outside the scope of f.
Sukrit Gupta Intro to Computing and Data Structures 31/36
How to think about variable scope

Here’s one way to think about this:


At top level, i.e., the level of the shell, a symbol table keeps track
of all names defined at that level and their current bindings.
When a function is called, a new symbol table (sometimes called a
stack frame) is created. This table keeps track of all names defined
within the function (including the formal parameters) and their
current bindings.
If a function is called from within the function body, yet another
stack frame is created.
When the function completes, its stack frame goes away.

Sukrit Gupta Intro to Computing and Data Structures 32/36


How to think about variable scope

def f(x):
y = 1
x = x + y
print ('x =', x) #
print ('y =', y) #
return x
x = 3
y = 2
z = f(x)
print ('z =', z)
print ('x =', x) #
print ('y =', y) #

Sukrit Gupta Intro to Computing and Data Structures 33/36


Q: Draw symbol table.

def f(x):
def h():
z = x
print ('z =', z) #
def g():
x = 'abc'
print ('x =', x) #
x = x + 1
print ('x =', x) #
h()
g()
print ('x =', x) #
return x
x = 3
z = f(x)
print ('x =', x) #
print ('z =', z) #

Sukrit Gupta Intro to Computing and Data Structures 34/36


What did we learn today?

1 The intuition behind functions

2 Formal definitions

3 Types of functions

4 Real world functions

5 Functions in Python

6 Variable Scope

Sukrit Gupta Intro to Computing and Data Structures 35/36


Thank you!

Sukrit Gupta Intro to Computing and Data Structures 36/36

You might also like