Functions in modern C++ and some related best practices 👌

  Рет қаралды 1,263

Code for yourself

Code for yourself

Күн бұрын

In this video we talk about functions. How they help us split out the functionality of our code and maintain its readability and our sanity. Here we only touch up on the basics of function writing, but we cover most of the best practices related to it. Enjoy!
🎓 Slides: github.com/cpp-for-yourself/s...
Related materials
- Fibonacci numbers: en.wikipedia.org/wiki/Fibonac...
- Video where we talk about warnings: • Common compilation fla...
- Video on control structure: • Control structures in C++
- Video on vectors: • STL sequence container...
- Setting up your terminal and text editor: • Setting up terminal an...
Please support my free work 😉🙏
- Through Patreon: / code_for_yourself
- Through GitHub Sponsors: github.com/sponsors/niosus
Get a FREE Audible trial here
- US: www.audibletrial.com/CodeForY...
- DE: www.amazon.de/hz/audible/mlp?...
Stuff I'm using to prepare this course
- Keyboard: Logitech MX Keys: amzn.to/3SbOhg8
- Mouse: Logitech MX Vertical: amzn.to/3qVYYI4
- Monitor: Dell P2720DC: amzn.to/3Bthv3A
- Camera: Sony a6400: amzn.to/3duP39x
- Microphone: Rode Wireless GO II: amzn.to/3QUH2sc
- SD card: SanDisk Extreme PRO 128GB: amzn.to/3BRCnS5
Contents
00:00 Intro
00:53 Function to organize code
03:00 Some technical details on functions
04:20 How to write good functions
06:53 Use [[nodiscard]] attribute!
08:31 Good function example
10:28 Bad function example
11:53 Declaration and definitions of functions
13:14 Passing big objects to functions
15:36 Problem with passing by reference
16:30 Use const references!
18:21 Never return a local reference!
19:34 Function overloading
21:12 Default function arguments
23:10 Outro

Пікірлер: 12
@CodeForYourself
@CodeForYourself Жыл бұрын
What was the hardest function *you* had to write? Oh, and thanks for watching! 😜
@kompila
@kompila Жыл бұрын
A recursive function to implement the fibonacci sequence. I was totally new to recursion then, felt an enormous joy when I was able to get over it.
@tomasmerva7978
@tomasmerva7978 Жыл бұрын
Thank you so much. I really appreciate those pro tips regarding the speed/optimization and readability. It would be also interesting to show us how to properly measure the time of execution. Looking forward to next videos :)
@CodeForYourself
@CodeForYourself Жыл бұрын
Yep, coming some time soon. We just need libraries for this and we should be good 😉 Probably will record it within the next couple of weeks.
@ChrisOffner
@ChrisOffner Жыл бұрын
Great video, thank you! :) Could you point me to where I can read up on what exactly are the rare cases under which modifying an object passed as a non-const reference could still be faster than returning an object from a function?
@CodeForYourself
@CodeForYourself Жыл бұрын
Hmm, not really. I have observed this behavior a couple of times and had to change the code to accommodate for this after careful benchmarking but I don't know of a good place to read about it from the top of my head. 🤷‍♂️
@piperosgr
@piperosgr Жыл бұрын
Thanks for the video tutorials. They are on point. So you've mentioned using const and reference wherever possible. What's your opinion on using const reference on the return of a function e.g. in your overloading example: const string& name = getNames("hello")` cout
@CodeForYourself
@CodeForYourself Жыл бұрын
Great question! You can totally do that. The reason is that the way compilers treat constant references is that they extend the lifetime of temporaries that we take a const reference of. I probably should mention it at some point but I probably will record a separate video about this and other fun things that happen especially with returning from functions.
@piperosgr
@piperosgr Жыл бұрын
@@CodeForYourself Thanks for answering. What about any performance/efficiency benefits e.g. do you avoid copying etc.?
@CodeForYourself
@CodeForYourself Жыл бұрын
@@piperosgr sorry, somehow missed your answer. There are no performance downsides to this. In some cases the compiler will even avoid the function call and construct the object in-place. Anyway, taking a reference will not cause any performance impact at all.
@kompila
@kompila Жыл бұрын
4:49 CamelCase or camelCase ?
@CodeForYourself
@CodeForYourself Жыл бұрын
I usually go for *CamelCase* here. See the Google style article: google.github.io/styleguide/cppguide.html#Function_Names
Enumerating options in C++ using enums
5:15
Code for yourself
Рет қаралды 739
How to use FUNCTION TEMPLATES - a comprehensive guide for modern C++
23:33
孩子多的烦恼?#火影忍者 #家庭 #佐助
00:31
火影忍者一家
Рет қаралды 46 МЛН
БОЛЬШОЙ ПЕТУШОК #shorts
00:21
Паша Осадчий
Рет қаралды 8 МЛН
🌊Насколько Глубокий Океан ? #shorts
00:42
Re-inventing move semantics in modern C++ in 13 minutes
13:20
Code for yourself
Рет қаралды 6 М.
1000 subscribers AMA answers
20:37
Code for yourself
Рет қаралды 348
061224 COSC 1336 Python void and value return functions
13:48
Coding Affliction
Рет қаралды 11
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 286 М.
Generics: The most intimidating TypeScript feature
18:19
Matt Pocock
Рет қаралды 166 М.
Const correctness in C++
10:02
Code for yourself
Рет қаралды 2,7 М.
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 389 М.
孩子多的烦恼?#火影忍者 #家庭 #佐助
00:31
火影忍者一家
Рет қаралды 46 МЛН