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

03 Functions Part1

This document provides an overview of functions, parameter passing, and return statements in Python. It discusses how functions can take parameters and pass arguments, and how variables behave locally within functions versus globally. The key points covered are: defining functions with parameters; the scope of local variables; sharing data across functions through parameters; and a process for completely designing functions with signatures, purposes, examples, definitions, and verification.

Uploaded by

Anubis Mage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

03 Functions Part1

This document provides an overview of functions, parameter passing, and return statements in Python. It discusses how functions can take parameters and pass arguments, and how variables behave locally within functions versus globally. The key points covered are: defining functions with parameters; the scope of local variables; sharing data across functions through parameters; and a process for completely designing functions with signatures, purposes, examples, definitions, and verification.

Uploaded by

Anubis Mage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit 03:

Functions: Parameter Passing


and Return Statements
Anthony Estey
CSC 110: Fundamentals of Programming: I
University of Victoria
Unit 03 Overview

u Reading:
u Textbook: 5.1 – 5.8
u Learning Objectives: (You should be able to…)
u perform a complete function design, including documentation and test cases
u pass arguments to a function
u return values from a function
u differentiate between local and global variables
u identify the scope of a variable

2
What we know already…

u We have used the print() function since the first lecture


u When we call the print function, we pass an argument representing
the information we want to be displayed
u Example: print("Anthony") and print(num)
u Arguments: "Anthony" the first time, num the second

u We have also defined and called our own functions:


def print_greeting(): Call: print_greeting()
print("Welcome to CSC110")
print("We are programming")

3
Local variables and scope

u A local variable is when we assign a variable a value within a function:


u that variable belongs to the function it was assigned in
u it can only be accessed within that function (by statements that are local to
that function) after it has been assigned
u different functions may have local variables with the same name, but they are
completely independent of each other
u it is an error if a different function attempts to access the variable

u A variable’s scope are the parts of the program in which the variable
can be accessed
u for a local variable: its scope begins when the variable is assigned, and
continues through to the end of its indented section
u variable-scope.py
4
variable-scope.py

def fn1():
x = 5
scope of x (it can only be accessed in fn1()
print(x)

def fn2():
print(x)

def main():
fn1()
fn2()

5
variable-scope.py
def fn1():
x = 5
print(x)
fn1() has a local variable x

def fn2():
fn2() has a local variable x, which is
x = 3
handled completely independently from the
print(x) x in fn1()

def main():
fn1() neither x can be accessed from main
fn2()
6
Sharing information across functions

u Sometimes we want our functions to work independently…


u but sometimes we also want to share data across functions!

u We have seen that the print() function can be given an argument


u We can also define our functions to accept input data

u Terminology is confusing:
u from the point of view of the function call, these are called arguments
u from the point of view of the function definition, they are called parameters

7
Defining functions that take a parameter

u A parameter is a variable in a function definition

u General form: u Concrete example:

def fn_name(param_name): def print_val(val):


tab statement1 print(val)
tab statement2
tab … print_val(5) à 5
print_val(22) à 22

8
Parameter passing and scope

u The scope of a parameter variable is the same as that of a local


variable in a function

def do_some_math(x):
y = 3
if (x < y): scope of x and y
z = 2 * x is the whole
scope of z
print("z is:", z) function body
print("done")

9
Parameter Passing complexities

u We can add any number of parameters, with multiple types:

# (str, float -> None)


# (int, int -> None) # prints transcript information for
# prints product of x*y # given student and gpa
def multiply(x, y): def transcript(name, gpa):
result = x * y print("Name:", name)
print(result) print("GPA:", gpa)

multiply(2,3) à 6 transcript("Anthony", 5.8)


multiply(9,4) à 36 transcript("Sam", 7.8)
multiply(0,13) à 0 transcript("Alix", 8.2)

10
This can get complicated!

u Remember, we want our functions to:


u work correctly (produce the intended result)
u be easy to read and understand, so…
u they are easy to use (know how to call them and how they should work)
u they are easy to extend or update in the future

u It is important to come up with a process we can follow when we


design our functions this semester
u This will be our “house style” talked about in the first lecture

11
Steps in a complete function design

1. Signature:
u The number and types of the arguments passed to the function
2. Purpose:
u A short explanation of what the function does
3. Examples/Tests:
u Provide some example values that we would call the function with…
u … and what you expect the function to produce given these values
4. Definition
u write the function definition so work as intended
5. Verification
u Run your code and ensure the examples produce the results you intended
12
Complete function design example
# (int -> None)
1.Signature
# prints out double the given number
def double(num): 2.Purpose
print(num*2)
3.Tests
def main(): 4.Definition
double(3) # expects 6
double(7) # expects 14 5.Then run it!

main()

u Try it yourself: Design a function that prints out whether a given


number is even or odd
13

You might also like