Download presentation
Presentation is loading. Please wait.
1
© 2010 David A Watt, University of Glasgow
Accelerated Programming 2 Part I: Python Programming 2 Functions © 2010 David A Watt, University of Glasgow
2
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
3
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)
4
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
5
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
6
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.
7
To define a new function:
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.
8
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.
9
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 while n < q: n = n print n, n**2
10
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:
11
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.
12
Example: geometric program (2)
We could implement this directly: pi = 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.
13
Example: geometric program (3)
Better, define functions to compute the circumference and area of a circle: pi = 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)
14
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.
15
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 return-statement, 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!
16
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
17
The body of a function must be indented w.r.t. the function heading:
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.
18
In each case, the indentation shows the extent of the statement.
Similarly, the body of an if- or while- or for-statement must be indented w.r.t. the statement header: if …: while …: for …: … … … … … … … … … … … … else: … … … … In each case, the indentation shows the extent of the statement.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.