Escolar Documentos
Profissional Documentos
Cultura Documentos
th
Function Review
More on Functions
Passing Variables using References
Function Overloading and Default Parameters
Global and Static Variables
Detecting errors in functions
Function Review
int square(int x);
// int square(int);
int main(void)
{
int n;
cout << "Enter a #: ";
cin >> n;
cout << square( n ) << endl;
return(0);
}
// function definition
int square( int x )
{
int y; // new each time its called
y = x * x;
return (y);
}
1. The prototype *.
2. The function
definition:
a. Func header.
b. Formal
parameter(s)
c. Return type
d. Function body
e. Return
statement(s)
f. Local variables
3. The function
call:
a. Actual
parameter(s)
Function Review
int square(int x);
// prototype
int main(void)
{
int n;
cout << "Enter a #: "; 3
cin >> n;
4
cout << square( n+1 ) << endl;
return(0);
Things to note:
1.
All variables/
parameters in a
function are
visible only to
the function.
Function Review
string nickname(int x);
// prototype
int main(void)
{
int age;
cout << "Enter your age: ";
cin >> age;
cout << nickname(age) << endl;
return(0);
}
// function definition
string nickname( int yourage )
{
if (yourage < 30)
return( kiddo );
strings!
else
return( old fogie );
}
Things to note:
1.
If a function has
a non-void return
type, it must
return a value.
Function Review
void eat(int x);
// prototype
int main(void)
{
int prunes;
cout << How many prunes? ";
cin >> prunes;
eat(prunes);
cout << aaah\n;
}
52
0 < 52?
for (i=0;i<num_prunes;i++)
{
if (i == 1)
return;
cout << phhhbbbbtttt\n;
}
}
Things to note:
1. A return statement
immediately exits
the function, no
matter what.
prunes 52
num_prunes 52
i 01
How many prunes?
52
phhhbbbbtttt
aaah
Function Review
// prototype:
void barf(void);
int main(void)
{
int n = 10;
barf();
cout << n;
}
// definition
void barf(void)
{
cout << n;
// OK?
n = 50;
cout << n << endl;
}
Function Review
// prototype:
void barf(void);
int main(void)
{
int n = 10;
barf();
cout << n;
}
// definition
void barf(void)
{
int n;
// local
n = 50;
cout << n << endl;
}
50
10
n 10
50
Function Review
// prototype:
void barf(void);
int main(void)
{
int x = 10;
barf();
barf();
}
// definition
void barf(void)
{
int n;
cout << n;
n = 50;
}
// local
Output:
-19 37
x 10
10
n 10
-19
37
50
Function Challenge
#1. Write a function called charfind that:
A. Accepts a string parameter and a char parameter
B. Returns the index in the string of the first
occurrence of the character.
C. If the char cant be found, your function should
return -1.
void main(void)
{
int n;
n = charfind(hello, l);
cout << n; // prints 2
n = charfind(hello, z);
cout << n; // prints -1
Functions: Understanding
Parameters
void change_me(int x);
int main(void)
{
int n;
cout << "Enter a #: ";
cin >> n;
Questions:
1. What does this program print?
2. Does the value of n change?
3. Why or why not?
// definition
void change_me(int x)
{
x = 12;
}
12
3
Enter a #: 3
3
3
Functions: Understanding
Parameters
void change_me(int x);
int main(void)
{
int n;
cout << "Enter a #: ";
cin >> n;
// definition
void change_me(int n)
{
n = 12;
}
12
3
Enter a #: 3
3
3
20
10 20
}
void main(void)
{
int x = 10, y = 20;
10, 20
swap( x, y);
cout << x << endl;
cout << y << endl;
}
x
y
a
b
temp
10
20
20
10
10
20
10
20 10
x 20
10
y 10
20
(ref) a
(ref) b
temp 10
Reference Parameters
You can use a reference parameter to modify
a variable from the calling function.
Any time you access the reference parameter,
youre really accessing the variable in the calling
function.
Syntax: Place an & between a parameters
type and its name in the function header:
void SomeFunction(int & doe, float & ray, string & mi)
{
etc
+ fight,
s = UCLA fight,
+ fight,
s = s + fight, ;
}
void main(void)
{
string msg = UCLA ";
cheer(msg);
cout << msg << endl;
cheer(msg);
cout << msg << endl;
}
UCLA fight,
UCLA fight, fight
(ref) s
msg
UCLA fight,
fight
Reference Parameters
void add4(float &x)
{
x += 4;
}
void add7(float &y)
{
y += 3;
add4(y);
}
void main(void)
{
float a = 50.0;
add7(a);
cout << 50 plus 7 is: << a;
}
53.0
50.0
a 57.0
50 plus 7 is 57.000
&i )
void main(void)
{
double d = 10;
zero_it(d); // SYNTAX ERROR
}
25
to provide
default values
for every
parameter.
BAD!
parameters
must be to the
right of all nondefault
parameters for
a given function.
Why? Certain
function calls
would be
ambiguous
otherwise!
It is illegal in C++ to
overload a function JUST
based on its return type.
Overloaded functions MUST
have different parameter
types in their lists.
Variables in Functions
As weve seen, each function has its own local
variables.
A function can only access its own local variables
or those variables passed by reference.
(at least thats what weve learned so far)
Now, lets learn about two new types of
variables: global variables and static variables.
Global Variables
// gN is global.
void change_me(void);
void main(void)
{
cout << "Enter a #: ";
cin >> gN;
cout << gN << endl;
change_me();
cout << gN << endl;
}
// definition
void change_me(void)
{
gN = -3;
}
Output:
0
gN 42
-3
Enter a #: 42
42
-3
Global Variables
#include <string>
string msg = Carey;
silly!
is
word
msg
silly
is
Carey
is silly!
is
Carey
Static Variables
void ucla(void);
void main(void)
{
ucla();
ucla();
ucla();
}
cout << a;
// ERROR!
void ucla(void)
{
skip static int a = 10;
13
12
10
11
10 11 12
Static Variables
If you do not explicitly
initialize a static variable, it
starts out with a value of 0 the
first time the function runs.
6
3
3
val 6
0
total 9
3
// global!
void hmm(void) {
int careful = 100;
cout << ++careful << endl;
}
void shh(void) {
cout << ++careful << endl;
}
void wow(void) {
static int careful = 50;
cout << ++careful << endl;
}
void main(void)
hmm();
shh();
wow();
hmm();
shh();
wow();
if (hypotenuse4/0
!= 0)
result = adj/hypotenuse;
return(result);
}
int main(void)
{
float
bool Cosine(float op, float adj,
,)
{
float &result)
hypotenuse
float result;
if (hypotenuse != 0)
{if (hypotenuse
0)
result = !=adj/hypotenuse;
Heres how we do it
1. First, lets change the
function so it returns the
result by reference.
2. Next, update the function
so it returns a boolean
result
3. Finally, update your other
functions to call the new
one properly
return(true); // success!
return(result);
}
false );
// failure!
int main(void)
{
cout
float<<c;Cosine(3,4,5);
cout << Cosine(3,4,0);
if (Cosine(3,4,0,c) == true)
cout << c;
else cout << ERROR!;