- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Returning multiple values from a C++ function
In C or C++, we cannot return multiple values from a function directly. In this section we will see how to use some trick to return more than one value from a function.
We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.
In this example we will see how to define a function that can return quotient and remainder after dividing two numbers from one single function.
Call By Address
Example
#include<iostream> using namespace std; void div(int a, int b, int *quotient, int *remainder) { *quotient = a / b; *remainder = a % b; } main() { int a = 76, b = 10; int q, r; div(a, b, &q, &r); cout << "Quotient is: "<< q <<"\nRemainder is: "<< r <<"\n"; }
Output
Quotient is: 7 Remainder is: 6
Call By Reference
Example
#include<iostream> using namespace std; void div(int a, int b, int "ient, int &remainder) { quotient = a / b; remainder = a % b; } main() { int a = 76, b = 10; int q, r; div(a, b, q, r); cout << "Quotient is: "<< q <<"\nRemainder is: "<< r <<"\n"; }
Output
Quotient is: 7 Remainder is: 6
- Related Articles
- Returning multiple values from a function using Tuple and Pair in C++
- Returning two values from a function in PHP
- Returning Multiple Values in Python?
- How can we return multiple values from a function in C/C++?
- How can we return multiple values from a function in C#?
- Returning values from a constructor in JavaScript?
- How to return multiple values from the function in Golang?
- PHP Returning values
- Declare a C/C++ function returning pointer to array of integer function pointers
- Function returning another function in JavaScript
- Remove same values from array containing multiple values JavaScript
- Returning Value from a Subroutine in Perl
- How to input multiple values from user in one line in C#?
- MongoDB query to limit the returning values of a field?
- MongoDB query to pull multiple values from array

Advertisements