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.
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;
}
Output:
Value before calling function = 5
Value after calling function = 6
Want to see more full solutions like this?
Chapter 9 Solutions
Problem Solving with C++ (10th Edition)
- What does it mean to provide parameters to a function in the right order when it accepts more than one?arrow_forwardInvoking a function that takes many arguments, the order in which parameters are supplied is crucial.arrow_forwardWrite a C++ program to facilitate the usage of several string-handling functions. The program should present a menu for the choices available. The options might be to create, insert, append, erase, reverse, and print. The program should ask the user what required by the function to prepare the real function call. For instance, for the insert function, the string might be printed on the screen as a reminder first, and then the start and finish indexes might be requested from the user. You should be aware of the run-time issues that might result due to user mistakes. Try to prevent them!arrow_forward
- In C++, If you had the double-pointer above and also had these variables: type x, * q; And had executed these statements: q = &x; p = &q; How would a function given p by value be able to change the contents of x?arrow_forwardpointers as Arguments:In the C programming language there is no pass-by-reference syntax to passa variable by reference to a function. Instead a variable is passed by pointer(just to be confusing, sometimes passing by pointer is referred to as pass byreference). This Practice Program asks you to do the same thing as C.Here is the header for a function that takes as input a pointer to an integer:1. void addOne (int ∗ptrNum )Complete the function so it adds one to the integer referenced by ptrNum.Write a main function where an integer variable is defined, give it an initialvalue, call addOne, and output the variable. It should be incremented by 1.arrow_forwardIn C++ programing language, Write the definition of the function template that swaps the content of two variablesarrow_forward
- In C/C++, Function overloading is the process of creating several functions that have exactly the same name. In order to overload successfully, each function must have: A. A different parameter list B. A different number of parameters and a different return type C. An integer on the end of the function name, such as func1()and func2() D. Either A or B E. Both A and Carrow_forwardUsing a pointer as the return value is considered a poor practise in C. For example, how does dynamic memory allow us to return an object pointer from a function safely?arrow_forwardA specific class of C++ variable is a pointer variable. Do you understand how to utilize it?arrow_forward
- Local declarations are those that are kept in the memory of the computer; but, how exactly are they kept in memory? If reaching one's objective can be done without making use of local declarations, then doing so is a waste of time. Why bother using value parameters when you can just use references as your arguments in any function? How crucial are value parameters when it comes to the processing of programmed data?arrow_forwardC Program Functions using Pointers Create a function modify that accepts an integer and divides the integer by 2 if it is even. If the integer is odd, add one and divide it by 2. The function does not return anything. In the main function, write a program that asks for an integer input and call the modify function by passing in the reference of that variable as a parameter. An initial code is provided for you. Just fill in the blanks. Input 1. One line containing an integer input Output Enter a number: 5 Before: 5 After: 3arrow_forwardPlease explain the concept of pointers in a function in C++.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr