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++ (Looseleaf) - With Access
- Write a C# program to define a function that takes parameters and also returns something. Also, make a call to your function inside the main function. Note: The return type should not be void.arrow_forwardIn C++, Write a complete program to call a function that swaps the values stored in two variables. The function accepts two parameters x and y which are to be swapped. The parameters can be of any data-type.arrow_forwardIn 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_forward
- Write a function in C language which takes an integer as parameter and returns the sum of all non zero digits in the number passed as argument to the function.arrow_forwardWrite a C# program to define a function that takes parameters and also returns something. Also, make a call to your function inside the main function.arrow_forwardin C++ Write the definition of a value-returning function that accepts two integer parameters and returns the larger one divided by the smaller one.arrow_forward
- Write a C++ program that swaps two values by passing pointers as arguments to function.arrow_forwardGiven the code segment below, what should be the data type of the formal parameter in the function prototype of func(), given the call from main()? If the accessing is wrong, answer INVALID. void func( ______ );int main(){ double aData[6][4];func(aData[6]);return 0;}arrow_forwardGiven the code segment below, what should be the data type of the formal parameter in the function prototype of func(), given the call from main()? If the accessing is wrong, answer INVALID. void func( ______ );intmain(){ double aData[6][4];func(&aData[4][1]);return 0;}arrow_forward
- Set-up and implementation code for a value-returning bool function EqualsYou are not required to write a complete C++ program but must write and submit just your responses to the four specific function-related questions below: QA1: Write the heading for a value-returning bool function called Equals that has two value float parameters, x and y. Document the data flow of the parameters with appropriate comments*. QA2: Write the function prototype for the function in QA1. QA3: Write the function definition of the function in QA1 so that it compares x and y, returning true if their difference is less than 0.000000001, and false otherwise.QA4: Add comments to the function definition* you wrote in QA3 that also states its precondition and postcondition.arrow_forwarda.)Write a C++ program that consists of a function declaration, a function definition and a main. The function declaration is as follows: void endTime (int ch, int cm, char cap, int dh, int dm, int eh, int em, char eap)// given any current hour, minutes and 'A' or 'P' for am/pm in parameters ch, cm and cap respectively,// and also given the duration (hours and minutes) of an activity in parameters dh and dm,// the function stores the end time in parameters eh, em and eap. Where eh will hold the hour of the end time, em holds the minute and eap holds eithe 'A' or 'P' for the end time.// All values are using a 12 hour clock// Function assumes all activities end in the same day Also include a main program that feeds the following arguments to the functions:11, 25, 'A', 3, 45, hour, minute, ampm causing the function to calculate the end time as 3 10 PUsing the calculated results, the main then outputs:If the current time is 11:25AM and we have 3 hours and 45 minutes to go, we have to sit…arrow_forwardYou can write a program in C++ using only one function. At the same time you can break this function into 20 smaller functions calling each other. Which approach is better? Why?arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning