A subprogram is a part of your program that implements an independent algorithm that can be used from different places within the rest of the program
Develop a program that, given a number n of lines,
draws a triangle with asterisks ('*'
).
Example for n=5:
*
***
*****
*******
*********
Develop a program that, given a numerator and denominator, simplifies the rational number.
Develop a program that calculates the least common multiple of two given numbers.
Specification :What does it do?
Specification :What does it do?
Implementation :How does it do it?
Implementation :How does it do it?
are represented in C++ as
return_type function_name(type1 parameter1, type2 parameter2);
/**
* The function function_name does ...
* parameter1 represents ...
* parameter2 is ...
* returns the result ...
**/
return_type function_name(type1 parameter1, type2 parameter2);
/**
* The function repeat outputs the same charater a number of times.
*
* n represents the number of times
* c is the character that is repeated
* The function returns nothing
*/
void repeat(int n, char c);
/**
* The function gcf calculates the greatest common factor
* between two numbers.
*
* a y b are the two numbers
* The function returns the calculated greatest common factor
*/
int gcf(int a, int b);
What does this do? How is it different from previous examples?
Parameters are copied, so they do not change. Does not work!
/**
* Swaps real values
*
* a and b are the values to be swapped
* Returns nothing
*/
void swap(float a, float b);
Parameters are referenced, so they must be variables. Works!
/**
* Swaps the real values of the variables
*
* a and b are references to the variables
* Returns nothing
*/
void swap(float& a, float& b);
By default: when invoking, values of arguments are copied to the corresponding parameters
float x = 5; float y = 3;
swap(x,y);
a
gets the value of x
(5)b
gets the value of y
(3)
void swap(float a, float b);
swap(5,3);
a
gets the value of x
(5)b
gets the value of y
(3)
void swap(float a, float b);
When invoking, the argument variables are referenced (aliased). Whatever happens to the parameters happens to the variables
float x = 5; float y = 3;
swap(x,y);
a
gets a reference to x
b
gets a reference to y
void swap(float& a, float& b);
float x = 5; float y = 3;
swap(x,y);
//swap(5,3) does not compile
a
gets a reference to x
b
gets a reference to y
void swap(float& a, float& b);
float x = 5; float y = 3;
swap(x,y);
//swap(5,3) does not compile
a
happens to x
b
happens to y
void swap(float& a, float& b);
The output of a function can be represented as a reference parameter or as the return value of the function.
Output parameter | Return value |
---|---|
|
|
|
|
When possible, use return values instead of reference parameters.
When is that not possible?
struct BigType {
float v[1000000];
};
This is too costly:
void function(BigType bt);
This might modify it (and sometimes I don't want to):
void function(BigType& bt);
struct BigType {
float v[1000000];
};
void function(const BigType& bt);
More about this later in this course
return_type function_name(type1 parameter1, type2 parameter2);
return_type function_name(type1 parameter1, type2 parameter2) {
//Code that implements the behavior of the function
}
Where to locate the implementation code?
Where to locate the code?
repeat.h
|
triangle.cpp
|
repeat.h
repeat.cpp
|
triangle.cpp
Compile both
|
You need to compile all .cpp
files
g++ repeat.cpp triangle.cpp -o triangle
Better if done step by step (no need to recompile)
g++ repeat.cpp -c -o repeat.o
g++ repeat.o triangle.cpp -o triangle
More about this later on this course
When implementing a function, do not integrate with the rest of the software yet.
How? Create another main program with the only purpose of testing it.
There are tools for facilitating the testing process
When to write the test code?