-1

Question:

Make function which will sum two integer variables (a,b) and then return result in variable rez , using pointer. Also, in the same function, return sum a+b+10 in another variable rez_a using pointer.

Well, here is the code. It returns only the first value (*p1):

#include <iostream>

using namespace std; 
int vrati(int a, int b) {
    int rez = a + b;
    int rez_a = a + b + 10;
    int* p1 = &rez;
    int* p2 = &rez_a;
    return *p1,*p2;
}

int main() {
    int a = 4;
    int b = 6;
    cout << vrati(a, b);
    
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    This is explained in any beginner level [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In particular, no C++ book will teach you to write: `return *p1,*p2;`. – Jason Aug 13 '22 at 14:55
  • Actually, it returns only the second value. That's what comma operator does in C++. – Yksisarvinen Aug 13 '22 at 15:06

1 Answers1

3

I think you are taking return too literally. I expect the function you are meant to write is this

void vrati(int a, int b, int *rez, int* rez_a) {
    *rez = a + b;
    *rez_a = a + b + 10;
}

int main() {
    int a = 4;
    int b = 6;
    int rez, rez_a;
    vrati(a, b, &rez, &rez_a);
    cout << rez << ' ' << rez_a;
    
    return 0;
}

This function returns values, but it doesn't use return. I can understand why you were confused.

john
  • 85,011
  • 4
  • 57
  • 81