8.1. Introduction: Functions Part II

You already learned how to create basic functions in Chapter 4. Functions are nice because we can call them repeatedly to execute the same set of code over and over again. In this chapter we learn how to make functions more powerful, by passing information in and out of them. Paramters allow us to pass information to functions, and return values allow us to get information from functions.

We’ve played a lot with turtles, and in Chapter 6 we created two different turtle functions to have a turtle draw a small square and a big square repeatedly to make a flower image:

With the use of parameters, we can simplify this code so that only one function is needed, and so that it will work for any turtle, not just a turtle named alex:

In this chapter we will explain all the details of how to construct and call functions with parameters, as well as learn about ways to return information from functions, and the side effects that functions can have. Really what we are focusing on here is where information is stored (locally in the function or globally in the program) and how it moves around in the program while the program is executing.

8.1.1. Topics

  • passing information to and from functions

  • local and global scope

  • documenting functions

  • side effects

8.1.2. Learning Objectives

At the end of this chapter, you should be able to:

  • identify formal parameters and parameter values in a code sample

  • predict the return value of a function given sample parameter values

  • define functions with appropriate names for formal parameters

  • use type annotations to specify expected parameter and return types

  • avoid the use of global variables in function definitions by creating formal parameters for all values that are needed

  • identify whether a function has any side effects

You have attempted of activities on this page