Indolent
You know if that's the case then the output should be like 3 3 2 2 1 1 right?
Indolent
Like every time n should decrease by one and then cout it out
Roxifλsz 🇱🇹
You know if that's the case then the output should be like 3 3 2 2 1 1 right?
Nope, because you first call printFun and only then you cout your variable for the second time
Indolent
So the n keeps getting substracted till it becomes 1 and then the couting happens?
Roxifλsz 🇱🇹
Maybe try watching some vid on YouTube about recursion, I think an interactive animated explanation might be easier to understand than my ramblings in text
Indolent
test = 3 cout << 3 printFun(2) // something happens cout << should be 3 right? cuz we're still in the middle of "test = 3" right?
Indolent
Glad you saw that 👍👍👍
☬ੴ Bassi
void printFun(int test) { if(test < 1) return; // to exit it must less //then 1 else { cout << test << " "; printFun(test - 1); // here is the action // of passing the value back in cout << test << " "; return; } } int main() { printFun(3); return 0; }
☬ੴ Bassi
you put in 3 so it goes straight to the else the statement and reurns -1 on 3 = 2 then it does it again = 1 the function is calling itsself
Indolent
test = 3 cout << 3 printFun(2) // something happens cout << ?? test = 2 cout << 2 printFun(1) // something happens cout << ?? test = 1 cout << 1 printFun(0) // something happens cout << ?? // And now what? The code should just stop here right? Because if(test < 1) return; Then how in output it prints 3 2 1 1 2 3 and what should be in place of "??" above?
☬ੴ Bassi
that code will not execute till the condition is true which is the number must be 0
☬ੴ Bassi
so only when test = 0 it will exit
Indolent
but after test = 1 it becomes 0 right?
☬ੴ Bassi
correct
Indolent
n-1 1-1 0
Indolent
Then where does the 1 2 3 comes from?
☬ੴ Bassi
to better understand change the (..< 1) to ( < 3)
☬ੴ Bassi
so when you put 3 in it just goes to the output you are normally seeing
☬ੴ Bassi
so when this runas printFun(test - 1); now what will happen is test = 2 so the code will return
☬ੴ Bassi
void printFun(int test) { if(test < 1) { cout << "test is lower then 1"; return; } else { cout << test << " "; printFun(test - 1); cout << test << " "; return; } } int main() { printFun(3); return 0; }
☬ੴ Bassi
try this instead
Indolent
Alright
☬ੴ Bassi
all i really did was just simply add a additional cout on the if statement so you can see the code is hit.
Indolent
3 2 1 test is lower then 11 2 3 Well this is the output, crazy
☬ੴ Bassi
because the last cout does not get called to the end
☬ੴ Bassi
void printFun(int test) { if(test < 1) { cout << "test is lower then 1\n"; return; } else { cout << test << " " << "before\n"; printFun(test - 1); cout << test << " " << "after\n"; return; } } int main() { printFun(3); return 0; }
☬ੴ Bassi
i just tried this to explain better
Madhav
Hi
Anonymous
🙄
Anonymous
3 2 1 test is lower then 11 2 3 Well this is the output, crazy
read about scopes, linkage, and storage duration int i = 3; printf("%d\n", i); { int i = 40; printf("%d\n", i); } printf("%d\n", i);
Indolent
That's C btw
Anonymous
That's C btw
C++ is crazier with user-defined namespaces, modules and other stuff
Indolent
Did you mean easier
Indolent
Look I just want to know what happens when I call printFun() the second time
Indolent
Like EXACTLY
Anonymous
function declarations and function bodies have their own scopes
Anonymous
which means their own local variables
Indolent
Not in that depth just the simple part
Anonymous
which expire after their scopes end
Indolent
I know in the end it would something really really simple and all that that I'll Google and read will be of simply no use tbh
Anonymous
you probably changed the example before posting
Anonymous
#include <iostream> #include <cstdio> using namespace std; int main() { int a; long b; char c; float d; double e; scanf(" %d %ld %c %f %lf", &a,&b,&c,&d,&e); printf("%d/n",a); printf("%ld/n",b); printf("%c/n",c); printf("%f/n",d); printf("%lf/n",e); return 0; }
Anonymous
is there an error;??
Anonymous
yes so i used f and lf here??
Anonymous
%f and %lf
Anonymous
scanf has to keep them different because the pointer is used to access precise number of bytes. for printf float and double are same because float arguments are promoted to double.
Anonymous
but this code is not printing the taken inputs on different lines
Anonymous
??
Anonymous
wrong /
Indolent
3, 2, 1
EXACTLY BUT THEN WHY IN THE OUTPUT IT PRINTS 3 2 1 1 2 3 WHY THAT 1 2 3 part??
Indolent
Where does it comes from?
☬ੴ Bassi
You have two cout
Anonymous
🙏🙏🙏🙏🙏thanks @chandradeepdey
Indolent
Just tell me what's happening in a simple line
Indolent
Like in the test = 1 the code should just stop because if(n < 1) return;
Anonymous
use gdb, look at the call stack
Anonymous
go line by line on your code
Anonymous
use gdb, look at the call stack
i'm a CLion shill, so use CLion. but anything is fine
Indolent
I will do something. *sighs*
Roxifλsz 🇱🇹
EXACTLY BUT THEN WHY IN THE OUTPUT IT PRINTS 3 2 1 1 2 3 WHY THAT 1 2 3 part??
You say you want a simple explanation, well apparently that's not possible, please go learn more about how regular (simple) functions work, then try again learning recursion
Indolent
Yeah I'll just do that. Thanks for everything y'all.
Anonymous
Hiiee
Anonymous
I am new in this group...
Anonymous
So i dont know any rules and regulations
Anonymous
☬ੴ Bassi
because the rest of the code is not printed til the end of the function