Skip to content

Instantly share code, notes, and snippets.

@titus-shoats
Created May 9, 2017 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titus-shoats/1037c1a7992fce891e47c801cdb85f47 to your computer and use it in GitHub Desktop.
Save titus-shoats/1037c1a7992fce891e47c801cdb85f47 to your computer and use it in GitHub Desktop.
Returning multiple values with pointers
#include "stdafx.h"
#include <iostream>
using namespace std;
void FreezeWindow();
void FreezeWindow() {
char response;
cin >> response;
}
short Factor(int n, int* pSquared, int* pCubed);
int main() {
int number, squared, cubed;
short error;
cout << "Enter a number (0-20); ";
cin >> number;
error = Factor(number, &squared, &cubed);
if (!error) {
cout << "number: " << number << endl;
cout << "square: " << squared << endl;
cout << "cubed: " << cubed << endl;
}
else {
cout << "Error encountered" << endl;
}
FreezeWindow();
return 0;
}
short Factor(int n, int *pSquared, int *pCubed) {
short Value = 0;
// set value to 1, dont change square or cube values
if (n > 20) {
Value = 1;
}else {
// if n is 3
// Change square, and cube values, set value to 0
*pSquared = n*n; // 9
*pCubed = n*n*n; // 27
Value = 0;
}
return Value;
}
// output is
/*****
Enter a number (0 - 20): 3
number: 3
square: 9
cubed: 27
The actual values square, cube, and number are not returned
by using the return mechanism, but are returned by changing
the pointers that were passed into the function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment