• Create Account

### #ActualZael

Posted 25 April 2012 - 03:19 PM

I will see if I can take a moment to look at it more in-depth over the next few evening, but right off the bat, you can make it a lot cleaner if you look at using functions. Functions help you abstract different procedures and not only easily re-use code, but also make the code itself more readable. In case you haven't encountered them before I have included a small example.

#include <iostream>

using namespace std;

int sumOfRange(int a, int b);

int main()
{
int a, b;
cout << "Provide first number: ";
cin >> a;
cout << "Provide second number: ";
cin >> b;
cout << "The sum of all numbers between " << a << " and " << b << " is: " << sumOfRange(a, b) << endl;
int c,d;
cout << "Provide first number: ";
cin >> c;
cout << "Provide second number: ";
cin >> d;
cout << "The sum of all numbers between " << c << " and " << d << " is: " << sumOfRange(c, d) << endl;
}

int sumOfRange(int a, int b)
{
int total = 0;
for(int i = a; i <= b; i++)
{
total+=i;
}
}



The first thing I do is declare the function on line 5. All functions must be declared before using. A function is comprised of really three parts. The first is the return type. In this case the function returns an int. The second is the name of the function. This is how the function is called. The last thing is a list of parameters. I believe there is an upper limit on the number of parameters a function can have, but I have never hit that limit (it is pretty high). Each parameter is essentially a variable that is declared just like you would inside a function except that it is a copy of the whatever value was passed to the function.

The next thing I do is define and declare my main function. Inside my main function I use the the function I declared on line 5. Finally after my main function I define my function. The function definition is essentially the code that is executed when the code is used.

A minor, but essential thing to understand is that because parameters are copies, if you change the value of a parameter in a function the variable used when calling the function is unchanged.

#include <iostream>

using namespace std;

void functionThatDoesNothing(int a)
{

a = 10;
}

int main()
{
int a = 5;
cout << "The variable 'a' is: "<< a << endl;
functionThatDoesNothing(a);
cout << "The variable 'a' is still "<< a << endl;
return 0;
}


You can see from this example that the function does not change a. I have also shown that you can define the function at the same time you declare it. It is usually best to define and declare the function separately if you have more than one file (declarations go in the .h or .hpp file and definitions go in the .cpp file).

If you want to have the parameter directly affect the variable that was used when calling the function you can use what is called "pass by reference". Whether a function parameter is "pass by value" or "pass by reference" depends on the way it is declared in the function declaration. To make a variable "pass by reference" you simply place an '&' character before the variable name. Below I will demonstrate the same code as above, except with "pass by reference".

#include <iostream>

using namespace std;

void functionThatDoesSomething(int & a)
{

a = 10;
}

int main()
{
int a = 5;
cout << "The variable 'a' is: "<< a << endl;
functionThatDoesSomething(a);
cout << "The variable 'a' is now "<< a << endl;
return 0;
}


"Pass by reference" can get a little bit dangerous (your code will crash) if you use it in certain ways (like for the return type of a function), but for the way I have demonstrated above it should be safe in most if not all situations.

P.S. Classes and Structs can also make code a lot cleaner and more readable, but I think learning to write your own functions first is a nice step in the right direction.

### #1Zael

Posted 25 April 2012 - 03:16 PM

I will see if I can take a moment to look at it more in-depth over the next few evening, but right off the bat, you can make it a lot cleaner if you look at using functions. Functions help you abstract different procedures and not only easily re-use code, but also make the code itself more readable. In case you haven't encountered them before I have included a small example.

#include <iostream>

using namespace std;

int sumOfRange(int a, int b);

int main()
{
int a, b;
cout << "Provide first number: ";
cin >> a;
cout << "Provide second number: ";
cin >> b;
cout << "The sum of all numbers between " << a << " and " << b << " is: " << sumOfRange(a, b) << endl;
int c,d;
cout << "Provide first number: ";
cin >> c;
cout << "Provide second number: ";
cin >> d;
cout << "The sum of all numbers between " << c << " and " << d << " is: " << sumOfRange(c, d) << endl;
}

int sumOfRange(int a, int b)
{
int total = 0;
for(int i = a; i <= b; i++)
{
total+=i;
}
}



The first thing I do is declare the function on line 5. All functions must be declared before using. A function is comprised of really three parts. The first is the return type. In this case the function returns an int. The second is the name of the function. This is how the function is called. The last thing is a list of parameters. I believe there is an upper limit on the number of parameters a function can have, but I have never hit that limit (it is pretty high). Each parameter is essentially a variable that is declared just like you would inside a function except that it is a copy of the whatever value was passed to the function.

The next thing I do is define and declare my main function. Inside my main function I use the the function I declared on line 5. Finally after my main function I define my function. The function definition is essentially the code that is executed when the code is used.

A minor, but essential thing to understand is that because parameters are copies, if you change the value of a parameter in a function the variable used when calling the function is unchanged.

#include <iostream>

using namespace std;

void functionThatDoesNothing(int a)
{

a = 10;
}

int main()
{
int a = 5;
cout << "The variable 'a' is: "<< a << endl;
functionThatDoesNothing(a);
cout << "The variable 'a' is still "<< a << endl;
return 0;
}


You can see from this example that the function does not change a. I have also shown that you can define the function at the same time you declare it. It is usually best to define and declare the function separately if you have more than one file (declarations go in the .h or .hpp file and definitions go in the .cpp file).

If you want to have the parameter directly affect the variable that was used when calling the function you can use what is called "pass by reference". Whether a function parameter is "pass by value" or "pass by reference" depends on the way it is declared in the function declaration. To make a variable "pass by reference" you simply place an '&' character before the variable name. Below I will demonstrate the same code as above, except with "pass by reference".

#include <iostream>

using namespace std;

void functionThatDoesSomething(int & a)
{

a = 10;
}

int main()
{
int a = 5;
cout << "The variable 'a' is: "<< a << endl;
functionThatDoesSomething(a);
cout << "The variable 'a' is now "<< a << endl;
return 0;
}


"Pass by reference" can get a little bit dangerous (your code will crash) if you use it in certain ways (like for the return type of a function), but for the way I have demonstrated above it should be safe in most if not all situations.

PARTNERS