LAB: INTRODUCTION TO FUNCTIONS IN C++

Similar documents
C++ Programming: From Problem Analysis to Program Design, Third Edition

LAB: WHILE LOOPS IN C++

Expressions, Input, Output and Data Type Conversions

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

LAB: FOR LOOPS IN C++

Objectives. In this chapter, you will:

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Lab Instructor : Jean Lai

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Binomial pricer (1.1)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Chapter 2 Basic Elements of C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Full file at

Chapter 2. Outline. Simple C++ Programs

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Chapter 3 Problem Solving and the Computer

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Computer Programming : C++

Function Call Example

download instant at Introduction to C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

REVIEW. The C++ Programming Language. CS 151 Review #2

1. In C++, reserved words are the same as predefined identifiers. a. True

Fundamentals of Programming CS-110. Lecture 2

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lab # 02. Basic Elements of C++ _ Part1

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

Programming for Engineers Introduction to C

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

VARIABLES & ASSIGNMENTS

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Lecture 3 Tao Wang 1

2 nd Week Lecture Notes

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Introduction to the C++ Programming Language

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Program Organization and Comments

In this chapter you ll learn:

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

The C++ Language. Arizona State University 1

Honors Computer Science C++ Mr. Clausen Program 4A, 4B, 4C (4D, 4G)

Engineering Problem Solving with C++, Etter/Ingber

Understanding main() function Input/Output Streams

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Honors Computer Science C++ Mr. Clausen Program 6A, 6B, 6C, & 6G

Special Section: Building Your Own Compiler

Chapter 1 INTRODUCTION

Computer Programming

UNIT- 3 Introduction to C++

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSc Introduction to Computing

Cpt S 122 Data Structures. Introduction to C++ Part II

Lecture 2 Tao Wang 1

BITG 1233: Introduction to C++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

Department of Computer and Mathematical Sciences. Lab 10: Functions. CS 1410 Intro to Computer Science with C++

Chapter 3 - Functions

Honors Computer Science C++ Mr. Clausen Program 3A, 3B, 3C

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

4. Java Project Design, Input Methods

Microsoft Excel Level 2

Add Subtract Multiply Divide

Introductionto C++ Programming

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Chapter 6 Introduction to Defining Classes

EE 109 Lab 8a Conversion Experience

Full file at C How to Program, 6/e Multiple Choice Test Bank

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

LAB: STRUCTURES, ARRAYS,

Question 2. [5 points] Given the following symbolic constant definition

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Unit 1 Lesson 4. Introduction to Control Statements

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Exercise: Inventing Language

Computer Science & Engineering 150A Problem Solving Using Computers

Chapter 1 - What s in a program?

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Introduction to C ++

Transcription:

