C++Review

C++Review - Review Topics Calling functions by value or...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays Constructors, destructors and copy-constructors Operator overloading
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Functions Section 2.2
Background image of page 2
Two ways to call a function: Call by “ value Useful for “protecting” the values of the variables passed to a function. Call by “ reference Useful for "returning” multiple values. Need to define “formal” and “actual” function parameters first …
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int FindMax(int x, int y) { int maximum; if(x>=y) maximum = x; else maximum = y; return maximum; } “FindMax” Example
Background image of page 4
“FindMax” Example (cont.) #include <iostream.h> int FindMax(int, int); // function prototype int main() { int firstnum, secnum, max; cout << "\nEnter two numbers: "; cin >> firstnum >> secnum; max=FindMax( firstnum, secnum); // the function is called here cout << "The maximum is " << max << endl; return 0; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The argument names in the function header are referred to as formal parameters . int FindMax(int x, int y) { int maximum; if(x>=y) maximum = x; else maximum = y; return maximum; } Formal Parameters x, y are called “formal parameters”
Background image of page 6
Actual Parameters The argument names in the function call are referred to as actual parameters #include <iostream.h> int FindMax(int, int); // function prototype int main() { int firstnum, secnum, max; cout << "\nEnter two numbers: "; cin >> firstnum >> secnum; max=FindMax( firstnum, secnum); // the function is called here cout << "The maximum is " << max << endl; return 0; } firstnum, secnum are called “actual parameters”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Calling a function by “ value The formal parameters receive a copy of the actual parameter values. The function cannot change the values of the actual parameters.
Background image of page 8
#include <iostream.h> void newval(float, float); // function prototype int main() { float firstnum, secnum; cout << "Enter two numbers: "; cin >> firstnum >> secnum; newval(firstnum, secnum); cout << firstnum << secnum << endl; return 0; } void newval(float xnum, float ynum) { xnum = 89.5; ynum = 99.5; } Calling a function by value (cont.)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Calling a function by “ reference The formal parameters become an alias for the actual parameters. The function can change the values of the actual parameters. Note: formal parameters must be declared as “reference” variables.
Background image of page 10
#include <iostream.h> void newval(float&, float&); // function prototype int main() { float firstnum, secnum; cout << "Enter two numbers: "; cin >> firstnum >> secnum; newval(firstnum, secnum); cout << firstnum << secnum << endl; return 0; } { xnum = 89.5; ynum = 99.5; } Calling a function by reference (cont.) reference variables!
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The "const" modifier Call by reference is the preferred way to pass a large structure or class instances to functions, since the entire structure need not be copied each time it is used! C++ provides us with protection against
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 63

C++Review - Review Topics Calling functions by value or...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online