1/16/2023
#include <iostream>
using namespace std;
int main () {
int numLines;
int i;
numLines = 5;
cout << "----------" << endl;
for (i=1; i<=numLines; i=i+1) {
cout << "| |" << endl;
}
cout << "----------" << endl;
return 0;
}
1
1/16/2023
To draw as many rectangles as we want, we can copy the
code which generates the rectangle to the appropriate
location in the program.
There are several disadvantages to this approach :
The program gets longer
More complicated
If a change has to be made this change must be made in all the
places where the code was copied.
It would be more efficient if we can write the code which
generates the rectangle once and then execute the code
whenever a rectangle is required.
The code which draws the rectangle is :
int numLines;
int i;
numLines = 5;
cout << "----------" << endl;
for (i=1; i<=numLines; i=i+1) {
cout << "| |" << endl;
}
cout << "----------" << endl;
2
1/16/2023
We can put the code which draws the rectangle inside a
function.
Whenever we wish to draw the rectangle, we can call upon
the function to do the job.
By putting the code within a function, it only has to be written
once in the program.
void drawRectangle () {
int numLines;
int i;
numLines = 5;
cout << "----------" << endl; Same as before
but now enclosed
for (i=1; i<=numLines; i=i+1) { in curly brackets
cout << "| |" << endl;
}
cout << "----------" << endl;
3
1/16/2023
void drawRectangle ()
• Keyword void • Brackets written
immediately after
• Name of the function
the function name
• Every function must
have a name
• Rules for naming a
function are the
same as for naming
a variable
In order to get a function to do its job, it must be called.
It is called by writing its name followed by the opening and
closing brackets.
drawRectangle();
It is written in the main part just like in previous programs
when we called the ceil and floor functions
4
1/16/2023
To keep things simple, we will write it just above the code
for the main part.
#include <iostream>
using namespace std;
void drawRectangle () {
int numLines;
int i;
numLines = 5;
cout << "----------" << endl;
for (i=1; i<=numLines; i=i+1) {
cout << "| |" << endl;
}
cout << "----------" << endl;
}
int main () { • The drawRectangle function is
drawRectangle(); called twice from within
main.
cout << endl; • Each time it is called, the
drawRectangle(); rectangle is displayed on the
monitor.
return 0;
}
5
1/16/2023
When a program is executed, the code in the main part is
executed first, starting from the top.
The code in a function is only executed when the function is
called.
1. #include <iostream>
2.
3. using namespace std;
4.
5. void drawRectangle() {
6.
7. int numLines;
8. int i;
9.
10. numLines = 5;
11.
12. cout << "----------" << endl;
13.
14. for (i=1; i<=numLines; i=i+1) {
15. cout << "| |" << endl;
16. }
17.
18. cout << "----------" << endl;
19.
20. }
21.
22. int main () {
23.
24. drawRectangle();
25.
26. cout << endl;
27.
28. drawRectangle();
29.
30. return 0;
31. }
Execution of
the program
starts here
6
1/16/2023
Variables declared inside a function are known (or are
visible) only to the code inside the function.
If an attempt is made to access a variable declared inside a
function from another segment of code a compilation error
occurs, indicating that the variable is not declared.
int main () {
cout << "Rectangles with " << numLines << " lines:" << endl; The code will generate
a compilation error
drawRectangle(); since numLines is not
known outside of the
cout << endl; drawRectangle()
function:
drawRectangle();
return 0;
}
There is a way to assign a value to outside a function.
In order to do so, the variable must be declared as a
parameter, rather than as a “regular” variable.
Parameters are like windows into a function; they allow
code from other parts of a program (e.g., the main part) to
send a value into a function.
In the drawRectangle() function given so far, numLines was
declared as a “regular” variable by declaring it within the
body of the function.
To declare numLines as a parameter, it must be declared
within the brackets after the name of the function as
follows:
7
1/16/2023
To declare numLines as a parameter, it must be declared
within the brackets after the name of the function as
follows:
void drawRectangle (int numLines) {
Since numLines is declared as a parameter, it
int i; cannot be re-declared as a “regular” variable
within the function.
cout << "----------" << endl;
for (i=1; i<=numLines; i=i+1) {
cout << "| |" << endl;
}
cout << "----------" << endl;
There is no need to assign a value such as 5 to the numLines
parameter within the function.
When the function is called, a value will be assigned to the
numLines parameter.
This causes the value 8 to be
drawRectangle(8); assigned to the numLines
parameter when the function
in called.
8
1/16/2023
Consider the drawRectangle() function below:
void drawRectangle (int numLines) {
.
.
.
}
The numLines variable which is declared as an integer parameter of
the drawRectangle() function is referred to as a formal parameter.
Any other integer value which is passed to the drawRectangle()
function when it is called is referred to as an actual parameter.
The actual parameter can also be a variable or expression which evaluates to a
value of the same type as the formal parameter.
Type of Parameter When Used Example Code
void drawRectangle(int numLines) {
When declaring a function, within
.
brackets, after the name of the
Formal parameter .
function.
.
}
When calling a function, within drawRectangle(8);
brackets, after the name of the function;
it must evaluate to a value of the same
Actual parameter drawRectangle(i);
type as the corresponding formal
parameter.
drawRectangle(i*2);
9
1/16/2023
Sometimes we need the value calculated in a function to
use in the main or in some other function.
All of the functions we have written before have used the
word void before the name of the function .
The void before the name of the function indicates that the
function does not return a value.
If a function returns a value, void should be replaced with
the type of the value being returned.
int sum1toN(int n) {
int i;
int sum;
sum = 0; The function returns
and integer value.
for (i=1; i<=n; i=i+1) {
sum = sum + i;
}
return sum;
10
1/16/2023
name of function
return type formal parameter list
int sum1toN(int n) {
int i;
int sum;
sum = 0;
“body” of function
for (i=1; i<=n; i=i+1) {
sum = sum + i;
}
return sum;
}
return value (type must
correspond to return type)
In the main part, an actual parameter must be sent
to the function when it is called.
This value will be assigned to the formal parameter,
n.
Since the function returns an integer value to the
main part, an integer variable should be used to
receive the value.
11
1/16/2023
int main () {
int total;
double average;
total = sum1toN(10);
average = total * 1.0 / 10.0;
cout << "The sum of the numbers is: " << total; total contains the
cout << endl; value of the returned
cout << "The average of the numbers is: " << average; from the sum1toN
function.
return 0;
}
12
1/16/2023
When declaring a function,
The return type of the function must be specified, followed by the name of
the function, followed by the formal parameter list enclosed within
brackets;
If the function does not return a value, the return type should be void;
If a function does not accept parameters, the formal parameter list should
be empty;
If a function returns a value, somewhere in the body of the function (i.e., the
code enclosed within the braces), there must be a return statement that
returns a value of the same type specified as the return type of the
function.
When calling a function:
The same name that was used to declare the function must be used;
The number of actual parameters must match the number of formal parameters
used to declare the function;
The type of each actual parameter must match the type of each formal
parameter (in left-to-right ordering);
If a function does not return a value, simply call the function, supplying actual
parameters as necessary.
If a function returns a value, the code that calls the function should have a
statement that stores the value returned by the function in a variable. It is then
free to manipulate this variable, like any other variable.
13