Telegram Group & Telegram Channel
Embedded Academy
🔺Comparision of C++ and Posix Threads What is the difference between using the C++ std threads and POSIX threads? API: The API for C++ std threads and POSIX threads are different, with different function names and parameters. The C++ std thread library is…
One line down, more efficient: Tail Recursion

📌 B4b4k

Recursive functions are known for programmers, but it uses the call stack and has stack overflow risk. but simple change results in a big difference. this change is called "tail recursive". The tail recursion is that kind of recursion in which the recursive call is made at the end of the function.
Consider this formal recursion:

unsigned int fact(unsigned int n)
{
if (n <= 0)
return 1;
return n * fact(n - 1);
}

Can Change to the Tail-recursion version as follows:
unsigned int factTail(unsigned int n, unsigned int a)
{
if (n == 1)
return a;
return factTail(n - 1, n * a);
}
unsigned int fact(unsigned int n) { return factTail(n, 1); }

Note in this version there is no statement after the recursive call.
While computers execute recursive with the help of stacks By using tail recursive instead of formal or head recursive, compilers (such as GCC) can transform this to loop and eliminates stack overflow risk and decrease space complexity from O(n) to O(1).

#Tips #Algorithms #Cpp
@embedded



tg-me.com/embedded/2098
Create:
Last Update:

One line down, more efficient: Tail Recursion

📌 B4b4k

Recursive functions are known for programmers, but it uses the call stack and has stack overflow risk. but simple change results in a big difference. this change is called "tail recursive". The tail recursion is that kind of recursion in which the recursive call is made at the end of the function.
Consider this formal recursion:

unsigned int fact(unsigned int n)
{
if (n <= 0)
return 1;
return n * fact(n - 1);
}

Can Change to the Tail-recursion version as follows:
unsigned int factTail(unsigned int n, unsigned int a)
{
if (n == 1)
return a;
return factTail(n - 1, n * a);
}
unsigned int fact(unsigned int n) { return factTail(n, 1); }

Note in this version there is no statement after the recursive call.
While computers execute recursive with the help of stacks By using tail recursive instead of formal or head recursive, compilers (such as GCC) can transform this to loop and eliminates stack overflow risk and decrease space complexity from O(n) to O(1).

#Tips #Algorithms #Cpp
@embedded

BY Embedded Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 280

Share with your friend now:
tg-me.com/embedded/2098

View MORE
Open in Telegram


Embedded Academy Telegram | DID YOU KNOW?

Date: |

Telegram announces Search Filters

With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.

Find Channels On Telegram?

Telegram is an aspiring new messaging app that’s taking the world by storm. The app is free, fast, and claims to be one of the safest messengers around. It allows people to connect easily, without any boundaries.You can use channels on Telegram, which are similar to Facebook pages. If you’re wondering how to find channels on Telegram, you’re in the right place. Keep reading and you’ll find out how. Also, you’ll learn more about channels, creating channels yourself, and the difference between private and public Telegram channels.

Embedded Academy from us


Telegram Embedded Academy
FROM USA