GFreak45
I didnt slap you, i high 5'd your face.
- Reaction score
- 130
Used in and uses: Getting Started
Variables:
Variables are used to store some value. Each variable has a type, for example, a number, letter, sentence, etc. In C++ variables are defined like so:
where <type> is the type of value that is held, <name> is the identifier used to recognize that variable, and <initial value> is the value it is set to before any functions use the created variable. Variables can be modified via the assignment operator (=), like so: x = 3;
There are a few modifiers available, like const, which means the variable can not be modified. Variable modifiers include the following:
Arrays:
Arrays are a group of one type of variable, identified by a single number, and they are defined using the [] operator, ie:
this creates 5 variables of type int (index 0-4), and they are accessed via x[index], ie: x[0].
Arrays can also be initialized to some value, like so:
which would initialize the array x's indexes to the number in the corresponding place, so in that example, x[0] = 4, x[1] = 3, x[2] = 2, x[3] = 1, and x[4] = 0.
Functions:
Functions can be used as any variable of the same type can, however, they run their own actions before returning a value, ie:
the format for a function is: <type> <name> (<arguments>)
every function of a non-void type must return some value of its type before the function ends, the function someFunction will always return 0, so every time it is called, it is the same as using the number 0. Arguments are information sent to the function when calling it, they have specific types and there can be as many arguments as you decide when creating a function. ie:
This function takes 2 numbers of type int, and returns the sum. You can call this function like so:
Using initial values:
Using variables:
Using other functions:
Function Prototypes:
Function prototypes are used to allow the compiler to recognize each function without it's implementation being above the function it is called in.
This allows you to call functions below the function it is referenced in, ie:
This allows you to easily see the main.
Overloaded Functions:
Overloaded functions, are simply functions with the same name, that take different ammounts or types of arguments, even 1 difference is enough. This allows you to easily call different functions using different parameters.
To wrap this up, we are going to make an application that asks the user for a number, then runs a function to output it's square.
Output:
Enter a number: 20
The square of 20 is 400
Press any key to continue...
This is how I would create this application:
Variables:
Variables are used to store some value. Each variable has a type, for example, a number, letter, sentence, etc. In C++ variables are defined like so:
Code:
<type> <name> = <initial value>;
int x = 0;
There are a few modifiers available, like const, which means the variable can not be modified. Variable modifiers include the following:
- const - (const <type> <name> = <initial value>) constant variable, never changes
- * operator - (<type> * <name> = <initial value>) pointer variable, references a point in memory
- & operator - (<type> & <name> = <initial value>) reference to a variable, initialized to some variable and allows the user to reference the variable it was initialized to
- ^ operator - (<type> ^ <name> = <initial value>) handle to a variable (garbage collected (for reference classes)) (these are only in VC++ or windows OS specific C++)
- int - integer, no decimal number, 4 bytes
- bool - boolean variable, true or false, 1 byte
- char - integer, no decimal number, 1 byte
- short - integer, no decimal, 2 bytes
- long - integer, no decimal, 8 bytes
- float - floating point number, decimal points allowed, 4 bytes
- double - floating point number, decimal points allowed, 8 bytes
- wide - floating point number, decimal points allowed, 16 bytes
Code:
unsigned int x = 0;
Arrays are a group of one type of variable, identified by a single number, and they are defined using the [] operator, ie:
Code:
int x [5];
Arrays can also be initialized to some value, like so:
Code:
int x [] = { 4, 3, 2, 1, 0 };
Functions:
Functions can be used as any variable of the same type can, however, they run their own actions before returning a value, ie:
Code:
int someFunction () { return 0; }
//calling this function: someFunction(), IE:
int x = someFunction()
every function of a non-void type must return some value of its type before the function ends, the function someFunction will always return 0, so every time it is called, it is the same as using the number 0. Arguments are information sent to the function when calling it, they have specific types and there can be as many arguments as you decide when creating a function. ie:
Code:
int add (int a, int b) { return a + b; }
Using initial values:
Code:
int x = add(1, 1);
Code:
int x = 0;
int y = 1;
int z = add(x, y); //or add(x, 1), etc
Code:
int x = add(add(1, 1), add(1, 1)); //4
Function prototypes are used to allow the compiler to recognize each function without it's implementation being above the function it is called in.
This allows you to call functions below the function it is referenced in, ie:
Code:
int add (int a, int b);
int main ()
{
int x = add(1, 2);
return 0;
}
int add (int a, int b)
{
return a + b;
}
Overloaded Functions:
Overloaded functions, are simply functions with the same name, that take different ammounts or types of arguments, even 1 difference is enough. This allows you to easily call different functions using different parameters.
To wrap this up, we are going to make an application that asks the user for a number, then runs a function to output it's square.
Output:
Enter a number: 20
The square of 20 is 400
Press any key to continue...
This is how I would create this application:
Code:
#include <iostream>
using namespace std;
int square (int);
int main ()
{
cout << "Enter a number: ";
int x = 0;
cin >> x;
cout << "The square of " << x << " is: " << square(x) << "\n";
system("Pause");
return 0;
}
int square (int a)
{
return a * a;
}