Recursion: Everything you need to know about it

  Рет қаралды 590

42 Berlin

42 Berlin

Күн бұрын

Пікірлер: 5
@nasimnajand9697
@nasimnajand9697 2 ай бұрын
I hope you explain the tower of Hanoi problem in further videos. Thanks in advance❤😊
@thob
@thob 2 ай бұрын
The other comment might be under review, so I'll post this in a new one. I wrote the iterative factorial function counting backwards because I missed that she said "the loop in _this case_ counts upwards", but the distinction in the execution might make new programmers think this is an inherent property of the control flow, which is why I wanted to exemplify the same behavior can occur in both recursive and iterative processes.
@zandrah496
@zandrah496 2 ай бұрын
You are correct, loops can of course go both ways. I chose to do the iterative solution by incrementing the counting, since many new coders (me included) find that easier.
@samuelvishesh
@samuelvishesh Ай бұрын
How to enforce tail recursion optimisation?
@42berlin
@42berlin Ай бұрын
Here is a quick summary for you ✌️ In C, tail recursion optimization (TRO) is not something you enforce directly in the language itself, but rather something that is handled by the compiler if possible. Tail recursion occurs when the recursive call is the last operation in the function before it returns. In this case, the current function's stack frame is no longer needed, and the compiler can optimize by reusing the stack frame instead of creating a new one. Modern C compilers (e.g., GCC, Clang) can optimize tail-recursive functions to eliminate stack frame buildup. However, this optimization is not guaranteed by the C standard and depends on the compiler and optimization settings. How to Write Tail-Recursive Functions - Ensure that the recursive call is the last action in the function. - Avoid computations or operations after the recursive call. - Pass along any intermediate results via function arguments. Example of tail-recursive function: ```c int factorial_tail(int n, int acc) { if (n == 0) return acc; return factorial_tail(n - 1, acc * n); // Tail call } ``` Example of non-tail-recursive function: ```c int factorial_non_tail(int n) { if (n == 0) return 1; return n * factorial_non_tail(n - 1); // Not a tail call because of multiplication }
Mastering Memory in C: How to use malloc like a pro
3:56
42 Berlin
Рет қаралды 895
The 42 Piscine - Everything You Need to Know
7:51
42 Berlin
Рет қаралды 18 М.
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН
Ozoda - Alamlar (Official Video 2023)
6:22
Ozoda Official
Рет қаралды 10 МЛН
What Is Recursion - In Depth
13:25
Web Dev Simplified
Рет қаралды 159 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 844 М.
Making a New Compiler
15:36
Modern Retro Dev
Рет қаралды 9 М.
Intro to Data Oriented Design for Games
52:35
Nic Barker
Рет қаралды 53 М.
Introduction to Pointers in C
8:38
42 Berlin
Рет қаралды 2,6 М.
Python laid waste to my C++!
17:18
Sheafification of G
Рет қаралды 174 М.
What P vs NP is actually about
17:58
Polylog
Рет қаралды 143 М.
Recursion in Programming - Full Course
1:51:36
freeCodeCamp.org
Рет қаралды 975 М.
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН