8
Introduction to Computing Using Py for loop / def-ining a new function Execution control structures (if, for, function call) def-ining a new function Passing and using function parameters return-ing a value from a function

Introduction to Computing Using Python for loop / def- ining a new function Execution control structures ( if, for, function call) def -ining a new

Embed Size (px)

Citation preview

Page 1: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

for loop / def-ining a new function

Execution control structures (if, for, function call) def-ining a new function Passing and using function parameters return-ing a value from a function

Page 2: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

Function calls and execution control

• A function call is another kind of statement that alters the order in which statements in a program are executed

• A function call creates a temporary ‘excursion,’ causing the sequence of execution to jump to the named function. When the function exits, execution returns to the point where the function call occurred.

• We've already seen these built-in function calls:• len(), type(), min(), max(), str(), bool(), ...

• Today we are going to learn• how to write our own functions• how to specify what parameters are passed to our functions• how to return information from functions

Page 3: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

Defining a new function

We have seen these calls to built-in functions:

• abs(), max(), len(), sum(), print()

You can define a new function using def

def iSquaredPlus10(x): result = x**2 + 10 return result

def: function definition keyword

iSquaredPlus10: name of function

x: variable name for input argument

return: specifies the value that the function returns (default: None)

>>> abs(-9)9>>> max(2, 4)4>>> lst = [2,3,4,5]>>> len(lst)4>>> sum(lst)14>>> print()

>>> def iSquaredPlus10(x):result = x**2 + 10return result

>>> f(1)11>>> f(3)19>>> f(0)10

Page 4: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

print() versus return

def iSquaredPlus10(x): result = x**2 + 10 return result

squarePlusTen = iSquaredPlus10(5)print(squarePlusTen)

def iSquaredPlus10 (x): result = x**2 + 10 print(result)

return returns the sequence of execution to the point of where the function is called and (optionally) sends a value (which can be any type) back to the function call.

Function returns value of res which can then be used in an expression

Function prints value of resbut does not return anything

print() sends text to the screen. Execution proceeds to the next line of code.

Page 5: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

Defining a new function

def <function name> (<0 or more variables>): <indented function body>

a

b

c

>>> hyp(3,4)5.0>>>

The general format of a function definition is

Let’s develop function hyp() that:• Takes two numbers as input (side lengths a and b of above right triangle ) • Returns the length of the hypotenuse c

import mathdef hyp(a, b): result = math.sqrt(a**2 + b**2) return result

Page 6: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

Exercise

>>> hello('Julie')Welcome, Julie, to the world of Python.>>>

def hello(name):

Write function hello() that:• takes a name (i.e., a string) as input• prints a personalized welcome message

Note that the function does not return anything

def hello(name): line = 'Welcome, ' + name + ', to the world of Python.’def hello(name): line = 'Welcome, ' + name + ', to the world of Python.' print(line)

Page 7: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

Exercise

>>> oddCount([4, 0, 1, -2])1>>>

def rng(lst):

Write function oddCount() that:• takes a list of numbers as input• returns the number of odd numbers in the list

def rng(lst): res = max(lst) - min(lst)

def oddCount(lst): count = 0 for i in lst: if i%2 == 1: count += 1 # same as count = count+1 return count

Page 8: Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new

Introduction to Computing Using Python

Comments and docstrings

Python programs should be documented

• So the program’s ‘mission’ is well defined• So the developer who writes/maintains the code

understands it• So the user knows what the program does

Comments

def iSquaredPlus10(x): result = x**2 + 10 # compute result return res # and return it

def f(x): 'returns x**2 + 10' res = x**2 + 10 # compute result return res # and return it

Docstring

>>> help(iSquaredPlus10)Help on function iSquaredPlus10 in module __main__:

iSquaredPlus10(x)

>>> def iSquaredPlus10 (x):'returns x**2 + 10'result = x**2 + 10return result

>>> help(iSquaredPlus10)Help on function iSquaredPlus10 in module __main__:

iSquaredPlus10(x) returns x**2 + 10