Starting Out with C++ from Control Structures to Objects 9th Edition by Tony Gaddis Test Bank
All for this textbook (2)
Written for
University of Pretoria (UP)
COS 132
All documents for this subject (7)
Seller
Follow
jennamortonx
Content preview
COS132 Chapter 6
COS 132
CHAPTER 6 – FUNCTIONS
UNIT 6.1- FOCUS ON SOFTWARE ENGINEERING: MODULAR PROGRAMMING
A program may be broken up into manageable functions.
A function = collection of statements that perform a specific task.
- We have experienced functions in two ways:
o Creating “main” in every program
o Library functions
Pow
Strcmp
Using functions can be called divide and conquer as a large problem is divided.
Benefit of using functions is code reuse.
UNIT 6.2 – DEFINING AND CALLING FUNCTIONS
Function call = statement that causes a function to execute, a function definition contains the statements that make up the function.
When creating a function, you must write its definition, all function definitions have the following parts:
VOID FUNCTIONS
Some functions simply perform one or more statements, and then terminate – they are known as void functions. This means the
function does not return a value to the part of the program that executed it. Also notice the function has no return statement
CALLING A FUNCTION
Function main is automatically called when a program starts, but all other functions must be executed by function call statements.
Function header ---- void functionName()
, COS132 Chapter 6
Function call --- functionName();
UNIT 6.3 – FUNCTION PROTOTYPES / FUNCTION DECLARATIONS
A function prototype eliminates the need to place a function definition before all calls to the function.
Before the complier encounters a call to a particular function, it must already know the function’s return type, the number of
parameters it uses, and the type of each parameter. One way of ensuring that the compiler has this info is to place the function
definition before all calls to that function.
Another method is to declare the function w a function prototype, (void functionName(); ) the prototype looks similar to the
function header, except there is a semicolon at the end.
Function prototypes are usually placed near the top of a program so the compiler will encounter them before any function calls.
UNIT 6.4 – SENDING DATA INTO A FUNCTION
When a function is called, the program may send values into the function. These values are called arguments.
The values that are passed into a function are called arguments, and the variables that receive those values are called parameters.
There are several variations of these terms in use. Some call the arguments actual parameters and call the parameters formal
parameters. Others use the terms actual argument and formal argument. Regardless of which set of terms you use, it is important to
be consistent
If you pass an argument whose type is not the same as the parameter’s type, the argument will be promoted or demoted
automatically.
Parameters have a scope – limited to the body of the function that uses it.
UNIT 6.5 – PASSING DATA BY VALUE
When an argument is passed into a parameter, only a copy of the arguments value is passed. Changes to the parameter do not affect
the OG argument.
Parameters are special-purpose variables that are defined inside the parentheses of a function definition. They are separate and
distinct from the arguments that are listed inside the parentheses of a function call. The values that are stored in the parameter
variables are copies of the arguments. Normally, when a parameter’s value is changed inside a function, it has no effect on the
original argument.
UNIT 6.6 – FOCUS ON SOFTWARE ENGINEERING: USING FUNCTIONS IN A MENU-DRIVEN PROGRAM
Functions are ideal for use in menu-driven programs, when the user selects an item from a menu, the program can call the
appropriate function.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller jennamortonx. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.07. You're not tied to anything after your purchase.