CSC 101 – Introduction
to Computing
Functions can be used to
Introduction define reusable code and
organize and simplify
code.
Suppose that you need to find the sum of integers from 1 to
10, 20 to 37, and 35 to 49.
If you create a program to add these three sets of numbers,
your code might look like this:
2
Functions can be used to
Introduction define reusable code to
organize and simplify
code.
The preceding code can be simplified by using functions, as follows:
3
A function definition
Defining a consists of the function’s
Function name, parameters, and
body.
The syntax for defining a function is as follows:
def functionName(list of parameters)
# Function body
4
A function definition
Defining a consists of the function’s
Function name, parameters, and
body.
A function contains a header and body.
The header begins with the def keyword, followed by the
function’s name and parameters, and ends with a colon.
The variables in the function header are known as formal
parameters or simply parameters.
A parameter is like a placeholder: When a function is
invoked, you pass a value to the parameter.
▪ This value is referred to as an actual parameter or argument.
▪ Parameters are optional; that is, a function may not have any
parameters.
▪ For example, the random.random() function has no parameters.
Some functions return a value, while other functions perform
desired operations without returning a value. If a function
returns a value, it is called a value-returning function.
5
Calling a function
Calling a Function executes the code in
the function.
In a function’s definition, you define what it is to
do.
To use a function, you have to call or invoke it.
The program that calls the function is called a
caller.
There are two ways to call a function, depending
on whether or not it returns a value.
6
Calling a function
Calling a Function executes the code in
the function.
If the function returns a value, a call to that function
is usually treated as a value.
For example,
larger = max(3, 4)
calls max(3, 4) and assigns the result of the function to the
variable larger.
Another example of a call that is treated as a value is
print(max(3, 4))
which prints the return value of the function call max(3, 4).
If a function does not return a value, the call to the
function must be a statement.
For example, the print function does not return a value. The
following call is a statement:
print("Programming is fun!")
7
8
9
Functions with/without
Return Values
A function does not have to return a value.
Such a function is commonly known as a void function in programming
terminology.
10
Modularizing makes code
Modularizin easy to maintain and
g Code debug, and enables the
code to be reused.
Functions can be used to reduce redundant code and
enable code reuse.
Functions can also be used to modularize code and
improve a program’s quality.
In Python, you can place the function definition into a file
called module with the file-name extension .py.
The module can be later imported into a program for reuse.
The module file should be placed in the same directory with your
other programs.
A module can contain more than one function.
▪ Each function in a module must have a different name.
random and math are the modules defined in the
Python library, and thus they can be imported into any
Python program.
11
12
The scope of a variable is
The Scope of the part of the program
Variables where the variable can be
referenced.
A variable created inside a function is referred to
as a local variable.
Local variables can only be accessed within a function.
The scope of a local variable starts from its creation and
continues to the end of the function that contains the
variable.
In Python, you can also use global variables.
They are created outside all functions and are
accessible to all functions in their scope.
13
The scope of a variable is
The Scope ofthe part of the program
Variables where the variable can be
referenced.
A global variable is
created in line 1.
It is accessed within the
function in line 4 and
outside the function in
line 8.
A local variable is created
in line 3.
It is accessed within the
function in line 5.
Attempting to access the
variable from outside of
the function causes an
error in line 9.
14
Python provides many
Common Python useful functions for
Functions common programming
tasks.
A function is a group of statements that
performs a specific task.
Python, as well as other programming
languages, provides a library of functions.
You have already used the functions eval,
input, print, and int.
These are built-in functions and they are
always available in the Python interpreter.
15
Common Python
Functions
16
Common Python Functions
''' Simple Python builtin functions
'''
num1, num2, num3 = -3, 16, 7.2
absNum = abs(num1) # Returns the absolute
value
print ("Absolute value = ",absNum)
maxNum = max(num1, num2, num3) # Returns the
maximum number
print("Maximum number = ", maxNum)
numPow = pow(num2, num3) # Same as num2 **
num3
print("numPow = ", numPow)
numR = round(num3) # Rounds to its nearest
integer
numRd = round(3.1456, 3) # Rounds to 3 digits after 17
18
Mathematical Functions
import math # import math module to use the
math functions
#Test algebraic functions
print("log10(10, 10) =", math.log(10, 10))
print("sqrt(4.0) =", math.sqrt(4))
# Test trigonometric functions
print("tan(PI / 2) =", math.sin(math.pi/2))
print("degrees(1.57) =", math.degrees(1.57))
print("radians(90) =", math.radians(90))
19
Positional and Keyword
Arguments
A function’s arguments can be
passed as positional arguments or
keyword arguments.nPrintln('a', 3)
nPrintln(3, 'a')
nPrintln(n = 5, message = "good")
def f(p1, p2, p3):
f(30, p2 = 4, 10)
f(30, p2 = 4, p3 = 10)