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

CEN111: Programming I: International Burch University

This document discusses functions in programming. It covers calling functions, return values, main functions, composition of functions, and boolean functions. Some key points include: 1) Functions are called by name and pass arguments in parentheses. They may or may not return a value with the return keyword. 2) Programs typically have a main function called at the start that can pass arguments. 3) Functions can call other functions to break down problems into smaller pieces. 4) Functions should have a return statement in all code paths to avoid errors. 5) Functions can return boolean values to encapsulate complex tests.

Uploaded by

lu cucu
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)
74 views

CEN111: Programming I: International Burch University

This document discusses functions in programming. It covers calling functions, return values, main functions, composition of functions, and boolean functions. Some key points include: 1) Functions are called by name and pass arguments in parentheses. They may or may not return a value with the return keyword. 2) Programs typically have a main function called at the start that can pass arguments. 3) Functions can call other functions to break down problems into smaller pieces. 4) Functions should have a return statement in all code paths to avoid errors. 5) Functions can return boolean values to encapsulate complex tests.

Uploaded by

lu cucu
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/ 8

CEN111: Programming I

IBU Lecture 5
International Burch University
Lecture #5: Functions - Part 2

Calling Functions

Function name
Arguments passed into function
Return Value
() tells Python to execute the function
Even if a function takes no input, the brackets are still required
Some functions do not return a value – these are called void
Function should return a value with the keyword return
After return, Python jumps out of the function and back to the
program

2/1
Main Function main()
It is both common and a good idea to use a main function in your
programs
This is usually the starting point of a program and is run by typing:
main()
This simplifies rerunning programs and as well as passing input
values
Return values
example:
def area(radius):
temp = math.pi * radius**2
return temp
In this example the function area returns the area of a circle with the
given radius
return expression means the following:
“Return immediately from this function and use the following
expression as a return value.”
we can rewrite this function as follows
def area(radius):
return math.pi * radius**2 3/1
example: Write a function that returns the absolute value of a number
def absolute_value(x):
if x < 0:
return -x
else:
return x
This function has 2 return statements.
Since these return statements are in an alternative conditional, only one
will be executed.
As soon as a return statement executes, the function terminates without
executing any subsequent statements.
Code that appears after a return statement, or any other place the flow
of execution can never reach, is called dead code.

4/1
When writing functions with multiple paths (ie when using conditionals)
we should make sure that every possible path through the program hits a
return statement. For example:

def absolute_value(x):
if x < 0:
return -x
if x > 0:
return x

This function is incorrect because if x happens to be 0, neither condition


is true, and the function ends without hitting a return statement.

5/1
Composition
We can call one function from within another. This ability is called
composition.
example 1: Write a function that computes the distance between 2
points (x1 , y1 ) and (x2 , y2 )

def distance(x1, y1, x2, y2):


dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = math.sqrt(dsquared)
return result

example 2: Write a function that takes two points, the center of the
circle and a point on the perimeter, and computes the area of the circle.
Assume that the center point is stored in the variables xc and yc, and the
perimeter point is in xp and yp. The first step is to find the radius of the
circle, which is the distance between the two points. We just wrote a
function, distance, that does that.

6/1
We also wrote the function for the area of a circle with a given radius

def area(radius):
return math.pi * radius**2

Encapsulating these steps in a function, we get:


def circle_area(xc, yc, xp, yp):
radius = distance(xc, yc, xp, yp)
result = area(radius)
return result
or we can make it more concise by composing the function calls:
def circle_area(xc, yc, xp, yp):
return area(distance(xc, yc, xp, yp))

7/1
Boolean functions
Functions can return booleans, which is often convenient for hiding
complicated tests inside functions. For example:

def is_divisible(x, y):


if x % y == 0:
return True
else:
return False

We can use this function as in


>>> is_divisible(6, 4)
False
>>> is_divisible(6, 3)
True

and also in conditional statemens:

if is_divisible(x, y):
print ("x is divisible by y")
8/1

You might also like