Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining Functions.

Similar presentations


Presentation on theme: "Defining Functions."— Presentation transcript:

1 Defining Functions

2 Today's Goal Learn to write your own functions

3 Functions Recall that a function is like a mini-program: Takes input
Performs calculations Produces result

4 Anatomy of a Function Definition
Function name Parameters A function definition consists of 1. Interface: Specifies Function name Parameters 2. Implementation Statements that perform the work def add(x, y): result = x + y return result

5 A Basic Example The drawSquare function:
takes no parameters draws a square of fixed size using the turtle created by the main program Note that the functions must be defined at the top of the program import turtle def drawSquare(): for i in range(4): t.forward(20) t.left(90) # ---- main program ---- t = turtle.Turtle() drawSquare()

6 An Example with Parameters
We want two different turtles to be able to draw a square Our new drawSquare() needs two parameters from the main program: Turtle to use to draw Length of side of square def drawSquare(t, size): for i in range(4): t.forward(size) t.left(90) # ---- main program ---- alex = turtle.Turtle() drawSquare(alex, 40) greta = turtle.Turtle() drawSquare(greta, 20)

7 More About Parameters Formal parameters Parameters in Function Definition Placeholders for values supplied by code that calls the function Called "Formal Parameters" Parameters in Calling Code Supply values to function Called "Actual Parameters" or "Arguments" When a function is called, values from the arguments are copied to the formal parameters def drawSquare(t, size): for i in range(4): t.forward(size) t.left(90) # ---- main program ---- alex = turtle.Turtle() drawSquare(alex, 40) Actual parameters (aka Arguments)

8 Returning Data A function can return a value to its caller using the return statement The return statement specifies a value to be transmitted back to the caller. When the return statement executes, the specified value is copied back to the variable in the calling assignment statement. Demo: Trace execution in CodeLens Textbook 6.2 Functions that Return Values def square(x): answer = x * x return answer result = square(10) print(result) 100

9 Returning Multiple Values
A function can return two or more values. def divide(x, y): quotient = x // y remainder = x % y return (quotient, remainder) (quo, rem) = divide(10, 3) print(quo, rem)

10 Calling Functions in Expressions
The data returned by a return statement can be used in any expression Doesn't have to be in an assignment statement def add(x, y): result = x + y return result print( add(2, 3) ) y = 5 * add(5, 10) z = add(add(2, 3), y)

11 Type Hints result = x + y return result
You can specify the data types of the parameters and the return value by adding type hints A type hint specifies the expected data type for a particular parameter or for the return type The type hints are for readers of your code They are not checked by the Python interpreter Data Type names: int, float, bool, str Parameter type hints def add(x, y): result = x + y return result def add(x: float, y: float) -> float: Return type hint

12 Practice Write a function named max that takes two numbers and returns the larger. Write a function named roundIt that takes a positive number and rounds it to the nearest integer (hint: think about adding .5 and then dropping any fractional portion). Include type hints.

13 Practice The program below finds the largest number in a list of positive numbers entered by the user. Rework the following code to use max(): largest = 0 num = 0 while num != -1: num = int(input(("Enter a number (-1 to quit): ")) if num > largest: largest = num print('The largest number was', largest) def max(x: int, y: int) -> int: if x > y: return x else: return y

14 Why Create Functions? Two key reasons: Manage complexity
Eliminate duplicate code

15 Duplicate Code Duplicate code is a Great Evil
When you copy and paste code, you copy the bugs in the code Duplicate code leads to larger, more complicated programs Practice: Create a function to eliminate the duplicate code alex.forward(30) alex.right(120) greta.forward(50) greta.right(120)

16 Managing Complexity Large programs are complicated.
To manage the complexity, we break the programs up into functions. We can Test each function separately Reason about each function separately Functions are a key mechanism that makes abstraction possible. Abstraction (n). Hiding information to manage complexity.

17 Abstraction Abstraction is one of the Big Ideas in Computer Science.
It makes digital technology possible. Without it, there would be no Smart phones Internet Digital messaging Why?


Download ppt "Defining Functions."

Similar presentations


Ads by Google