Commit eb58bd9c authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Cleanup chapter about functions and references

parent 2afc9a26
......@@ -186,8 +186,6 @@ b = (x == 2 || y == 3 || z == 4);
b = (x != 0 && y / x > 1);
```
> .h3[Principle:] Never rely on a function call in logical expressions!
---
# Relational and Logical Operators
......@@ -309,6 +307,8 @@ int n3, n4 , n5;
# Precedence and Associativity of Operators
## Summary
https://en.cppreference.com/w/cpp/language/operator_precedence
| **Priority** | **Associativity** | **Operators** |
|--------------|--------------------|----------------|
| highest | left | `++`, `--` (postfix) |
......@@ -320,4 +320,3 @@ int n3, n4 , n5;
| | left | `&&` |
| | left | \(\vert\vert\) |
| lowest | right | `=`, `+=`, `-=`, `*=`, `/=`, `%=` |
......@@ -8,8 +8,7 @@ class: center, middle
The declaration of a function in C++ follows
```c++
<return_type> <function_name> (<argument_list>...); // C-style
// or
<return_type> <function_name> (<argument_list>...); // C-style, or
auto <function_name> (<argument_list>...) -> <return_type>; // Since [C++11]
```
......@@ -30,12 +29,39 @@ auto square2 (double const x) -> double
---
# Functions
## Function Declaration/Definition
The declaration of a function in C++ follows
```c++
<return_type> <function_name> (<argument_list>...); // C-style, or
auto <function_name> (<argument_list>...) -> <return_type>; // Since [C++11], or
auto <function_name> (<argument_list>...); // Since [C++14]
```
- When a function does not return a result, then the return type is `void`.
- To return a value from a function, C++ provides the keyword `return`.
```c++
double square (double const x)
{
return x*x;
}
// or
auto square2 (double const x)
{
return x*x; // -> double
}
```
---
# Functions
## Function Call
A function is called by
```c++
<function_ name>(<argument1>, <argument2>...);
<function_name>(<argument1>, <argument2>...);
```
### Example:
......@@ -227,7 +253,7 @@ returning a `std::tuple`
auto min_max (int const n1, int const n2)
{
if (n1 < n2)
return std::tuple{n1,n2};
return std::tuple{n1,n2}; // [C++17]
else
return std::tuple{n2,n1};
}
......
......@@ -548,7 +548,8 @@ int* pn1; // non-const pointer to non-const var.
int const* pn2; // non-const pointer to const var.
int* const pn3; // const pointer to non-const var.
int const* const pn4; // const pointer to const var.
int const& const pn5; // ERROR
int const& re1 = <var>; // reference to const var
int const& const re2 = <var>; // ERROR: ref cannot be const
```
If the pointer is constant, the address it is directing to cannot be
......@@ -566,6 +567,31 @@ pn3 = &m; // Error: cannot assign to 'pn3', variable 'pn3' declared
---
# Variables and Datatypes
## Differences between Pointer and Reference
- You can change the location where the pointer points to (except if the pointer is `const`)
- A pointer variable is itself an object with an identity (like an integer variable). Thus, stored in memory and can be addressed. You can take a reference/pointer to it.
```c++
int i = 42;
int* p = &i;
int** pp = &p;
int*** ppp = &pp; // ...
int*& rp = p; // reference to pointer
```
- References bind to a fixed object/data. This cannot be changed.
- A reference is not an object itself. It cannot be referenced.
```c++
int i = 42;
int& ri = i;
int&& rri = ri; // Error: cannot bind [...] ‘int&&’ to [...] ‘int’
int&* pri = &ri; // Error: cannot declare pointer to ‘int&’
```
---
# Variables and Datatypes
## Placeholder Type
For variables, `auto` specifies that the type of the variable that is being declared will be
......@@ -579,6 +605,7 @@ decltype(auto) <variable name> = <expression>; // (2.)
type of `<expression>` \(\rightarrow\) "raw type"
2. type `decltype(<expression>)`, possibly including modifiers and qualifiers
<br>
The placeholder `auto` may be accompanied by modifiers, such as `const` or `&`, which will participate in the type deduction.
---
......
int main()
{
int i = 42;
int i0 = 0;
int j = i/i0;
}
\ No newline at end of file
int main()
{
int i = 42;
int& ri = i;
int&& rri = ri;
int&* pri = &ri;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment