C++ Tutorial: Variables and Functions

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:
Code:
<type> <name> = <initial value>;
int x = 0;
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:
  • 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++)
There are also multiple variable types, each of the following can be signed or unsigned, allowing negative or unallowing negative values, ie:
  • 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
Unsigned/signed variables are written like so:
Code:
unsigned int x = 0;
Arrays:
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];
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:
Code:
int x [] = { 4, 3, 2, 1, 0 };
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:
Code:
int someFunction () { return 0; }
//calling this function: someFunction(), IE:
int x = someFunction()
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:
Code:
int add (int a, int b) { return a + b; }
This function takes 2 numbers of type int, and returns the sum. You can call this function like so:
Using initial values:
Code:
int x = add(1, 1);
Using variables:
Code:
int x = 0;
int y = 1;
int z = add(x, y); //or add(x, 1), etc
Using other functions:
Code:
int x = add(add(1, 1), add(1, 1)); //4
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:
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;
}
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:
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;
}
 

UnknowVector

I come from the net ... My format, Vector.
Reaction score
145
GFreak45 said:
^ operator - (<type> ^ <name> = <initial value>) handle to a variable (garbage collected (for reference classes))
Handles are not a part of standard C++. There are, to the best of my understanding--which is somewhat lacking because the C++ ecosystem is pretty confusing--an addition that came from Microsoft's Common Language Infrastructure and are available in C++/CLI language, which is only supported by the Visual Studio family of runtimes and compilers. You should at least make mention of this, though you did say in the other thread that you were targeting visual studio.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
I normally would not but I plan on branching of into a VC++ tutorial chain as well, so i knew it would be needed, ill edit it to make sure that is mentioned
Next tutorial to come out will probably be strings and vectors as well as proper object passing (via reference etc)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    I will be out of town until Sunday evening
    +1
  • The Helper The Helper:
    I am back! Did you miss me LOL
    +1
  • jonas jonas:
    where did you go?
  • The Helper The Helper:
    Jefferson TX on a Paranormal Investigation of a haunted bed and breakfast - I got some friends that are paranormal investigators and they have an RV and do YouTubes
    +1
  • The Helper The Helper:
    It was a lot of fun. The RV was bad ass
  • jonas jonas:
    That sounds like fun!
    +1
  • The Helper The Helper:
    it was a blast!
  • The Helper The Helper:
    I am going to post the Youtube of the investigation in the forums when it is ready
    +1
  • jonas jonas:
    cool!
  • vypur85 vypur85:
    Sounds cool TH.
  • tom_mai78101 tom_mai78101:
    I was on a Legend of Zelda marathon...
  • tom_mai78101 tom_mai78101:
    Am still doing it now
    +1
  • jonas jonas:
    which one(s) are you playing?
  • jonas jonas:
    I played a little bit of the switch title two weeks ago and found it quite boring
  • The Helper The Helper:
    just got back from San Antonio this weekend had the best Buffalo Chicken Cheesesteak sandwhich in Universal City, TX - place was called Yous Guys freaking awesome! Hope everyone had a fantastic weekend!
    +1
  • The Helper The Helper:
    Happy Tuesday!
  • The Helper The Helper:
    We have been getting crazy numbers reported by the forum of people online the bots are going crazy on us I think it is AI training bots going at it at least that is what it looks like to me.
  • The Helper The Helper:
    Most legit traffic is tracked on multiple Analytics and we have Cloud Flare setup to block a ton of stuff but still there is large amount of bots that seem to escape detection and show up in the user list of the forum. I have been watching this bullshit for a year and still cannot figure it out it is drving me crazy lol.
    +1
  • Ghan Ghan:
    Beep boop
    +1
  • The Helper The Helper:
    hears robot sounds while 250 bots are on the forum lol
  • The Helper The Helper:
    Happy Saturday!
    +1
  • The Helper The Helper:
    and then it was Thursday...
    +2
  • tom_mai78101 tom_mai78101:
    And then Monday
    +1
  • The Helper The Helper:
    I got the day off today!
    +1
  • tom_mai78101 tom_mai78101:
    How...? (T-T)

    The Helper Discord

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top