Python: Function Basics - Introduction

Python: Function Basics - Introduction ? In Python, a function is a named block of code that performs a specific task ? We have seen a number of Python functions already:

x = sqrt(15) y = int('75') z = eval(input('Enter an integer between 10 and 20)) ? When a function is invoked, it is said to be called

? Python code calls a function ? The call causes the function to perform its given task ? The values transmitted to the function are called the arguments (parameters, or more properly actual parameters) ? These are processed by the function ? Some functions take a fixed number of parameters

E.g., sqrt() takes exactly one parameter ? Some functions take a variable number of parameters, with a limit

E.g., string.find() may take up to three parameters, which must appear in a fixed order. ? Some functions take a variable number of parameters, with no limit E.g., print() ? Most functions produce a result ? The result is said to be returned by the function ? This value can be assigned, used in an expression, etc. ? Python allows the programmer to create his/her own functions

1

Python: Function Basics - Introduction (2)

? Functions are useful in many ways ? They facilitate writing a program Earlier, we discussed top-down design Functions can be used to structure a program so that it reflects the structure of the top-down design ? They make a program modular Each function is self-contained Each acts like a small program in itself (in fact, they are sometimes called subprograms) They help divide a program into functional units that reflect program design ? They make a program easier to read For example, when the sqrt() function is called, we have no idea how it produces its result These details are hidden from us; sqrt() acts like a black box All we care about is that it produces the correct answer for the parameter passed to it By hiding these details, the program is easier to understand ? They make a program easier to debug Since functions are modular and self-contained, it is easier to trace and isolate errors to a specific function ? They make a program easier to write If certain processing is performed several times in a program, a single function can be written that is called each time that processing is needed, rather than duplicating the required code many times throughout the program

? When working with functions, there are two aspects to consider: 1. Definition 2. Call

2

? Syntax:

Python: Function Basics - Definition

? function-name is an identifier The name should indicate what task the function accomplishes

? the parameters are identifiers They are the means by which information passes into the function These will be discussed below

? The first line of the definition is called the function header It contains all the information needed for a program to use it

? The block is called the body of the function These statements do the work of the function

? A simple example:

def print_header (): print('*'* 40) print('*' + ' ' * 38 + '*') print('*{:^38}*'.format('John Doe')) print('*' + ' ' * 38 + '*') print('*'* 40)

3

? Syntax:

C: Function Basics - Calls

? The example function above would be called using print header()

? Semantics (abbreviated) 1. Control passes to the body of the function 2. The function body is executed 3. Control passes to the point immediately after the call ? This is called a return

? Local variables are variables used in the body of the function ? They exist only as long as the function is being executed

? Revised function call semantics (1): 1. An activation record is created for the function ? Storage is allocated for local variables 2. The function body is executed 3. The AR is destroyed 4. The function returns

4

C: Function Basics - Calls (2) ? Example:

def print_header (): columns = 40 print('*'* columns) print('*' + ' ' * (columns - 2) + '*') print('*{:^{}}*'.format('John Doe', (columns - 2))) print('*' + ' ' * (columns - 2) + '*') print('*'* columns)

#call print_header()

5

Python: Function Basics - Parameters ? The examples presented thus far do exactly one thing the same way every time

they are called ? Parameters allow a function to produce different results each time it is called

? General terminology ? Parameters/formal parameters generally refer to the identifiers used in the definition ? Arguments/parameters generally refer to the expressions used in the call

? Revised function call semantics (2): 1. An activation record is created for the function ? Storage is allocated for the parameters ? Storage is allocated for local variables 2. Values of the arguments are copied into the storage allocated for the parameters 3. The function body is executed 4. The AR is destroyed 5. The function returns

6

Python: Function Basics - Parameters (2) ? Example:

def print_header (columns): print('*'* columns) print('*' + ' ' * (columns - 2) + '*') print('*{:^{}}*'.format('John Doe', (columns - 2))) print('*' + ' ' * (columns - 2) + '*') print('*'* columns)

#calls n = 40 print_header(n) w = 80 print_header(w)

7

Python: Function Basics - Parameters (3)

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download