Download presentation
Presentation is loading. Please wait.
1
Functions
2
Functions You might have considered the situation where you would like to reuse a piece of code, just with a few different values. Instead of rewriting the whole code, it's much cleaner to define a function, which can then be used repeatedly. A function is a block of organized, reusable code that is used to perform a single, related action. They name pieces of code the way variables name strings and numbers.
3
Advantages Reducing duplication of code
Decomposing complex problems into simpler pieces Improving clarity of the code Reuse of code Information hiding Two types of functions: Built-in and user defined
4
Built-In Examples??
5
Function syntax
6
Defining functions A function is created with the def keyword. The statements in the block of the function must be indented. The def keyword is followed by the function name with round brackets and a colon. The indented statements form a body of the function. The function is later executed when needed. We say that we call the function. If we call a function, the statements inside the function body are executed. They are not executed until the function is called. To call a function, we specify the function name with the round brackets.
7
The header Includes the def keyword The name of the function
Any parameters the function requires Optional comment that explains what the function does Example:
8
The body Describes the procedures the function carries out Example:
Indented, just like conditional statements Example of header with comment, and the body:
9
You try it Create a function, spam, that prints the string “Eggs!” to the console. Include a comment of your choice using triple quotes
10
Call and response After defining a function, it must be called to be implemented In the previous exercise, spam() needs to be called after your code. On a new line outside of the block, call spam() to execute the code inside it.
11
How much of the code do you know?
12
Using parameters and return values
Built-in functions: Example: len() You provide a sequences, the function returns its length. User-defined functions Your own functions can also receive and return values. This allows your functions to communicate with the rest of your program
13
Receiving info through parameters
The function receives a value through a parameter. Parameters are essentially variable names inside the parenthesis of a function header. Parameters catch the values sent to the function from a function call through its arguments.
14
Below the function square is set up
n is the parameter of square. A parameter acts as a variable name for a passed in argument. How can we call square on the number 10? 10 would be the passed in argument.
15
Check out the following function, power
Its parameters are missing as well as the arguments
16
MATCH PARAMETERS AND ARGUMENTS
In the previous example, we called square with the argument 10. In this instance, the function was called. It should take two arguments, a base and an exponent, and raise the first to the power of the second. A function can require as many parameters as you’d like, but when you call the function, you should generally pass in a matching number of arguments. Open the Receive and Return Program in your chapter 6 folder!
17
Software reuse Function can be easily reused in other programs!
For ex: Since asking the user a yes or no question is common, you could use the ask_yes_or_no() function in another program without doing any extra coding How to reuse functions: One way – copy them into your new program. Better way – create your own modules and import your functions into a new program (just like you import standard python modules and use their functions) We will learn how to do this is chapter 9!!!!!!!
18
In-class practice exercise
Task: Implement a function that takes as input three variables, and returns the largest of the three. Do this without using the Python max() function! The goal of this exercise is to think about some internals that Python normally takes care of for us. Hint: All you need is some variables and if statements!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.