More than one returning value
#include <iostream.h>
void f (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z;
f (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}
Previous=99, Next=101"
Multiple return statements in a function
#include <iostream>
int Doubler(int AmountToDouble);
int main()
{
int result = 0;
int input = 123;
std::cout << "\nBefore doubler is called...";
std::cout << "\ninput: " << input
<< " doubled: " << result << "\n";
result = Doubler(input);
std::cout << "\nBack from Doubler...";
std::cout << "\ninput: " << input
<< " doubled: " << result << "\n\n";
return 0;
}
int Doubler(int original)
{
if (original <= 10000)
return original * 2;
else
return -1;
std::cout << "You can"t get here!\n";
}
Before doubler is called...
input: 123 doubled: 0
Back from Doubler...
input: 123 doubled: 246
Return a double value from a function
#include <iostream>
using namespace std;
double box(double length, double width, double height); // use double data
int main()
{
double answer;
answer = box(10.1, 11.2, 3.3); // assign return value
cout << "The volume is " << answer;
return 0;
}
// This version of box uses double data.
double box(double length, double width, double height)
{
return length * width * height ;
}
The volume is 373.296
Return a pointer from a function
#include <iostream>
using namespace std;
char *get_substr(char *sub, char *str);
int main()
{
char *substr;
substr = get_substr("three", "one two three four");
cout << "substring found: " << substr;
return 0;
}
// Return pointer to substring or null if not found.
char *get_substr(char *sub, char *str)
{
int t;
char *p, *p2, *start;
for(t=0; str[t]; t++) {
p = &str[t];
start = p;
p2 = sub;
while(*p2 && *p2==*p) { // check for substring
p++;
p2++;
}
/* If at end of p2 (i.e., substring), then a match has been found. */
if(!*p2)
return start; // return pointer to beginning of substring
}
return 0;
}
/*
Quote from: C++: A Beginner"s Guide, Second Edition
# Publisher: McGraw-Hill Osborne Media; 2 edition (December 3, 2003)
# Language: English
# ISBN-10: 0072232153
# ISBN-13: 978-0072232158
*/
substring found: three four
Return a value from a function
#include <iostream>
using namespace std;
int box(int length, int width, int height); // return the volume
int main()
{
int answer;
answer = box(10, 11, 3); // assign return value
cout << "The volume is " << answer;
return 0;
}
// This function returns a value.
int box(int length, int width, int height)
{
return length * width * height ;
}
The volume is 330
Return class instance from a function
#include<iostream.h>
class MyClass
{
char ch;
public:
MyClass(char c)
{
ch=c;
cout << "Constructing";
cout << ch <<"\n";
}
~MyClass(){
cout << "Destructing" << ch <<"\n";
}
};
MyClass createMyClass()
{
MyClass B("B");
return B;
}
main()
{
MyClass A("A");
createMyClass();
return 0;
}
ConstructingA
ConstructingB
DestructingB
DestructingA
Returning a reference
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string& refToElement(vector<string>& inventory, int i);
int main()
{
vector<string> inventory;
inventory.push_back("sword");
inventory.push_back("armor");
inventory.push_back("shield");
cout << refToElement(inventory, 0) << endl;
//assigns one reference to another -- inexpensive assignment
string& rStr = refToElement(inventory, 1);
cout << rStr << endl;
//copies a string object -- expensive assignment
string str = refToElement(inventory, 2);
cout << str << endl;
//altering the string object through a returned reference
rStr = "Healing Potion";
cout << inventory[1] << endl;
return 0;
}
//returns a reference to a string
string& refToElement(vector<string>& vec, int i)
{
return vec[i];
}
Returning multiple values from a function using pointer
#include <iostream>
short f(int, int*, int*);
int main()
{
int number = 2, squared, cubed;
std::cout << "number: " << number << "\n";
std::cout << "square: " << squared << "\n";
std::cout << "cubed: " << cubed << "\n";
return 0;
}
short f(int n, int *pSquared, int *pCubed)
{
*pSquared = n*n;
*pCubed = n*n*n;
}
number: 2
square: 6
cubed: 2293672
Return void from a function
#include <iostream>
using namespace std;
void f();
int main()
{
cout << "Before call\n";
f();
cout << "After call\n";
return 0;
}
void f(){
cout << "in f\n\n\n";
}
Before call
in f
After call
void function with return statement
#include <iostream>
using namespace std;
void power(int base, int exp);
int main()
{
power(10, 2);
power(10, -2);
return 0;
}
void power(int base, int exp)
{
int i;
if(exp < 0) return; /* Can"t do negative exponents. */
i = 1;
for( ; exp; exp--)
i = base * i;
cout << "The answer is: " << i;
return;
}
The answer is: 100