0% found this document useful (0 votes)
112 views7 pages

CEN111: Programming I Lecture 3: Functions: International Burch University

The document discusses functions in programming. It defines what a function is, how to define new functions using the def keyword, and how to call functions. It explains function parameters and arguments, local variables that exist only within a function, and return values. It provides examples of built-in functions, user-defined functions, and functions that call other functions.

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)
112 views7 pages

CEN111: Programming I Lecture 3: Functions: International Burch University

The document discusses functions in programming. It defines what a function is, how to define new functions using the def keyword, and how to call functions. It explains function parameters and arguments, local variables that exist only within a function, and return values. It provides examples of built-in functions, user-defined functions, and functions that call other functions.

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

CEN111: Programming I

IBU Lecture 3: Functions


International Burch University
Lecture #3: Functions

A function is a named sequence of statements that performs a


computation
function call
argument of a function
return value

Type conversion functions


Python provides built-in functions that convert values from one type to
another.
eg: int(’1024’) will convert the string ’1024’ to an integer.
Math functions
module: file that contains a collection of related functions
math module provides most of the usual math functions
in order to use math module we import it by
import math

2/1
Composition
example:
x = math.sin(degrees / 360.0 * 2 * math.pi)
is a composition
Adding new functions
A function definition specifies the name of a new function and the
sequence of statements that execute when the function is called.
example:

def print_lyrics():
print("I’m a lumberjack, and I’m okay.")
print("I sleep all night and I work all day.")

def is a keyword that indicates that this is a function definition


print lyrics is the name of the function
the rules for function names are the same as for variable names
The empty parentheses after the name indicate that this function
doesn’t take any arguments

3/1
The syntax for calling the new function is the same as for built-in
functions:
>>> print_lyrics()
I’m a lumberjack, and I’m okay.
I sleep all night and I work all day.
Once we have defined a function, we can use it inside another function.
For example, to repeat the previous refrain, we could write a function
called repeat lyrics:
def repeat_lyrics():
print_lyrics()
print_lyrics()

4/1
Definitions and uses
Pulling together the code fragments from the previous section, the whole
program looks like this:
def print_lyrics():
print("I’m a lumberjack, and I’m okay.")
print("I sleep all night and I work all day.")
def repeat_lyrics():
print_lyrics()
print_lyrics()
repeat_lyrics()
Flow of execution
Execution always begins at the first statement of the program.
Statements are executed one at a time, in order from top to bottom.
Function definitions do not alter the flow of execution of the program,
but remember that statements inside the function are not executed until
the function is called.

5/1
Parameters and arguments
Inside the function, the arguments are assigned to variables called
parameters.

def print_twice(bruce):
print(bruce)
print(bruce)

This function assigns the argument to a parameter named bruce. When


the function is called, it prints the value of the parameter twice.
Local variables
When we create a variable inside a function, it is local, which means that
it only exists inside the function.For example:

def cat_twice(part1, part2):


cat = part1 + part2
print_twice(cat)

This function takes two arguments, concatenates them, and prints the
result twice. Here is an example that uses it:
6/1
>>> line1 = ’first ’
>>> line2 = ’second’
>>> cat_twice(line1, line2)
firstsecond
firstsecond

When cat twice terminates, the variable cat is destroyed. If we try to


print it, we get an error:
>>> print(cat)
NameError: name ’cat’ is not defined

Return values for functions


functions that return a value (fruitful functions)
void functions

7/1

You might also like