Study Guide - Chapter 4 void Functions ITP 134 C++ Study Guide Instructions: Use these notes to give you the important points of this chapter. See the book for lots of great programming examples. Important concepts, keywords, statements and functions are shown in emphasized font. Programming code syntax and examples are shown in code font. Chapter 4 void Functions 4.1 Modularizing a Program with Functions There are two general types of functions: those that return a value, and those that do not. In C++, a function that does not return a value is known as a void function. In this chapter, we will demonstrate how void functions can be used to break down a long program into smaller, manageable pieces. This approach is sometimes called divide and conquer because a large program is divided into several smaller programs which are easily solved. (pg 139) CONCEPT: A large program can be broken up into manageable functions that each performs a major task. (pg 139) Benefits of Using Functions (pages 140 – 141) Simpler code Code reuse Better testing Faster development Easier facilitation of teamwork 4.2 Defining and Calling a void Function CONCEPT: The code for a function is called a function definition. To execute a function you write a statement that calls it. (pg 141) Function Names C++ rules for naming a function: (pg 142) Must be 1 word. Cannot contain spaces. First character must be a-z, A-Z, or _ (underscore) After first character can also be a number, but cannot be a special character such as $. Names are case sensitive. ITP 134 – Chapter 4 Mrs. Eaton Defining a void Function – Function Definition To create a function you write the function definition. This specifies what a function does, but it does NOT cause the function to execute. The first line in a function definition is known as the function header. (page 142) Notice that the function header does NOT end with a semicolon This is the general syntax of a void function definition: void functionName() { statement1; statement2; } Calling a Function To execute a function, you must call it. When a function is called, the program jumps to the function definition code and executes it. After the function ends, the programs jumps back to the statement that called it and continues executing with the very next statement. Notice that you do NOT write the word void in the function call statement. The general form of a call statement is: (pg 143) functionName(); Function Prototypes A function prototype is a statement that declares the existence of a function, but does not define the Page 1 Study Guide - Chapter 4 void Functions function. It tells the C++ compiler that a particular function exists, and the definition appears later in the program. (pg 145 – 147). Eaton note: This is also called a function declaration in some books and languages. The general syntax of a function prototype is: void functionName(); 4.3 Designing a Program to Use Functions CONCEPT: Programmers commonly use a technique known as top-down design to break down a program into functions that each performs a single task. (pg 148) Top-down design process (pg 148- 149) 1. Break down the overall task into a series of subtasks. 2. Determine if each subtask can be further broken down into more subtasks. Repeat this step until no more subtasks can be identified. 3. Once you identify all the subtasks, then write the code. Hierarchy Charts Programmers commonly use hierarchy charts to visually represent the relationships between functions in a program. This is also known as a structure chart, and shows boxes that represent each function. The boxes are connected to show how the functions are called. (pg 150-151) See Figure 4-8 on page 151 for an example hierarchy chart for drawing the City Skyline. 4.4 Local Variables Duplicate Variable Names You cannot have 2 different variables with the same name inside the same function. (pg 154) 4.5 Passing Arguments to Functions CONCEPT: A piece of data is an argument passed into a function when the function is called. When an argument is passed into a function, a special variable known as a parameter in the function receives the argument. (pg 155) Eaton Note: Remember that the calling statement contains arguments. Mnemonic call, call, get into a fight and argument. The function definition contains parameters. Parameter Variable Scope A parameter variable’s scope is the entire function where the parameter is declared. It is visible (and accessible) only to statements inside the function. Eaton Note: In other words a parameter is a local variable to the function definition. (pg 159) Passing Multiple Arguments If a function needs multiple arguments passed, you also need multiple parameters to receive the values passed. Eaton note: The arguments and parameters must be in the same order and the same type. (pg 160) Passing Arguments by Value When you pass an argument by value you pass only a copy of the value. Therefore if you change the value, the value is only changed inside the function. (pg 163) CONCEPT: A local variable is declared inside function and cannot be accessed by statements that are outside the function. Different functions can have local variables with the same names because the functions cannot see inside other functions for the local variables. (pg 151) Passing Arguments by Reference When you pass an argument by reference, you pass the memory location of the value (not just a copy). Therefore if you change the value, the value is changed inside the function and also in the calling program or function. (pg 165) Scope and Local Variables Scope describes the portion of the overall program where a variable may be accessed. A local variable’s scope begins at the variable’s declaration and ends at the end of the function where the variable is declared. (pg 154) 4.6 Global Variables and Constants ITP 134 – Chapter 4 Mrs. Eaton CONCEPT: A global variable is available to every function in a program, and is declared outside all the functions in a program. (pg 169) Global Constants A global constant is a named constant that is available to every function in a program. Global constants are Page 2 Study Guide - Chapter 4 void Functions typically used to represent unchanging values that are needed throughout a program. (pg 171) Programs in this Chapter 4.1 FunctionDemo.cpp (page 143) This program displays “I am Arthur, King of the Britons.” Notice the functions are given before the DarkGDK function, so function prototypes are not needed. This is not a good programming practice. This program demonstrates function calls and function definitions for 2 very simple functions: displayMessage() pauseProgram() 4.2 Prototypes.cpp (page 147) This program displays “I am Arthur, King of the Britons.” Notice the DarkGDK is the first function followed by the other function, so function prototypes are required. This is a good programming practice. This program rewrites the previous program using function prototypes. displayMessage() pauseProgram() Program 4-3 ModularCitySkyline.cpp (page 149 – 150) This program draws a city skyline. Compare this program with functions to Program 2-9 (pages 55-56) which draws the same drawing without using functions. The functions are: drawBuildings() drawWindows() drawStars() Program 4-4 ErrorLocalVars.cpp (page 152) This program should draw 2 boxes, but it has errors. This program has errors on lines 34 and 46 before you are trying to use local variables outside the functions where they are declared. The next program corrects these errors. Program 4-5 LocalVars.cpp (page 153) This program draws 2 boxes. This program demonstrates the correct use of local variables and fixes the errors from the previous program. ITP 134 – Chapter 4 Mrs. Eaton Program 4-6 CenterCircle.cpp (page 156) This program draws a circle in the center of the screen. This program demonstrates passing the radius of the circle as arguments or parameters by value. Passes a copy. Program 4-7 ConcentricCircles.cpp (page 158) This program is the same as the previous expect the function drawCircle is called 4 times. Program 4-8 Sum.cpp (page 160) This program calculates the sum of 2 numbers. This program demonstrates passing 2 arguments or parameters by value. Passes a copy. Program 4-9 RandomBricks.cpp (page 162-163) This program draws random bricks. This program demonstrates calling the drawBrick function 3 times and passing 2 arguments or parameters x and y. Program 4-10 PassByValue.cpp (page 164) This program demonstrates passing arguments or parameters by value. The value does not change in the calling program because you are passing a copy which you pass by value. Program 4-11 PassByReference(pages 166 – 167) This program prints some numbers. This program demonstates pass by reference and shows how the numbers also change in the calling program because you are passing a memory location not just a copy. Program 4-12 WindowSizes.cpp (page 168) This program prints the height and width of the program window. It is supposed to demonstrate passing by reference, but it is a terrible example because the arguments are not initialized before they are passed. Program 4-13 GlobalVar.cpp (page 169) This program prompts the user for a number and then displays it. Page 3 Study Guide - Chapter 4 void Functions The program demonstrates a global variable called number. Program 4-14 ItalianFlag3.cpp (page 171-173) This program draws the Italian flag. This program demonstrates using global constants: GREEN, WHITE, RED and BLACK. ITP 134 – Chapter 4 Mrs. Eaton This program also demonstrates the use of functions. Compare this program to to Program 3-17 ItalianFlag2.cpp on page 130 that draws the same image. Page 4