Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

 C++ References #include <iostream>

Creating References. A reference variable is a #include <string>


using namespace std;
"reference" to an existing variable, and it is
created with the & operator:
int main() {
string food = "Pizza";

cout << &food;


Now, we can use either the variable return 0;
name food or the reference }
name meal to refer to
the food variable: Note: The memory address is in
hexadecimal form (0x..). Note that
#include <iostream> you may not get the same result in
#include <string> your program.
using namespace std;

int main() {
string food = "Pizza";
string &meal = food;
 C++ Pointers
cout << food << "\n";
cout << meal << "\n"; Creating Pointers. You learned from the
return 0; previous chapter, that we can get
} the memory address of a variable by
using the & operator:
 Memory Address
#include <iostream>
In the example from the previous page, #include <string>
the & operator was used to create a using namespace std;
reference variable. But it can also be used to
get the memory address of a variable; which int main() {
is the location of where the variable is stored string food = "Pizza";
on the computer.
cout << food << "\n";
When a variable is created in C++, a memory
cout << &food << "\n";
address is assigned to the variable. And when
return 0;
we assign a value to the variable, it is stored
}
in this memory address.

To access it, use the & operator, and the A pointer however, is a variable


result will represent where the variable is that stores the memory address as
stored: its value.

1
A pointer variable points to a data Tip: There are three ways to declare
type (like int or string) of the same pointer variables, but the first way is
type, and is created with preferred:
the * operator. The address of the
variable you're working with is
assigned to the pointer:

#include <iostream>
#include <string>
using namespace std;  C++ Dereference

Get Memory Address and Value. In the


int main() {
example from the previous page, we
string food = "Pizza"; // A string variable
used the pointer variable to get the
string* ptr = &food; // A pointer variable
that stores the address of food memory address of a variable (used
together with
// Output the value of food the & reference operator). However,
cout << food << "\n"; you can also use the pointer to get
the value of the variable, by using
// Output the memory address of food the * operator
cout << &food << "\n"; (the dereference operator):

// Output the memory address of food with #include <iostream>


the pointer #include <string>
cout << ptr << "\n"; using namespace std;
return 0;
} int main() {
string food = "Pizza"; // Variable
declaration
Example above explained. Create a string* ptr = &food; // Pointer declaration
pointer variable with the name ptr,
that points to  a string variable, by // Reference: Output the memory address
using the asterisk sign * (string* of food with the pointer
ptr). Note that the type of the cout << ptr << "\n";
pointer has to match the type of the
variable you're working with. // Dereference: Output the value of food
with the pointer
Use the & operator to store the cout << *ptr << "\n";
memory address of the variable
return 0;
called food, and assign it to the
}
pointer.

Now, ptr holds the value of food's Note that the * sign can be confusing


memory address. here, as it does two different things
in our code:

2
 When used in declaration  C++ Functions
(string* ptr), it creates
a pointer variable. A function is a block of code which only runs
 When not used in declaration, when it is called.
it act as a dereference
operator. You can pass data, known as parameters, into
a function.
 C++ Modify Pointers
Functions are used to perform certain actions,
and they are important for reusing code:
Modify the Pointer Value. You can
also change the pointer's value. But Define the code once, and use it many times.
note that this will also change the
value of the original variable: Create a Function:

C++ provides some pre-defined


#include <iostream> functions, such as main(), which is
#include <string> used to execute code. But you can
using namespace std; also create your own functions to
perform certain actions.
int main() {
string food = "Pizza"; To create (often referred to
string* ptr = &food; as declare) a function, specify the
name of the function, followed by
// Output the value of food parentheses ():
cout << food << "\n";

// Output the memory address of food


cout << &food << "\n";

// Access the memory address of food and


output its value
cout << *ptr << "\n";

// Change the value of the pointer Example above explained:


*ptr = "Hamburger";
 myFunction() is the name of the
// Output the new value of the pointer function
cout << *ptr << "\n";  void means that the function
does not have a return value.
// Output the new value of the food You will learn more about
variable return values later in the next
cout << food << "\n"; chapter
return 0;  inside the function (the body),
}
add code that defines what the
function should do

3
Call a Function: #include <iostream>
using namespace std;
Declared functions are not executed
immediately. They are "saved for later use",
void myFunction() {
and will be executed later, when they are cout << "I just got executed!\n";
called. }
To call a function, write the function's name
followed by two parentheses () and a int main() {
semicolon ; myFunction();
myFunction();
In the following example, myFunction() is myFunction();
used to print a text (the action), when it is return 0;
called: }

Inside main, call myFunction():
Function Declaration and
#include <iostream> Definition:
using namespace std;
A C++ function consist of two parts:
void myFunction() {
cout << "I just got executed!";  Declaration: the return type,
} the name of the function, and
parameters (if any)
 Definition: the body of the
int main() {
function (code to be executed)
myFunction();
return 0;
}

A function can be called multiple


times:
Note: If a user-defined function,
such as myFunction() is declared after
the main() function, an error will
occur:

4
#include <iostream>
using namespace std;

// Function declaration
void myFunction();

// The main method


int main() {
myFunction(); // call the function
return 0;
}

// Function definition
#include <iostream> void myFunction() {
using namespace std; cout << "I just got executed!";
}
int main() {
myFunction();
return 0;
}

void myFunction() {
cout << "I just got executed!";
}

However, it is possible to separate


the declaration and the definition of
the function - for code optimization.

You will often see C++ programs


that have function declaration
above main(), and function definition
below main(). This will make the code
better organized and easier to read:

You might also like