Can't understand logic of perimeters

Hi, I am student currently studying computer games development. I am having trouble coming to terms with what parameters are really used for.
A lot of the time when I am learning I have to completely understand why something is used otherwise I just refuse to use it myself, especially with programming. If anyone could give me a good example or reason why I would want to use perimeters in my functions, I would greatly appreciate it!

Thanks

Ross
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.
Lets take a math equation:
y = 7*x + 42

When you replace the x with a number of your choosing, the equation can evaluate the corresponding y.

Lets turn that into a function:
1
2
3
4
int foo( int x )
{
  return 7*x + 42;
}

Now you can call in your code the function foo, but you have to give a value of x as parameter, and the function will return the corresponding value of y.

The function has code for how to do something, but the parameters deliver what to do it for.
Hey peeps, thanks a lot for your quick responses. Yeah I did mean "parameter", for some reason my browser told me it was an incorrect spelling when I first typed that, now I feel like a donk haha.

Thanks for the help, I honestly haven't looked at the logic in this way.

Ross
Autocorrect drives me bonkers -- and I still can't figure out how to turn it off.

I meant to spell what I spelt
Dumb autocorrect
Thinks it's smarter than I am
Topic archived. No new replies allowed.