42 Piscine/42 Piscine: Preparation Day 3/Pointer Functions

# Pointer Functions

This program introduces a function, `square`, which, when provided with a pointer to an integer, computes the square of the value at that memory location, effectively updating it. Subsequently, the `main` function exemplifies the utilization of the `square` function to determine the square of a numeric value. This C code exemplifies the interaction between functions and pointers, specifically showcasing the process of squaring an integer variable `x` by invoking the `square` function. The function operates directly on the variable's memory location using a pointer, leading to the calculation of the square.

```
#include <stdio.h>

void square(int *num) {
    *num = (*num) * (*num);
}

int main() {
    int x = 5;
    printf("Original value: %d\n", x);

    square(&x);

    printf("Squared value: %d\n", x);

    return 0;
}

```

## Code Breakdown

### #include <stdio.h>

* This line includes the standard input-output library (`stdio.h`), which provides functions for reading input and displaying output. It's essential to use functions like `printf`.

```
void square(int *num) {
    *num = (*num) * (*num);
}

```

* Here, a function named `square` is declared. This function takes a pointer to an integer (`int \*num`) as an argument. The function's purpose is to square the value pointed to by `num`. Inside the function, `\*num` is used to access the value pointed to by the pointer, and then it's squared using `(\*num) \* (\*num)`. The result is stored back in the same memory location pointed to by `num`.

* So, the `square` function takes a pointer to an integer as its parameter and calculates the square of the integer value stored at the memory location pointed to by `num`. This function modifies the value at that memory location, effectively squaring the input integer.

### void square(int \*num) {

* `void square(int \*num) {`: This line declares a function named `square`. It takes an argument, which is a pointer to an integer, denoted as `int \*num`. The `void` in front of the function name indicates that the function does not return a value.

### \*num = (\*num) \* (\*num);

* `\*num = (\*num) \* (\*num);`: Within the function, this line performs the squaring operation. Let's break it down further:
* `\*num`: Here, the asterisk (`\*`) is used to dereference the pointer `num`, which means accessing the value that `num` points to.
* `(\*num) \* (\*num)`: This is a mathematical expression that calculates the square of the value pointed to by `num`. It multiplies the value by itself, effectively squaring it.

### int main() {

* This line is the beginning of the `main` function, which is the entry point of a C program. It returns an integer (`int`) and takes no arguments in this case.

### int x = 5;

* Here, an integer variable named `x` is declared and initialized with the value `5`. This is the original value that will be squared using the `square` function.

### printf("Original value: %d\n", x);

* This line uses the `printf` function to display the original value of `x`. The `%d` format specifier is used to print an integer.

### square(&x);

* The `square` function is called with the address of `x` passed as an argument, denoted by `&x`. This means that the function will operate on the actual value of `x` in memory, not a copy of it. After this call, the value of `x` will be squared.

### printf("Squared value: %d\n", x);

* This line uses `printf` to display the squared value of `x`. After the call to the `square` function, the value of `x` has been modified to its square, so this line prints the squared value.

### return 0;

* The `return` statement is used to indicate that the `main` function is finished executing. It also returns an exit status of `0`, which typically indicates that the program terminated successfully.