- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two numbers a and b. We shall have to define a function that can calculate (a + b) and (a - b) both. But using a function in C++, we can return at most one value. To find more than one output, we can use output parameters into function arguments using pointers, and call that function using addresses of those variables. Here in this problem we shall update a with a+b and b with a-b. When we call the function we shall have to pass the address of these two variables.

So, if the input is like a = 15, b = 18, then the output will be a + b = 33 and a - b = -3

To solve this, we will follow these steps −

define a function solve(), this will take addresses of a and b

temp := sum of the values of variable whose addresses are given

b := difference of the values of variable whose addresses are given

a = temp

Let us see the following implementation to get better understanding −

#include <iostream> using namespace std; int solve(int *a, int *b){ int temp = *a + *b; *b = *a - *b; *a = temp; } int main(){ int a = 15, b = 18; solve(&a, &b); cout << "a + b = " << a << " and a - b = " << b; }

15, 18

a + b = 33 and a - b = -3

- Related Questions & Answers
- Addition and Subtraction of Matrix using pthreads in C/C++
- Given an example of C pointer addition and subtraction
- Signals and Systems: Addition and Subtraction of Signals
- C program for Addition and Multiplication by 2 using Bitwise Operations.
- How MySQL performs date arithmetic with addition and subtraction operators?
- C++ Program to Swap Numbers in Cyclic Order Using Call by Reference
- Call by value and Call by reference in Java
- Difference between Call by Value and Call by Reference
- C++ Program to Perform Addition Operation Using Bitwise Operators
- C program to find sum and difference using pointers in function
- How to call the main function in C program?
- C# program to find IP Address of the client
- C# program to Display Hostname and IP address
- C Program to display hostname and IP address
- Program to display hostname and IP address C

Advertisements