LAB: INTRODUCTION TO FUNCTIONS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 2 Introduction This lab will provide students with an introduction to the fundamentals of user-defined functions in C++. The focus of this lab is on how to solve simple problems using C++ data types, variables, and arithmetic operations. At the end of this module, students will Be able to solve simple problems using C++ Understand the structure of a simple C++ program Understand the basic C++ data types Understand and apply the basic concepts behind C++ variables Understand and be able to apply simple arithmetic expressions using C++ variables Understand and apply the basic concepts behind C++ arithmetic operators Understand and be able to apply simple C++ arithmetic expressions These exercises have two different types of answers. Some questions ask for answers to specific questions; you should collect these answers in a Microsoft Word document. Other exercises ask you to write code; for these exercises, structure your storage drive in the following manner. There should be a palms folder, and within that folder there should be a LabFunctions folder. All coding exercises (and related folders) for this lesson should be placed in the LabFunctions folder. Let s begin! Part 1: Programming With Functions in C++ We've said before that C++ code is broken down into small code segments called functions. These functions, when combined, form a program. Until now we have only dealt with one function called main; however, we can define our own functions to make them easier to read and to maintain. In addition, C++ provides us with a number of useful functions to perform common tasks, like finding square roots of numbers and converting between different numerical representations. In this lab, we will learn how to use some of the built-in C++ functions to simplify our programs, as well as learn how to create our own. In the future, our programs will grow to include a larger number of functions. Functions in C++ Functions are defined as a unit of computational abstraction. In programming, as in life, very often we want to ignore the small details and concentrate on the big picture. Functions allow us to do just that, by allowing us to use previously written code to perform a task without worrying about how the task is performed. For instance, in the following two lines of code:

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 3 sum = pow(side1, 2) + pow(side2, 2); side3 = sqrt(sum); This code uses two functions that C++ provides in the <cmath> standard library. The first line uses the pow function to compute the result of raising a number to a power (in this case, the value of side1 raised to the second power). We don't know how the function works, all we know is that it will do a certain task and give us a result we desire. Likewise, the second line uses sqrt to compute the square root of a given number (in this case, the value of sum). When calling a function in C++, the function name is written first, followed by the inputs in parentheses. For example, suppose the variable sum contained the value 9. The function call or invocation sqrt(sum) would return the value 3. Similarly, the call sqrt(25) would return the value 5, and pow(3, 2) would return the value 3 2 = 9. A function call can appear anywhere in a C++ expression. When an expression containing a function call is evaluated, the return value for that call is substituted into the expression. For example, in the assignment "value = sqrt(4);", the call on the right-hand side would evaluate to 2, and so the variable value would be assigned the value 2. In order to use a function effectively, we need to know the type of data that is returned by the function. We can only use C++ functions in expressions where data of the same type as the function's return type is needed. For instance, the following code would not make sense: char my_char; my_char = sqrt(30); Because sqrt returns a real number, it does not make sense to try and place that result into a variable of type character. Watch and listen to the What is a C++ Function? podcast before proceeding:

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 4 Part 2: User-Defined Functions Until now we have only used functions that were either (a) required by the language, or (b) provided by the C++ Standard Library of functions. The standard library functions help to simplify the programmer's task in two major ways. First, functions help to minimize the amount of detail of which the programmer must keep track. Since the sqrt function is available, the programmer does not need to remember the sequence of steps involved in computing the square root. He or she only needs to remember how to call the sqrt function. Second, functions help to minimize the size and complexity of code. Once the sqrt function has been defined, a single call to this function will suffice to compute a square root. Since the number of steps required to otherwise compute the square root would be considerable, this can represent a significant savings in the complexity of code. The C++ Standard Library is designed to provide the programmer with a set of common tools which can be used without worrying about the nitty-gritty details of how they do their job. These predefined functions represent a collection of useful, general purpose abstractions. In addition to these, you are able to define new abstractions by defining your own functions. Any computation that you find especially useful can be encapsulated as a function. Once defined and loaded into a C++ file, your function can be called just like any predefined function. Therefore, your function may be viewed as extending the capabilities of the C++ language itself. As an example, consider the formula for converting a temperature in degrees Fahrenheit (F) to degrees Celsius (C): C = (5.0 / 9.0) * (F - 32) We can use this formula to create a C++ program. If converting temperatures was a task that you were to perform relatively often in other programs, you would spend a lot of time remembering and retyping this formula. Alternatively, you could encapsulate this computation in a function and save it in a C++ file. Any time you wanted to convert temperatures, you could then add this C++ file to your project and call the function. You wouldn't need to remember the formula anymore, just the name of the function and how to call it. The following is the definition of a C++ function that performs the temperature conversion. A description of the key parts of this definition is given below:

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 5 / FUNCTION NAME: fahrtocelsius PARAMETERS: double fahr -- The degrees in Fahrenheit RETURN TYPE: double PURPOSE: Converts a given degree reading from F to C. / double fahrtocelsius(double fahr) { variable declarations and initialization double celsius = 0.0; convert the value from F to C... celsius = (5.0 / 9.0) * (fahr - 32); } return the converted value... return celsius; The first eight lines are a function header comment, describing the important aspects of the function (name, inputs, output, and purpose). You should always have comments in your functions to improve readability. The next line is the function header. The header specifies that you are defining a function named fahrtocelsius with one input. The input value is represented by a variable inside of the parentheses, also known as a parameter This variable name (fahr) is preceded by the data type of that variable (in this case, double). When the function is called, the input specified in the function call is assigned to the parameter for use in the function's computation. For example, for the call fahrtocelsius(212), the parameter fahr would be assigned the value 212. Thus, the value 212 would be used in the function code as the value for fahr, resulting in the function returning the value 100. The actual code that carries out the computation of the function is enclosed in curlybraces and is called the function body. In this example, there is only three C++ code statements, but functions can contain any number of statements. A return statement is a special C++ statement that specifies the value that should be returned by the function. It consists of the keyword return followed by an expression. When a return statement is executed, the expression is evaluated and the resulting value is returned as the value of the function call. In order to make a user-defined function accessible in a C++ program, it must be included in the C++ project. This can be accomplished in two ways. When the function is specific to that program only, its definition can be inserted directly into the.cpp file in the project; for instance,

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 6 we could have a single file in our fahrtocelsius project with two functions: main and fahrtocelsius. Alternatively, general-purpose function definitions can be entered into a separate library file and loaded into your project. In the future, we will see how to do this. Exercise #1: Create a new C++ code file called converttemp.cpp and type the following code into the file: / Name: converttemp.cpp Author: Jeffrey A. Stone Purpose: Program which converts a Fahrenheit reading to Celsius. / #include <iostream> using namespace std; / FUNCTION NAME: fahrtocelsius PARAMETERS: double fahr -- The degrees in Fahrenheit RETURN TYPE: double PURPOSE: Converts a given degree reading from F to C. / double fahrtocelsius(double fahr) { variable declarations and initialization double celsius = 0.0; convert the value from F to C... celsius = (5.0 / 9.0) * (fahr - 32); } return the converted value... return celsius; / FUNCTION NAME: main PARAMETERS: None RETURN TYPE: int PURPOSE: Entry point for the application. /

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 7 int main() { variable declaration and initialization double fahr_temp = 0.0; variable declaration and initialization double cel_temp = 0.0; prompt the user... cout << "Enter a reading in fahrenheit degrees : "; input the value from the user... cin >> fahr_temp; convert... cel_temp = fahrtocelsius(fahr_temp); output the temperature, in both units... cout << "You entered " << fahr_temp << " F degrees." << endl << "That is " << cel_temp << " C degrees." << endl; } return success to the Operating System... return 0; Compile and run the program, and verify that it works as it should. Next, watch and listen to the Walking Through a Function Call in C++ podcast before proceeding. This podcast will present a dynamic view of how the above program executes.

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 8 Exercise #2: Add a new function to converttemp.cpp called celsiustofahr. Modify the code in main to input two values: the degree reading, along with an F or a C, with the two values separated by a space. If the user inputs an F, the degree reading is in Fahrenheit; your program should convert the degrees to Celsius and output the result to the screen. If the user inputs a C, the degree reading is in Celsius; your program should convert the degrees to Fahrenheit and output the result to the screen. Don't forget to properly comment your code! Hint: You case use a predefined function called toupper to convert a character from lowercase to uppercase. This may prove useful for your conditional tests. For example, simplechar = toupper(simplechar); takes the value of simplechar and (if it is an alphabetic character) returns the uppercase version of the letter. Simply add #include <cctype> right below #include <iostream> to gain access to the function. Local Variables Notice from Exercise 1 that the function fahrtocelsius contains a single variable declaration, namely double celsius = 0.0; The first line in this function is a variable declaration specifying that the new variable celsius is to be used in the function. Declaring variables in this way signifies that they are local to the function, meaning they will only exist inside of the function. In this sense, local variables are similar to parameters. Declaring a local variable tells the C++ compiler that you are going to use it as temporary storage while computing the function value, and that the storage should go away i.e. be returned to the pool of available storage when execution of the function is complete. In all functions, including main, variables have a defined scope, the area(s) and time for which they are allowed to be used. In the case of C++ functions, this scope is limited to the code statements within the beginning and ending brackets ({ }). The lifetime of these variables (i.e. the time from which memory is allocated by the system to the time in which the memory is given back) is the time for which the function is executing. Global variables are variables declared outside of any function. Global variables are thus accessible by any function within the code file. While the C++ language permits this functionality, it is generally considered poor programming practice to declare variables with global scope. Do not declare global variables in any program you create in this course.

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 9 Part 3: Function Calling Sequence Whenever a function is called, a specific sequence of events takes place. If you understand this sequence, it should be possible to trace the execution of any function and ascertain its behavior. For example, consider the following program that computes the sum of a range of integers. / Name: sumnumbers.cpp Author: Jeffrey A. Stone Purpose: Sums the numbers in a user-defined sequence. / #include <iostream> using namespace std; / FUNCTION NAME: sumnumbers PARAMETERS: long min -- The lower bound value. long max -- The upper bound value. RETURN TYPE: long PURPOSE: Sums the numbers from low to high. / long sumnumbers(long min, long max) { long counter = 0; keeps a count of the number of loops long sum = 0; keeps a running sum of values set the counter to the min value... counter = min; while (counter <= max) { Add to our running total... sum = sum + counter; } Add to lcounter... counter = counter + 1; } return the sum of the numbers in the sequence... return sum;

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 10 / FUNCTION NAME: main PARAMETERS: None RETURN TYPE: int PURPOSE: Entry point for the application. / int main() { long low = 0, high = 0; stores the input values cout << "Enter two integers separated by a space, "; cout << "smallest integer first : "; cin >> low >> high; cout << "\nthe sum from " << low << " to " << high; cout << " is " << sumnumbers(low, high) << "\n"; prompt prompt input output output } return 0; return "success" to the operating system When the sumnumbers function is called in the main function, the following events occur: 1. Execution shifts to the sumnumbers function. 2. The inputs to the function are evaluated first, with the parameters min and max assuming the values contained within low and high, respectively. 3. Within the function, new memory cells are allocated for the parameters min and max. These memory cells are assigned the values of the corresponding arguments from the call: the values contained within the variables low and high, respectively. Throughout the function, any references to these parameters will be associated with these memory cells. 4. Memory cells are next allocated (created) for each local variable: counter and sum. Similarly, any references within the function to these local variable names will be associated with these memory cells. They are initialized with a zero value. 5. The statements in the function are executed in order, assigning sum the value of the sum of the integers from min to max. 6. When the return statement is reached, the expression in the statement is evaluated, yielding the value contained within sum. 7. After computing the return value, the execution of the function is over. Since the local variables exist only inside the function itself, the memory cells associated with the variables are de-allocated, i.e., made available for other uses. 8. Similarly, the memory cells associated with parameters are de-allocated.

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 11 9. Once the memory cells have been freed, the value in sum can be returned as the value of the function call. In this case, the value returned by the function replaces the function call in the assignment, and so that value is output to the screen. In general, the calling sequence for functions is as follows: 1. The arguments in the function call are evaluated. 2. Execution shifts to the function being called. 3. Memory cells are allocated (created) for each parameter in the function, and the values of the corresponding arguments are assigned to these memory cells. 4. Memory cells are allocated (created) for each local variable. 5. The statements in the body of the function are executed in order. 6. When a return statement is encountered, the expression is evaluated. 7. Memory cells associated with the local variables are de-allocated (destroyed). 8. Memory cells associated with the parameters are de-allocated (destroyed). 9. Upon return, the value of the expression in the return statement replaces the function call in whatever expression it appears in. Watch and listen to the Function Calling Sequence podcast before proceeding: Exercise #3: Create a new C++ workspace called incometax. Your main function should prompt the user for his/her gross income and itemized deduction (both double values). Construct a function called incometax which computes the tax due using the input values (gross income and itemized deduction) as follows:

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 12 The taxpayer's taxable income is determined by subtracting the taxpayer s deduction from the taxpayer's gross income. Total tax is determined by multiplying the taxable income by the tax rate (0.17). Your main function should call your incometax function to compute the total tax due, giving it appropriate arguments. Your incometax function should return the total tax due back to your main function. Finally, your main function should output the tax due to the screen with a descriptive message. Please watch and listen to the Techniques for User-Defined Functions in C++ podcast before proceeding. This podcast will present a description of how to create input, processing/computation, and output functions. These tips will be used heavily in this course. Part 4: Calculating the Energy Produced by a Wind Generator Suppose you wanted to harness to power of wind to generate electricity. One question you might want to ask is: what is the amount of power my wind generator can produce? To answer this question, you will need at least the following information: The average wind speed in your backyard (in m/s) The operating efficiency of your wind generator (in %) The radius of the blades on your wind generator (in meters)

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 13 To compute the maximum power output for your wind generator, you will need the following math formulas: A = πr 2, which represents the cross-sectional area of a circle (π = 3.14159265). P max = 1 2 ρav3, which calculates the maximum available power given the wind speed (ν), cross-sectional area of the blades (A), and the density of the air (ρ= 1.2 kg/m 3 ). Once you have the maximum available power, computing the actual amount of power (not the maximum) produced by the wind generator is a matter of determining the amount of power based on the operating efficiency. Exercise #4: Create a program to compute the amount of power produced by a wind generator. Your program should take three double precision inputs (see the discussion above) before performing its calculations. Create two functions other than main in your program. Both of these functions will return a double value. One function should be called computearea and should take one double parameter named radius. The other function should be called computemaximumpower and should take two double parameters (named area and wind_speed). You will need to call these functions from main function. Your output should include two calculated results the maximum power output and the actual power output. All numeric amounts should be precise to two decimal places. Remember: a percentage is really a number between 0 and 1, inclusive. Here are few sample tests you can run to check if your program is working correctly: Test Case #1: Enter the Average Wind Speed (in m/s): 5.0 Enter the Operating Efficiency [0...1]: 0.98 Enter the Blade Radius (in meters): 2.0 Maximum Power = 942.48, Actual Power = 923.63. Test Case #2: Enter the Average Wind Speed (in m/s): 12.29 Enter the Operating Efficiency [0...1]: 0.10 Enter the Blade Radius (in meters): 3.0 Maximum Power = 31491.93, Actual Power = 3149.19.

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 14 Part 5: Vocabulary Words You will be assessed on your knowledge of the following terms and concepts as well as the function concepts discussed in this document. As a result, you are encouraged to review these terms in the preceding document. Argument a value that is provided in a function call to fill in a parameter. Arguments provide functions with the data necessary to complete its task. Comment a statement, sentence, or phrase which describes a piece of code. Comments are ignored by the compiler and exist only to document the C++ code with a more natural language (e.g. English). In C++, single-line comments begin with. Multi-line comments are bounded with /* and */. C++ Standard Library a set of common tools which can be used in any program. These predefined functions and classes represent a collection of useful, general purpose abstractions. Function a specific unit of code in C++ containing a header and a body. Functions can be called by other functions to perform tasks, and can return a value back to the caller. Function Body The actual code that carries out the computation of the function; that part of the function which is enclosed in curly-braces. Function Call the act of invoking a function. A function call is made by using the function name and providing the function with any necessary arguments. Function Header the first line of a function, containing the function name, the parameter list, and the type of data returned by the function. Local Variable a variable declared within a set of { }, such as a function body. Local variables can only be used within that set of { }. Declaring a local variable tells the C++ compiler that you are going to use it as temporary storage while computing the function value, and that the storage should go away i.e. be returned to the pool of available storage when execution of the function is complete. Parameter a value that a function expects to be given when the function is called. A function can have 0 or more parameters. These are placeholders that must be filled in by arguments when the function is called. As a result, parameters can take on different

PALMS MODULE 2 LAB: FUNCTIONS IN C++ 15 values each time the function is called. Parameters act as local variables within the function body. Parameter List a set of parameters, enclosed in parentheses within the function header. Parameter lists follow the function name in a function header. Each parameter is declared using a data type and a name, much like a variable declaration. The parameter declarations in the list are separated by commas. Return Statement a special C++ statement that specifies the value that should be returned by the function; begins with the keyword return. Scope the visibility or lifetime of a variable. All variables have a defined area and time for which they are allowed to be used. In the case of C++ functions, this scope is limited to the code statements within the beginning and ending brackets ({ }). The lifetime of these variables (i.e. the time from which memory is allocated by the system to the time in which the memory is given back) is the time for which the function is executing.