# Revision Exercise - 3

## Exercise 0 - Variables and Constants

### Part 0 - Constants

1. How are **constant variables** defined?&#x20;
2. What is the difference between a **constant variable** and a regular variable?&#x20;
3. What is the `constexpr` keyword? And when do you use `constexpr` as opposed to `const`? (see: <https://docs.microsoft.com/en-us/cpp/cpp/constexpr-cpp?view=msvc-160#constexpr-variables>)
4. What is the `typedef` keyword, and when is it used? Show an example.

### Part 1 - Auto variables

Based on the following statements, state if the statements are **true** or **false**.&#x20;

* An `auto` variable can be re-assigned to values of any type (i.e. it is typeless)
* The underlying type of an `auto` variable is determined by the compiler&#x20;
* All `auto` variables have to be initialised on declaration&#x20;

## Exercise 1 - Arrays&#x20;

### Part 0 - Arrays

1. Show an example defining an array of any primitive type (`int`, `float`, `long`, etc) and initialising its elements.&#x20;
2. Show an example defining a 2D array of any primitive type and initialising its elements.&#x20;
3. Within the same example as 2, write a loop that enumerates over every element in the 2D array, and sums its values together.&#x20;

### Part 1

*coming soon*

## Exercise 2 - Functions

### Part 0 - Declaring and Defining

1. What is a **function prototype**?&#x20;
2. Write the function prototype for a function named `getArea` that accepts two float parameters, and returns another float parameter.&#x20;
3. When can two functions be defined with the same name?
4. When can two functions be defined with the same name and return type?
5. When can two functions be defined with the same function prototype?&#x20;
6. What is **function overloading**?
7. What is a **function definition**, and how does it differ from a **function prototype**?

### Part 1 - Function Parameters

1. How do you define a function with default parameter values?
2. What is a reference function parameter, and how does it differ from a regular function parameter?
3. If an object is passed by reference, will its copy constructor be invoked?

### Part 2 - Arrays & Functions

1. What is meant by **array decay**? Show an example of this.&#x20;
2. How do we pass an array into a function? Show an example of passing an array into a function that enumerates over the array and prints its contents.&#x20;

### Part 3 - Lambda Functions

1. What are lambda functions?
2. What is the syntax for a lambda function?&#x20;
3. Show a simple example using a lambda function, making sure to show the use of the 'captures' feature.

## Exercise 3 - Pointers

### Part 0 - Variables and Pointers

1. What are pointers?&#x20;
2. How can we get the memory address of a variable? Show an example of getting an address of a variable, and storing it into a pointer.&#x20;
3. How do we retrieve the value stored at a memory address through a pointer? Show an example of printing a value stored at a certain memory address using a pointer.&#x20;
4. What is the size of an `int*`, `float*` and `double*`? Explain your answer.&#x20;

### Part 1 - Dynamic Values

1. What is the `new` keyword, and what does it return? Show an example of using the `new` keyword to dynamically allocate a `float`.
2. Is memory allocated with `new` automatically released? If not, how do we release memory that was allocated with the `new` keyword?&#x20;
3. How does the syntax for releasing dynamically allocated memory differ for **dynamically allocated values,** and **dynamically allocated arrays**?&#x20;
4. What is the implication of not releasing memory that was dynamically allocated?&#x20;
5. What is the implication of releasing memory twice?&#x20;

### Part 2 - Constant Pointers

Study each snippet below, and explain whether:

* The memory address stored by the pointer `num_ptr` can be changed
* The data stored at the address of `num_ptr` can be changed

```cpp
int num = 30;
int* const num_ptr = &num;

*num_ptr = 20;  // OK?

int number_2 = 20;
number_ptr = &number_2;  // OK?
```

```cpp
int num = 30;
const int* num_ptr = &num;

*num_ptr = 20;  // OK?

int num2 = 20;
num_ptr = &num2;  // OK?
```

```cpp
int num = 30;
const int* const num_ptr = &num;

*num_ptr = 20;  // OK?

int num2 = 20;
num_ptr = &num2;  // OK?
```

### Part 3 - References&#x20;

(Refer to the textbook, page 205 if you need help on this)

* What are **reference variables**? Show an example of creating one.
* How are **reference variables** similar to **reference function parameters**?
