Mentions légales du service

Skip to content
Snippets Groups Projects

Hands on how to: add a section about pros and cons on separating or not declarations and definitions

1 unresolved thread
1 file
+ 72
2
Compare changes
  • Side-by-side
  • Inline
+ 72
2
First create a folder in which the executables will be built and initialize CMake here:
First create a folder in which the executables will be built and initialize CMake here:
```
```shell
mkdir -p build
mkdir -p build
cd build
cd build
cmake ..
cmake ..
and if everything is alright you should see (in the case of the [first hands-on](../1-ProceduralProgramming/2b-hands-on.ipynb#EXERCISE-1:-Adding-a-loop)):
and if everything is alright you should see (in the case of the [first hands-on](../1-ProceduralProgramming/2b-hands-on.ipynb#EXERCISE-1:-Adding-a-loop)):
```
```shell
0.65 ~ 1 / 2^1
0.65 ~ 1 / 2^1
0.65 ~ 3 / 2^2
0.65 ~ 3 / 2^2
0.65 ~ 5 / 2^3
0.65 ~ 5 / 2^3
For more information about the Docker way please look the dedicated [README](README.md) (the one within the _HandsOn_ folder of the root directory).
For more information about the Docker way please look the dedicated [README](README.md) (the one within the _HandsOn_ folder of the root directory).
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
 
## Declaration and definition
 
%% Cell type:markdown id: tags:
 
We saw briefly [here](http://localhost:8888/lab/tree/1-ProceduralProgramming/4-Functions.ipynb#Function-declaration) and [there](http://localhost:8888/lab/tree/1-ProceduralProgramming/4-Functions.ipynb#Function-definition) what is the difference between the declaration and the definition of a function (and also from other stuff such as objects that we'll see in part 3).
 
 
We'll explain in a [late notebook](http://localhost:8888/lab/tree/6-InRealEnvironment/2-FileStructure.ipynb) how to properly structure a project with several files, but for the hands-on the idea is not to bother with this and each exercise is fully present in one file (see above).
 
 
Therefore, you are not exactly required to separate declaration and definitions, and we don't dictate the choice you should do. We can nonetheless give you the pros and cons of each approach:
 
 
%% Cell type:markdown id: tags:
 
### No declaration - only definitions
 
 
In this configuration, when you use a function you provide both the signature of the function and its implementation. For instance:
 
%% Cell type:code id: tags:
 
``` C++17
 
void DisplayPowerOf2Approx(double value)
 
{
 
... // implementation is provided here
 
}
 
%% Cell type:markdown id: tags:
 
The advantage is that when you need to modify the signature you only do it in one place.
 
 
The drawback though is that if you're using such a function in another function, you must be sure it is already defined beforehand.
 
 
So you might have to reshuffle the order of your functions in the exercises to ensure they are defined before use.
 
%% Cell type:markdown id: tags:
 
### Declaration and definition
 
%% Cell type:markdown id: tags:
 
This configuration is much closer to what you would do in an actual code.
 
 
The idea is that at the beginning of the file you first place all the declarations:
 
%% Cell type:code id: tags:
 
``` C++17
 
// Beginning of the file
 
void DisplayPowerOf2Approx(double value);
 
%% Cell type:markdown id: tags:
 
and that after that you provide the actual definitions, e.g.:
 
%% Cell type:code id: tags:
 
``` C++17
 
void DisplayPowerOf2Approx(double value)
 
{
 
... // implementation is provided here
 
}
 
%% Cell type:markdown id: tags:
 
You should figure out the pros and cons by reading the alternate case: the drawback is that you have to modify the signatures at two locations if you do that, but you don't have to bother about the functions ordering in the definition part.
 
%% Cell type:markdown id: tags:
Loading