Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

L06 Functions

Uploaded by

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

L06 Functions

Uploaded by

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

COL100: Introduction to Computers and Programming

Semester II, 2022-23

Functions

1
Lecture outline
• This lecture introduces functions.
• Formal and actual parameters
• Scope of variables
Our programs so far…
• consisted of a sequence of instructions
• are easy for small-scale problems
• but messy for larger problems
• hard to keep track of details
• difficult to debug large programs
Good programming practice
• Decomposition: divide code into modules
• which are self-contained
• used to break up code
• intended to be reusable
• keep code organized
• keep code coherent
• Abstraction: treat a piece of code as a black box
• do not need to see details
• hide tedious coding details
• Achieve decomposition with functions and abstraction with
function specifications
Functions
• Mathematical functions
• f(x) = x2
• f(x,y) = x2 + y2
• In programming, functions help creating better structure with decomposition
• Functions are not run until they are “called” or “invoked” in a program
• Function characteristics:
• has a name
• has parameters (0 or more)
• has a body
• returns something
Defining and invoking a function

def is_even (i):

"""
Input: i, a positive int
Returns True if i is even, otherwise False
"""
print("inside is_even")
return i%2 == 0

is_even(3)
Defining and invoking a function
Consider f(x) = x2

def square(x): # defining function


return x*x # function body

square(4) # invoking function

>>> 16 # output
Formal and actual parameters
• formal parameter gets bound to the value of the actual parameter
when function is called
• actual parameter is also known as argument
def f(x):
x = x + 1
print('in f(x): x =', x)
return x

x = 3
z = f(x)
Function: Arguments
• Functions may not have arguments and return statement

def my_print(): # defining function


print(“Hello world”)

• Python returns the value None, if no return statement given


• A function can call another function

def repeat_my_print(): # defining function


my_print()
my_print()

repeat_my_print() # invoking function


Scope of a variable
• scope is mapping of names to objects
• new scope/frame/environment is created when a
function is executed
def f(x): Global scope f scope
x = x + 1 Some
f x 3
print('in f(x): x =', x) code
return x
x 3
x = 3
z = f(x)
z
Scope of a variable

def f(x): Global scope f scope


x = x + 1 Some
f x 4
print('in f(x): x =', x) code
return x
x 3
x = 3
z = f(x)
z
Scope of a variable

def f(x): Global scope f scope


x = x + 1 Some
f x 4
print('in f(x): x =', x) code
return x
x 3
x = 3 returns 4
z = f(x)
z
Scope of a variable

def f(x): Global scope


x = x + 1 Some
f
print('in f(x): x =', x) code
return x
x 3
x = 3
z = f(x)
z 4
Function: Arguments
• Arguments can take on any type, even functions
def func_a():
print 'inside func_a’ No argument
def func_b(y):
print 'inside func_b’
One argument
return y
def func_c(z):
print 'inside func_c’ One argument
return z() (function)
Function: Arguments
• Arguments can take on any type, even functions
def func_a():
print 'inside func_a’
def func_b(y):
print 'inside func_b’
return y
def func_c(z):
print 'inside func_c’
return z() Output
print func_a() None
print func_b(2) 2
print func_c(func_a) None
More examples of variable scope
§ inside a function, can access a variable defined outside
def f(y):
x = 1
x += 1
print(x)

x = 5
f(x)
print(x)
More examples of variable scope
§ inside a function, can use global variables, but not recommended
def g(y):
print(x)
print(x + 1)

x = 5
g(x)
print(x)
More examples of variable scope
§ inside a function, cannot modify a variable defined outside

def g(y):
x += 1
print(x, y)

x = 5
g(x)
print(x) UnboundLocalError: local variable
'x' referenced before assignment
Exercise
• Write a function to check whether a number is part of Fibonacci
sequence or not.
• Hint:
• You can use another function to generate a Fibonacci Sequence

You might also like