UCLA CS 31 Lecture5 Post On Functions
UCLA CS 31 Lecture5 Post On Functions
Review Challenge
A new data type: bool
Short Circuiting
Introduction to Function Calls
Review Challenge
Write a program that reverses a string
void main(void)
{
string s = a man a plan a canal panama;
// write your code here to reverse the string
cout << s;
}
int main(void)
{
bool bigger;
17
age
oldPerson
bigger = 10 > 3;
17
-44
false
??
if (bigger == true)
cout << 10 is > than 3\n;
bigger
true
??
if (bigEars)
cout << U got big Ears!;
int main(void)
{
bool bigEars;
...
if (bigEars == false)
cout << Small ears!;
if ( ! bigEars )
cout << Small ears!;
randExamples
function returns a
MoreThe
bool
#include <iostream>
#include <cstdlib>
int main(void)
{
bool done = false;
int myNum = rand( ); // 0 to 65535
true == false??
== 721??
guess
myNum
done
721
-10
721
true
false
Short Circuiting
bool cute = true;
if (cute == true || smart == true || rich == true)
cout << Hey, want to go on a date?;
Consider the above expression
If youre either cute, or smart, or rich, then Id
like to go out with you.
Lets say that I happen to know youre cute.
Once I know youre cute, I no longer need to
waste time asking the other two questions
Short Circuiting
bool cute = true;
s
d
e
!
e
p
p
s
u
s
i
h
T rogram
p
r
u
yo
16
16 < 2
16 > 8
if (eyes < 2 || eyes > 8 || eyes == 5)
cout << FREAK!\n;
Short Circuiting
17
int age;
cin >> age;
17 > 18??
if (age > 18 && smart == true && cute == true)
cout << <witty pickup line here>.\n;
else
cout << Uh See you later!\n;
Case 2: Your if statement uses all && as above
As soon as an expression (e.g. age > 17) is found to
be false, C++ knows that it doesnt need to
evaluate anything else.
Short Circuiting
int main(void)
{
int hw, sleep;
cout << "Enter hours of HW: ";
cin >> hw;
cout << "Enter hours of sleep: ";
cin >> sleep;
11 > 10???
if ( hw++ > 10 || ++sleep < 8 )
cout << You nerd!\n";
cout <<"hw is " << hw << "\n";
cout <<"sleep is " << sleep << "\n";
}
hw 50
12
11
sleep 69
10
n = 5;
for (i=1,f=1;i<=n;i++)
f *= i;
cout << n << factorial: << f;
...
n = 15;
for (i=1,f=1;i<=n;i++)
f *= i;
cout << n << factorial: << f;
...
n = 6;
for (i=1,f=1;i<=n;i++)
f *= i;
cout << n << factorial: << f;
}
Functions
In the previous example, we repeated the same
logic over and over and over
Not only does this make our program hard to
read, but it also may introduce bugs!
for (i=1,f=1; i<=n ;i++)
f *= i;
// calculate n factorial
cout << n << factorial: << f;
...
for (i=1,f=1; i<n ;i++)
f *= i;
// calculate n factorial
cout << n << factorial: << f;
}
Functions
A function is a special component of a program that
performs a certain task (like computing a factorial).
void fact(int n)
{
int i,f;
}
int main(void)
{
fact(5);
...
fact(15);
...
fact(6);
}
Functions
// call to a function
void cube(
{
3*3*3
2*2*2
int c = n*n*n;
n
c
2
3
8
27
cube(value);
3
cube(value+1);
}
value 93
2
314
return( a );
}
int main(void)
{
float r, area;
cout >> Enter radius: ;
cin >> r;
10
314
area = ComputeArea( r );
rad 10
a 314
-1
r 913
10
area 314
71
Enter radius: 10
Area: 314.00
Function Parameters
4.0
return( a );
}
int main(void)
{
float area;
// ERROR!
char x = A;
area = ComputeArea( 4.0
x );
cout << Area is: << area;
}
A value/variable passed to
the function is called an
actual parameter.
The actual parameter value
is copied into the formal
parameter variable when you
call the function.
Function Parameters
#include <iostream>
#include <cmath>
using namespace std;
float TotalFluid(int zits, float ozPerZit)
{
float totalOunces;
totalOunces = zits * ozPerZit;
return(totalOunces);
}
int main(void)
{
int num_zits = 4, total;
total = TotalFluid( num_zits , .52 );
cout << Ounces of fluid: << total;
}
Function Parameters
void praise(void)
{
cout << totally cool\n;
}
void tease() // ( ) is the same as void
{
cout << would like to be ;
praise();
}
int main(void)
{
cout << Carey is ;
praise();
cout << David S.;
tease();
cout << The end.\n;
}
Functions may
also have no
parameters.
In this case, you
should place the word
void in between the
parentheses in the
function definition.
Then, make sure you
dont pass any
parameters to the
function when you
call it.
18
18
180
18*10
Old and
cout <<
hairy!\n;
return(age*100);
}
int main(void)
{
int nh;
180
nh =
ComputeNoseHairs(18);
cout << Will has << nh <<
nose hairs!\n;
nh 9991
180
grade
85
Variable Rules
float ComputeArea( float rad )
{
float a;
a = 3.14 * rad * rad;
int main(void)
{
float area;
// ERROR!
return( a );
area = ComputeArea( 4 );
// ERROR!
Function Prototypes
void UCLARules(int x)
{
int j;
for (j=0;j<x;j++)
cout << UCLA Rules\n;
int main()
{
int n;
cin >> n;
UCLARules(n);
int main()
{
int n;
void UCLARules(int x)
{
int j;
cin >> n;
UCLARules(n);
}
for (j=0;j<x;j++)
cout << UCLA Rules\n;
}
Function Prototypes
In this example, a function
(main) calls another
function (UCLARules) that
is defined later in the
program.
This is like a novel that
refers to a character,
but the character is
introduced in a later
chapter.
Its confusing!
int main()
{
int n;
cin >> n;
UCLARules(n);
}
void UCLARules(int x)
{
int j;
for (j=0;j<x;j++)
cout << UCLA Rules\n;
}
Function Prototypes
This is also confusing for
the C++ compiler.
When compiling the main
function, it sees the
function call to UCLARules
but it doesnt know about
this function yet.
This results in an error.
int main()
{
int n;
cin >> n;
UCLARules(n);
}
void UCLARules(int x)
{
int j;
for (j=0;j<x;j++)
cout << UCLA Rules\n;
}
Function Prototypes
If you would like to call a
function defined later in the
program, you must add a special
line called a prototype above
your call to the function.
;
int main()
{
int n;
cin >> n;
UCLARules(n);
}
void UCLARules(int x)
{
int j;
for (j=0;j<x;j++)
cout << UCLA Rules\n;
}
Note: function
names are case
sensitive in C++.