Am I to assume that you mean "parameters" every time you write "perimeters"? Because they are two totally different things...
Why is the idea of a parameter so difficult?
Person A: "Hey, I'm making sandwiches. What do you want?"
Person B: "I don't believe in parameters. I'll just take the two slices of bread."
Plumber: "There you go Mrs Johnson. Your bathroom has been entirely replaced. That'll be $7,000."
Mrs Johnson: "But... I only had a leaky faucet."
Plumber: "Sorry. I don't use parameters. It's all or nothing."
I'm not trying to be mean -- just to illustrate. A
parameter is a modifier -- it tells us specifics about something.
I want a
peanut butter and jam sandwich.
I want a list of the
first twelve Fibonacci numbers.
It is through parameters that the functionality of our code gains variability.
Now, whether you pass them as argument or just maintain a giant list of global structures doesn't really matter -- they are still parameters to your code. (But you really should try to localize stuff.)
Parameterizing functions is a basic computational principle that lets you reuse code to operate on different data.
Here's a (very simple) example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <vector>
using namespace std;
void print( const string& name, vector <int> xs )
{
cout << name << ": ";
for (int x : xs)
cout << x << " ";
cout << endl;
}
int main()
{
vector <int> primes = {{2, 3, 5, 7, 11, 13, 17, 19}};
vector <int> fibonaccis = {{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}};
print( "Some primes", primes );
print( "Fibonacci numbers", fibonaccis );
}
|
Conveniently, I used the
same function to print two totally different lists of numbers!
Hope this helps.