function1.C
has all its code in the main
function.function2.C
has one function named
chorus
,
in addition to the main
function.
()
means that we are not attempting
to carry any information from the main
function
down to the chorus
function.
void
means that we are not attempting
to carry any information back from the chorus
function
back up to the main
function.
The product of all the integers from 1 to n is called n factorial, and is written n! with an exclamation point. Here are two examples:
3! = 1 × 2 × 3 = 6
4! = 1 × 2 × 3 × 4 = 24
One use of a factorial is to tell us how many orders n objects can be arranged in. n objects can be arranged in n! different orders. For example, the three objects a, b, c can be arranged in 6 different orders, and the four objects a, b, c, d can be arranged in 24 different orders:
automatic.C
:
create a variable
f
within the {
curly
braces}
of a function body.
In the above program,
we created the variable f
within the
{
curly
braces}
of the factorial
function.
When we create a variable within any pair of
{
curly
braces}
,
we can mention the name of the variable only
within those curly braces.
We therefore say that the variable has local scope.
In the above program, for example, the variable f
could not be mentioned in the main
function.
In the above program,
we created the variable f
within the
{
curly
braces}
of the factorial
function.
When we create a variable within any pair of
{
curly
braces}
as the computer executes the program,
the variable stays alive only until the computer reaches the closing curly brace
}
;
at that point the variable dies.
(Here’s what “death” means:
when a variable dies, it stops holding a value.)
We therefore say that the variable is automatically allocated.
In the above program, for example, the variable f
dies as we return form the factorial
function to the main
function.
If the computer executes the code within the
{
curly
braces}
a second time,
then the variables created within the braces are reincarnated.
In the following excerpt, for example,
a variable named j
is born holding the value 2
.
After it dies, another variable named j
is born holding the value 4
.
The two
j
’s
are two different variables,
living at different times and holding different values.
They just happen to have the same name and data type.
for (int i {1}; i <= 2; ++i) { //The loop iterates 2 times. int j {2 * i}; cout << j << "\n"; }
argument.C
:
pass an argument to a function.
graphpaper1.C
:
pass four arguments from the main
function to the
graphPaper
function.
graphpaper2.C
:
the
main
function calls the
graphPaper
function, which calls the
line
function.
Error checking omitted for brevity.
retval.C
main
function of
retval.C
,
call the
factorial
function 13 times to
output the following table of factorials.
for (int i {0}; i <= 12; ++i) { cout << setw(2) << i << " " << setw(9) << factorial(i) << "\n"; }and remember to
#include <iomanip>
for setw
.
0 1 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880 10 3628800 11 39916800 12 479001600
pi.C
:
a function named pi
that returns an approximation of the value of π.
The argument n
of the function specifies
how much work the function should do;
more work gives a better approximation.
We saw this algorithm in the old
pi.C
;
now it is neatly packaged here.
The
main
function concentrates on formatting the nice output table;
the
pi
function concentrates on the numerical computation.
true
if a year is leap.
string
s:
last hired, first fired.
stack1.C
:
a simplified version of the
stack.C
we saw earlier,
with all the code written in the main
function.
stack2.C
:
separate functions to
push
and
pop
the stack.
The contents of the stack are held in variables of
global scope and
static allocation.
These variables live as long as the program is running.
reference1.C
:
a reference
(r
)
to a variable
(j
).
reference2.C
:
demonstrate pass-by-value vs. pass-by-reference.
referencedate.C
:
Using three reference arguments,
a function can return three answers.
Also, more examples of static
variables inside a function.
referencedate.C
,
make the last argument of the function travel
optional.
Change the function declaration at the top of the program to
void travel(int& month, int& day, int& year, int distance = 1);Then change the statement that calls this function to
travel(month, day, year); //distance defaults to 1Only trailing arguments can be made optional.
recursion1.C
:
output the int
s from 1 to 10 inclusive
with a for
loop.
recursion2.C
:
output the int
s from 1 to 10 inclusive
without any loop at all.
=
or ++
at all.
recursion3.C
:
let the user specify the end point as well as the starting point.
main
function.
if
statement that prevents the function from calling itself if the job is already done.
factorial1.C
:
output the product of the int
s
from 1 to 10 inclusive
with a for
loop.
factorial2.C
:
output the product of the int
s
from 1 to 10 inclusive
without any loop at all.
maze.C
:
find a path through a maze by using recursion.
storm.cis.fordham.edu
,
change
cout << a[row][col];in
maze.C
to
if (a[row][col] == '.') { cout << "\033[48;5;9m.\033[0m"; //red background } else { cout << a[row][col]; }These crazy binary codes are the
xterm-256color
control codes.
For example,
48
for background,
38
for foreground;
9
for red,
10
for green,
etc.
sed
and have it look for every period
(.
):
jsmith@storm:~$ ./a.out | sed $'s/\./\033[48;5;9m.\033[0m/g'
'B'
to
'E'
,
not just any path.
Have the function f
return the length of the path
(or numeric_limits<int>::max()
if there is no path),
not just true
or false
.
Give the function an additional argument,
a bool
named retract
,
that will cause the function to erase the path it found
(i.e., change the periods back to blanks).
Then have the
for
loop in f
select the shortest path of the four possibilities,
not just any path that if finds.