Chapter 5 - Functions in Python
Chapter 5: Functions
In this chapter, the student first learns how to write and call void functions. The chapter shows the benefits of using functions to modularize programs and discusses the top-down design approach. Then, the student learns to pass arguments to functions. Common library functions, such as those for generating random numbers, are discussed. After learning how to call library functions and use their return value, the student learns to define and call his or her own functions. Then the student learns how to use modules to organize functions.
Chapter 5 Learning Outcomes
Upon completion of this week's material, you should be able to:
- describe how to create Functions in a Python program and the meaning of the parts of a function header.
- describe how to pass values from one Function to another and describe a parameter list.
- describe the concept of designing a program to use Functions.
- describe the concept local variables and global variables.
- Create your own Value-Returning functions.
- describe storing Functions in a module.
- describe how to use the math and random Modules.
Highlights This Week
In chapter 4 we examined the logical structure of algorithms and focused on implementing repetition execution to yield true or false results. These relational operations have been referred to as logical expressions. Logical expressions use comparative operators to compare the values of data in computer memory, finally, the chapter examined logical operations that combine values in Boolean expressions to form more complex expressions. The logical operators that we have been discussing have been the (and, or, not) logical operators. These items have overall examined the nature of algorithms and the structure of algorithm development in programming.
In Chapter 4, we examined another significant component of the logical structure of an algorithm in a computer program, the repetition sequence. In computer science and Computer Technology, the repetition sequence is also referred to as a loop. Loops provide the means of repeating a sequence of computer instructions multiple times.
The Fundamental components of Repetition Sequences are the Initialization process, the Testing process, the Updating process, and the Process that is being performed repetitively.
In the case of the pre-test loop, the loop control variable has to set to a value that permits the loop to be entered and executed. A post-test loop will execute at least one time before a test is performed to determine f the loop should be repeated additional times after the first iteration. Just as in the selection sequences a looping sequence can have multiple logical expressions to determine the overall outcome of a complex logical expression. The logical operators (and, or, not) are used to join multiple logical expressions together to form a complex logical expression. Chapter 4 attempted to describe the differences between sentinel control variables and count control variable.
This week in Chapter 5 we are covering New Material Functions.
The primary focus of this week is understanding user-defined functions in Python. Chapter 5 also discusses the topic of parameter passing in Python. The text inside the parenthesis of the method definition describes the data that must be passed into the function when it is used. This consists of a series of sets of variable names declarations. These declarations are similar to declaring variables inside a function. They have the same variables scope as declaring a variable at the beginning of a function. The main difference is the variable declarations placed in the function header are used to receive data that is to be used when the function is invoked by a call to the function from another function during program execution.
Chapter 5 explains the difference between a formal parameter and an actual parameter. The formal parameter is the list of variable names in the header of the function declaration. The actual parameter is the list of data that is sent to the function when it is invoked. invoked means calling the function.
Chapter 5 explains passing a variable by value. When a variable is passed by value, a copy of the value in the variable is sent to the formal parameter variable of the called method. When a variable is passed by reference, the memory address of the object is passed to the formal parameter variable of the called method. The way that these types of passing differ is that if a value of the formal parameter variable changes when the values is pass by value, it has no effect on the actual parameter variable in the calling method. However, if a value of a property changes in the formal parameter variable of the called method, it has a direct effect on the property of the object in the actual parameter of the method call.
Chapter 5 has many concepts and topics. The main topic of the chapter is the introduction of breaking a computer program into several separate modules. These separate modules are called functions in Python. In other programming languages, they are called procedures and methods. That will be the main focus of the assignments associated with chapter 5, understanding how to take a large problem and break it into smaller components. those parts are then solved individually and then the smaller solutions are assembled into a big solution. In the chapter, this is referred to as top-down design and modular development.