Can C++ make a function that returns two values ?

No you cannot. But you can pass the variables by reference so that the changes are reflected in the original variables. For example

void test(int &a,int &b)
{
     a=a+1;
     b=b+1;
}

int main()
{
    int c=23,d=6;
    test(c,d);
    cout<<c<<endl<<d;
cin.get();
cin.ignore();
return 0;
}

Here the values of c and d will be incremented by 1 i.e. changes will be reflected in the original variables.

Alternatively, if you want to return something you could create a struct/class which contains the data you want to return.

#include <iostream>
#include <string>
#include <fstream>

struct names {
  std::string first_name;
  std::string last_name;
};

names read_from_file( std::ifstream &in ) {
  names i_names;
  in >> i_names.first_name >> i_names.last_name;

  return i_names;
}

Probably isn't the best example of how to do it but gives you an idea anyhow.

Alternatively, you can make a class where the return values are fields and the object can be called like a function by overloading the () operator:

#include <iostream>
#include <string>

struct Name {
  std::string First;
  std::string Last;

  void operator()()
  {
    std::cin >> First >> Last;
  }
};

int main()
{
  Name getName;

  getName();
  std::cout << getName.Last << ", " << getName.First << '\n';
}

c++98 has the convenient std::pair<> .
c++09 also has std::tuple<> .

#include <utility>
#include <algorithm>
#include <tuple>

std::pair<int,int> min_n_max( const int* arr, std::size_t N )
{
  // invariant: N > 0
  int low = arr[0] ;
  int high = arr[0] ;
  
  for( std::size_t i=1 ; i<N ; ++i )
  {
    low = std::min( low, arr[i] ) ;
    high = std::max( high, arr[i] ) ;
  }
    
  return std::make_pair(low,high) ;
}

std::tuple<int,int,double> min_max_n_avg( const int* arr, std::size_t N )
{
  // invariant: N > 0
  int low = arr[0] ;
  int high = arr[0] ;
  int sum = arr[0] ;
    
  for( std::size_t i=1 ; i<N ; ++i )
  {
    low = std::min( low, arr[i] ) ;
    high = std::max( high, arr[i] ) ;
    sum += arr[i] ;
  }
    
  return std::make_tuple( low, high, sum/double(N) ) ;
}

I need to write a code which I have a large quantity of values, and from these sample, get the maximum and minimum values. I was thinking of using vectors or arrays but there are too large a quantity...

> .. I have a large quantity of values...
if the values are in a file, pass the file (istream) to the minmax function. in the function, read the values one by one and check for min/max.
if the values are programmatically generated, pass the generator function (or function object) to the minmax function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.