Skip to content

Commit

Permalink
feat: added functions with trailing types in cpp/function.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh672003 committed Nov 10, 2023
1 parent fd71829 commit 9420a68
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CPP/07-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,31 @@ int multiplyNumbers(int x, int y) {
In this example, we use a function prototype for `multiplyNumbers()` before defining it. This way, we can call the function from the `main()` function even though it hasn't been defined yet in the code.
## Functions with trailing return types
In this type of declaration is done in following ways:
```cpp
auto function_name(parameters) -> return_type {
// function body
}
```

This kind of declaration is used in lambdas and also preferred to be used in templates

Lambdas:

```cpp
auto function_name->return_type = []() { /*function body*/ }
```
Templates:
```cpp
template <typename T, typename U>
auto function_name(T t, U u) -> decltype(t + u) {
return t + u;
}
```

- [introduction to functions in c++](https://www.learncpp.com/cpp-tutorial/introduction-to-functions/)

0 comments on commit 9420a68

Please sign in to comment.