Skip to content

Instantly share code, notes, and snippets.

@WingTillDie
Last active July 27, 2023 15:33
Show Gist options
  • Save WingTillDie/561c3d41ce78348c31688b58a24e7219 to your computer and use it in GitHub Desktop.
Save WingTillDie/561c3d41ce78348c31688b58a24e7219 to your computer and use it in GitHub Desktop.
Return multiple levels up in the call stack
#if false
g++ $0 && ./a.out
exit
#endif
// Modified code that demonstrates a code with multi-level return
#include <stdio.h>
void print_enter(const char* function_name) {
printf("Entering %s\n", function_name);
}
void print_exit(const char* function_name) {
printf("Exiting %s\n", function_name);
}
int level_10() {
print_enter("level_10");
print_exit("level_10");
return 0;
}
int level_9() {
print_enter("level_9");
// return 3 levels up in the call stack
return 2;
int l = level_10();
if (l) return l-1;
print_exit("level_9");
return 0;
}
int level_8() {
print_enter("level_8");
int l = level_9();
if (l) return l-1;
print_exit("level_8");
return 0;
}
int level_7() {
print_enter("level_7");
int l = level_8();
if (l) return l-1;
print_exit("level_7");
return 0;
}
int level_6() {
print_enter("level_6");
int l = level_7();
if (l) return l-1;
print_exit("level_6");
return 0;
}
int level_5() {
print_enter("level_5");
int l = level_6();
if (l) return l-1;
print_exit("level_5");
return 0;
}
int level_4() {
print_enter("level_4");
int l = level_5();
if (l) return l-1;
print_exit("level_4");
return 0;
}
int level_3() {
print_enter("level_3");
int l = level_4();
if (l) return l-1;
print_exit("level_3");
return 0;
}
int level_2() {
print_enter("level_2");
int l = level_3();
if (l) return l-1;
print_exit("level_2");
return 0;
}
int level_1() {
print_enter("level_1");
int l = level_2();
if (l) return l-1;
print_exit("level_1");
return 0;
}
int main() {
print_enter("main");
level_1();
print_exit("main");
return 0;
}
#if false
g++ $0 && ./a.out
exit
#endif
// Modified code with transformation to the code with multi-level return capability
#include <stdio.h>
void print_enter(const char* function_name) {
printf("Entering %s\n", function_name);
}
void print_exit(const char* function_name) {
printf("Exiting %s\n", function_name);
}
int level_10() {
print_enter("level_10");
print_exit("level_10");
return 0;
}
int level_9() {
print_enter("level_9");
int l = level_10();
if (l) return l-1;
print_exit("level_9");
return 0;
}
int level_8() {
print_enter("level_8");
int l = level_9();
if (l) return l-1;
print_exit("level_8");
return 0;
}
int level_7() {
print_enter("level_7");
int l = level_8();
if (l) return l-1;
print_exit("level_7");
return 0;
}
int level_6() {
print_enter("level_6");
int l = level_7();
if (l) return l-1;
print_exit("level_6");
return 0;
}
int level_5() {
print_enter("level_5");
int l = level_6();
if (l) return l-1;
print_exit("level_5");
return 0;
}
int level_4() {
print_enter("level_4");
int l = level_5();
if (l) return l-1;
print_exit("level_4");
return 0;
}
int level_3() {
print_enter("level_3");
int l = level_4();
if (l) return l-1;
print_exit("level_3");
return 0;
}
int level_2() {
print_enter("level_2");
int l = level_3();
if (l) return l-1;
print_exit("level_2");
return 0;
}
int level_1() {
print_enter("level_1");
int l = level_2();
if (l) return l-1;
print_exit("level_1");
return 0;
}
int main() {
print_enter("main");
level_1();
print_exit("main");
return 0;
}
#if false
g++ $0 && ./a.out
exit
#endif
// Original code without transformation to the code with multi-level return capability
#include <stdio.h>
void print_enter(const char* function_name) {
printf("Entering %s\n", function_name);
}
void print_exit(const char* function_name) {
printf("Exiting %s\n", function_name);
}
void level_10() {
print_enter("level_10");
print_exit("level_10");
}
void level_9() {
print_enter("level_9");
level_10();
print_exit("level_9");
}
void level_8() {
print_enter("level_8");
level_9();
print_exit("level_8");
}
void level_7() {
print_enter("level_7");
level_8();
print_exit("level_7");
}
void level_6() {
print_enter("level_6");
level_7();
print_exit("level_6");
}
void level_5() {
print_enter("level_5");
level_6();
print_exit("level_5");
}
void level_4() {
print_enter("level_4");
level_5();
print_exit("level_4");
}
void level_3() {
print_enter("level_3");
level_4();
print_exit("level_3");
}
void level_2() {
print_enter("level_2");
level_3();
print_exit("level_2");
}
void level_1() {
print_enter("level_1");
level_2();
print_exit("level_1");
}
int main() {
print_enter("main");
level_1();
print_exit("main");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment