02.functions - University of Glasgow

Download Report

Transcript 02.functions - University of Glasgow

Accelerated Programming 2
Part I: Python Programming
2
Functions
© 2010 David A Watt, University of Glasgow
Built-in functions (1)
 Python provides a variety of “built-in” functions.
 Arithmetic functions:
abs(x)
returns the absolute value of x
max(x,y)
returns the maximum of x and y
min(x,y)
returns the minimum of x and y
round(x)
returns the value of x rounded to the
nearest integer
2-2
Built-in functions (2)
 Input functions:
raw_input(p)
displays prompt p, awaits user input,
then returns the input as a string
input(p)
displays prompt p, awaits user input,
then returns the input value
(typically a number)
2-3
Example: using built-in functions (1)
 Program:
n1 = input('Enter a number: ')
n2 = input('Enter another number: ')
m = max(n1, n2)
print 'The larger number is', m
 Program’s output and input:
Enter a number: 48
Enter another number: 63
The larger number is 63
2-4
Example: using built-in functions (2)
 Program:
name = raw_input('Enter your name: ')
print 'Hullo', name
 Program’s output and input:
Enter your name: David
Hullo David
2-5
Function calls
 A function call is an expression that invokes a
named function, passing argument(s). E.g.:
abs(10-n)
max(n1, n2)
input('Enter a number: ')
 The function call contains sub-expression(s)
enclosed in parentheses. These sub-expressions
are evaluated to determine the argument(s),
which are passed to the named function.
2-6
Defined functions
 To define a new function:
– Specify the function. Decide what argument(s) the
function will accept, and what the function is supposed
to do.
– Choose a name for the function. It is good practice to
choose a meaningful name that reflects what the
function is supposed to do.
– The function needs parameter(s) – one for each
argument. Choose a name for each parameter.
– Implement the function. Its header contains the name
of the function and the name(s) of its parameter(s). Its
body is a block of statements to do what the function is
supposed to do.
2-7
Example: defining a function (1)
 Suppose that we need a function to print
consecutive integers and their squares.
 The program of §1 did this for integers 2, 3, 4.
Let us generalize this to integers p, …, q.
 Specification:
– The function will have two integer arguments, p and q.
It will print each integer in the range p…q together with
its square.
 Let us name the function print_squares. It will
have two parameters, named p and q.
2-8
Example: defining a function (2)
 Implementation of the function:
def print_squares (p, q):
# Print each integer in the range p … q
# together with its square.
n = p-1
while n < q:
n = n+1
print n, n**2
2-9
Example: defining a function (3)
 The following program calls the function:
print_squares(2, 4)
print '****'
 The function call “print_squares(2, 4)”:
– evaluates the arguments, here 2 and 4 respectively
– assigns these arguments to the parameters, p and q
respectively
– executes the function body of print_squares, thus
producing the following output:
2 4
3 9
4 16
2-10
Example: geometric program (1)
 Let us develop a program that:
– reads the radii of two circles
– displays the circumference and area of each circle.
 The relevant formulae are:
circumference = 2 π r
area = π r2
 Clearly the program must compute the
circumference and area using these formulae.
2-11
Example: geometric program (2)
 We could implement this directly:
pi = 3.14159
r1 = input('Radius of circle: ')
print 'Circumference is', 2 * pi * r1
print 'Area is', pi * (r1 ** 2)
r2 = input('Radius of another circle: ')
print 'Circumference is', 2 * pi * r2
print 'Area is', pi * (r2 ** 2)
 But this program:
– is not very readable.
– contains duplicated code.
2-12
Example: geometric program (3)
 Better, define functions to compute the
circumference and area of a circle:
pi = 3.14159
def circum (r):
return 2 * pi * r
def area (r):
return pi * (r ** 2)
r1 = input('Radius of circle: ')
print 'Circumference is', circum(r1)
print 'Area is', area(r1)
r2 = input('Radius of circle: ')
print 'Circumference is', circum(r2)
print 'Area is', area(r2)
2-13
Functions and procedures
 Most Python functions return results. E.g.,
circum
area
– Python functions that return results, but have no other
effects, are like mathematical functions.
 Some Python functions do not return results.
They only perform effects such as input/output.
E.g.:
print_squares
– These are called procedures in some programming
languages.
2-14
Return-statements
 If a function is to return a result, the function
body must execute a return-statement.
 If the function body does not execute a returnstatement, it returns a special value None.
– This will cause the program to fail if the function call
expects (say) a number or string to be returned!
2-15
Example: returning a result
 If abs were not a built-in function, we could
define it ourselves:
def abs (x):
# Return the absolute value of the number x.
if x < 0:
return –x
else:
return x
2-16
Indentation (1)
 The body of a function must be indented w.r.t.
the function heading:
def f
…
…
…
(…):
…
…
…
function body
 The indentation shows the extent of the function
body.
 Standard practice is to indent by 4 spaces
consistently.
2-17
Indentation (2)
 Similarly, the body of an if- or while- or forstatement must be indented w.r.t. the statement
header:
if …:
…
…
else:
…
…
…
…
while …:
… …
… …
for …:
… …
… …
…
…
 In each case, the indentation shows the extent of
the statement.
2-18