Read Aloud the Text Content
This audio was created by Woord's Text to Speech service by content creators from all around the world.
Text Content or SSML code:
5.1: Introduction to Functions CONCEPT: A function is a group of statements that exist within a program for the purpose of performing a specific task. In Chapter 2, we described a simple algorithm for calculating an employee’s pay. In the algorithm, the number of hours worked is multiplied by an hourly pay rate. A more realistic payroll algorithm, however, would do much more than this. In a real-world application, the overall task of calculating an employee’s pay would consist of several subtasks, such as the following: Getting the employee’s hourly pay rate Getting the number of hours worked Calculating the employee’s gross pay Calculating overtime pay Calculating withholdings for taxes and benefits Calculating the net pay Printing the paycheck Most programs perform tasks that are large enough to be broken down into several subtasks. For this reason, programmers usually break down their programs into small manageable pieces known as functions. A function is a group of statements that exist within a program for the purpose of performing a specific task. Instead of writing a large program as one long sequence of statements, it can be written as several small functions, each one performing a specific part of the task. These small functions can then be executed in the desired order to perform the overall task. This approach is sometimes called divide and conquer because a large task is divided into several smaller tasks that are easily performed. Figure 5-1 illustrates this idea by comparing two programs: one that uses a long complex sequence of statements to perform a task, and another that divides a task into smaller tasks, each of which is performed by a separate function. When using functions in a program, you generally isolate each task within the program in its own function. For example, a realistic pay calculating program might have the following functions: A function that gets the employee’s hourly pay rate A function that gets the number of hours worked A function that calculates the employee’s gross pay A function that calculates the overtime pay A function that calculates the withholdings for taxes and benefits A function that calculates the net pay A function that prints the paycheck A program that has been written with each task in its own function is called a modularized program. Figure 5-1 Using functions to divide and conquer a large task The figure consists of two parts that describe one long sequence of statements and a set of functions. Figure (1):There are twenty four lines and each line consists of the word statement. Note: This program is one long, complex sequence of statements. Figure (2): There are four rectangular sections. Each section defines a function as follows. Line 1 at indentation level 0: def function1 open round bracket close bracket colon. Line 2 at indentation level 1: statement. Line 3 at indentation level 1: statement. Line 4 at indentation level 1: statement. Line 5 at indentation level 0: def function2 open round bracket close bracket colon. Line 6 at indentation level 1: statement. Line 7 at indentation level 1: statement. Line 8 at indentation level 1: statement. Line 9 at indentation level 0: def function3 open round bracket close bracket colon. Line 10 at indentation level 1: statement. Line 11 at indentation level 1: statement. Line 12 at indentation level 1: statement. Line 13 at indentation level 0: def function4 open round bracket close bracket colon. Line 14 at indentation level 1: statement. Line 15 at indentation level 1: statement. Line 16 at indentation level 1: statement. Note: In this program the task has been divided into smaller tasks, each of which is performed by a separate function. Benefits of Modularizing a Program with Functions A program benefits in the following ways when it is broken down into functions: Simpler Code A program’s code tends to be simpler and easier to understand when it is broken down into functions. Several small functions are much easier to read than one long sequence of statements. Code Reuse Functions also reduce the duplication of code within a program. If a specific operation is performed in several places in a program, a function can be written once to perform that operation, then be executed any time it is needed. This benefit of using functions is known as code reuse because you are writing the code to perform a task once, then reusing it each time you need to perform the task. Better Testing When each task within a program is contained in its own function, testing and debugging becomes simpler. Programmers can test each function in a program individually, to determine whether it correctly performs its operation. This makes it easier to isolate and fix errors. Faster Development Suppose a programmer or a team of programmers is developing multiple programs. They discover that each of the programs perform several common tasks, such as asking for a username and a password, displaying the current time, and so on. It doesn’t make sense to write the code for these tasks multiple times. Instead, functions can be written for the commonly needed tasks, and those functions can be incorporated into each program that needs them. Easier Facilitation of Teamwork Functions also make it easier for programmers to work in teams. When a program is developed as a set of functions that each performs an individual task, then different programmers can be assigned the job of writing different functions. Void Functions and Value-Returning Functions In this chapter, you will learn to write two types of functions: void functions and value-returning functions. When you call a void function, it simply executes the statements it contains and then terminates. When you call a value-returning function, it executes the statements that it contains, then returns a value back to the statement that called it. The input function is an example of a value-returning function. When you call the input function, it gets the data that the user types on the keyboard and returns that data as a string. The int and float functions are also examples of value-returning functions. You pass an argument to the int function, and it returns that argument’s value converted to an integer. Likewise, you pass an argument to the float function, and it returns that argument’s value converted to a floating-point number. The first type of function that you will learn to write is the void function. Self-review This won't affect your score Checkpoint 5.1-1 3 tries left A ________ is a group of statements that exist within a program for the purpose of performing a specific task. blueprint paradigm protocol function Self-review This won't affect your score Checkpoint 5.1-2 3 tries left A large task is divided into several smaller tasks that are easily performed. This technique is known as _________. get and set break and build pass and process divide and conquer Self-review This won't affect your score Checkpoint 5.1-3 1 try left If a specific operation is performed in several places in a program, a function can be written once to perform that operation, and then be reused any time it is needed. True False