When a function is supposed to return a single value

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: all Non-void Function Call CS 140 Chapter 4 Page 3 CallbyReference Parameters When a function isn't supposed to return any values, it can be made a void function. When a function is supposed to return a single value, it can be made a nonvoid function, using the same type as the value being returned. When a function is supposed to produce multiple values that are needed in the calling function, then the two functions are allowed to share the same memory by means of callby reference parameters. CS 140 Chapter 4 Page 4 #include <iostream> using namespace std; void retrieveThreeNumbers(int &firstNbr, int &secondNbr, int &thirdNbr); void reorderThreeNumbers(int &nbrA, int &nbrB, int &nbrC); void outputThreeNumbers(int smallest, int middle, int largest); void main() { int x, y, z; retrieveThreeNumbers(x, y, z); reorderThreeNumbers(x, y, z); outputThreeNumbers(x, y, z); } Notethat theam rsand (& ) in front of thevariablenam pe e signifie that theparam te is be passe by re re inste s e r ing d fe nce ad of by value . I n othe words, thefunction will not m a copy of thevalue r ake be passe but will, in fact, beusing theoriginal m m ing d, e ory. void retrieveThreeNumbers(int &firstNbr, int &secondNbr, int &thirdNbr) { cout << "Enter three integers." << endl; cout << "First integer: cin >> fir...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 140 taught by Professor Staff during the Fall '08 term at Southern Illinois University Edwardsville.

Ask a homework question - tutors are online