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

Update the section about functions

parent eb58bd9c
......@@ -186,6 +186,8 @@ b = (x == 2 || y == 3 || z == 4);
b = (x != 0 && y / x > 1);
```
See https://en.cppreference.com/w/cpp/language/eval_order for details.
---
# Relational and Logical Operators
......@@ -233,6 +235,8 @@ bool b = std::abs(x-y) <= eps * std::abs(x+y) * ulp || std::abs(x-y) < min;
> .h3[Remark:] Mathematical functions often have an accuracy of a few ULPs, see
> [Errors-in-Math-Functions](https://www.gnu.org/software/libc/manual/html_node/Errors-in-Math-Functions.html)
See [boost/math_toolkit/float_comparison](https://www.boost.org/doc/libs/1_76_0/libs/math/doc/html/math_toolkit/float_comparison.html) for details.
---
# Precedence and Associativity of Operators
......
......@@ -7,6 +7,26 @@ class: center, middle
## Function Declaration/Definition
The declaration of a function in C++ follows
```c++
<return_type> <function_name> (<argument_list>...); // C-style, or
```
- 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) void print ()
{ {
return x*x; std::cout << "Hello World" << std::endl;
} }
```
---
# 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]
......@@ -16,15 +36,15 @@ auto <function_name> (<argument_list>...) -> <return_type>; // Since [C++11]
- To return a value from a function, C++ provides the keyword `return`.
```c++
double square (double const x)
{
return x*x;
}
double square (double const x) void print ()
{ {
return x*x; std::cout << "Hello World" << std::endl;
} }
// or
auto square2 (double const x) -> double
{
return x*x;
}
auto square2 (double const x) -> double auto print () -> void
{ {
return x*x; std::cout << "Hello World" << std::endl;
} }
```
---
......@@ -43,15 +63,15 @@ auto <function_name> (<argument_list>...); // Since [C++14]
- To return a value from a function, C++ provides the keyword `return`.
```c++
double square (double const x)
{
return x*x;
}
double square (double const x) void print ()
{ {
return x*x; std::cout << "Hello World" << std::endl;
} }
// or
auto square2 (double const x)
{
return x*x; // -> double
}
auto square2 (double const x) auto print ()
{ {
return x*x; /* -> double */ std::cout << "Hello World" << std::endl;
} }
```
---
......
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