0

Can anyone help me to understand how to return multiple values using pointers in a function? I am confused with the following example.

  1. Can we assign other values to int value besides 0 or 1, while the program can still run normally?

  2. What does defining the value of 0 and 1 in the if statement and else statement do for us in the int factor function?

  3. What does if(!error) statement do in the main ()?

#include <iostream>
using namespace std;

int factor(int n, int *p_addition, int *p_subtraction, int *p_squared, int *p_cubed)
{
    int value = 0;
    if (n >= 0 && n <= 100)
    {
        *p_addition = n + n;
        *p_subtraction = n - n;
        *p_squared = n*n;
        *p_cubed = n*n*n;
        value = 0;
    }
    else
    {
        value = 1;
    }
    // This function will return a value of 0 or 1
    return value;
}

int main()
{
    int number, num_add, num_sub, squared, cubed;
    int error;

    cout << "Enter a number between 0 and 100: ";
    cin >> number;

    error = factor(number, &num_add, &num_sub, &squared, &cubed);

    if (!error)
    {   
        cout << "number: " << number << endl;
        cout << "num_add: " << num_add << endl;
        cout << "num_sub: " << num_sub << endl;     
        cout << "squared: " << squared << endl;
        cout << "cubed: " << cubed << endl;
    }
    else
    {
        cout << "Error encountered!!" << endl;
    }
    return 0;
}
Sebastian
  • 1,834
  • 2
  • 10
  • 22
daiman00
  • 25
  • 2
  • 1
    https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list I think that your questions are better addressed by a tutorial. SO doesn't aim at teaching a language from scratch. Also, perhaps take the [tour] and read [ask]. – Ulrich Eckhardt Feb 19 '22 at 08:07
  • @TedKleinBergman All questions seem to be more or less related to int->bool implicit conversion, so I think it to be ok to keep it together – Sebastian Feb 19 '22 at 08:37

1 Answers1

0
  1. int is at least 16 bits (depending on the system - hardware, operating system, 32-bit computing / vs. 64-bit computing), for numeric range, cf: https://en.cppreference.com/w/cpp/language/types (at least -32768 to 32767 for 16 bit integers).

  2. factor contains return value, which (here) signifies to the caller, whether an error occurred. It has no other effect within factor.

  3. The ! negates the boolean value, so that the test is for false. if with an int implicitly converts to bool and tests for error being 0: https://en.cppreference.com/w/cpp/language/implicit_conversion#Boolean_conversions The value zero (for integral [...]) [...] become[s] false. All other values become true.

So any value beside 0 would have the same effect instead as 1.

It would have been better to name value something like wrong_input_range.

It would have been better to make value a bool type instead of int and make the return type of factor bool, too.

Whether your main returns a value beside 0 in case of the error, you can decide by yourself. Within a script, the returned value of called programs often is tested to know, whether the script can continue.

Sebastian
  • 1,834
  • 2
  • 10
  • 22