Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
Question
Book Icon
Chapter 9, Problem 1P
Program Plan Intro

Program Plan:

  • Include required header files.
  • Declaration of function prototype.
  • Definition for function “main()”.
    • Declare and initialize the variable “var”.
    • Print the value before calling function
    • Call the function “addOne()”.
    • Print the value after calling function.
    • Return the value “0”.
  • Definition of function “addOne()”.
    • Increment the pointer variable.

Expert Solution & Answer
Check Mark
Program Description Answer

The given program is to add one to the integer referenced by “ptrNum” by using reference parameter syntax.

Explanation of Solution

//Include required header files

#include<iostream>

using namespace std;

//Declaration of function header

void addOne(int *ptrNum);

//Definition of function main()

int main()

{

    //Declare and initialize the variable "var".

    int var = 10;

    //Print the value before calling function

  cout << "Value before calling function = " << var << '\n';

    //Call the function

    addOne(&var);

    //Print the value after calling function

  cout << "Value after calling function = " << var << '\n';

    //Return the value "0"

    return 0;

}

//Definition of function "addOne()"

void addOne(int *ptrNum)

{

    //Increment the pointer variable

    *ptrNum = *ptrNum + 1;

}

Sample Output

Output:

Value before calling function = 5

Value after calling function = 6

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Create a function in C language that takes two integers x, and y as the parameters and returns the sum and the absolute difference of both the numbers but the return type of the function should be void. You can add extra parameters to the function but the function should not return anything and should send the required values as well. Test your function for the numbers 10, and 5 inside the main function.
In c++ language. The code should be structured( only one return in each function, no exit or continue, or break)
For C++, why is it that reference values can change when given to a function? I understand that if you make a const reference parameter like: double functionname(const point& p1) then the function cannot attempt to make a change to the parameter.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr