
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How can we return multiple values from a function in C/C++?
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.
Example Code
#include<stdio.h> 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); printf("Quotient is: %d\nRemainder is: %d\n", q, r); }
Output
Quotient is: 7 Remainder is: 6
- Related Articles
- How can we return multiple values from a function in C#?
- How to return multiple values from the function in Golang?
- How can we return a dictionary from a Python function?
- How do we return multiple values in Python?
- Haskell Program to return multiple values from the function
- Returning multiple values from a C++ function
- Can a method return multiple values in Java?
- How can we ignore the negative values return by MySQL DATEDIFF() function?
- How can we return null from a generic method in C#?
- Returning multiple values from a function using Tuple and Pair in C++
- How to return multiple values to caller method in c#?
- How can we delete multiple rows from a MySQL table?
- How can a Python function return a function?
- C function argument and return values
- How to return an array from a function in C++?

Advertisements