0

Why 'reminder'(rem) is declared globally here?

#include<iostream> using namespace std; int palindrome(int); int rem=0; int main() { int num,res; cout<<"Enter number:- "; cin>>num; res=palindrome(num); if(num==res) { cout<<"Entered number is palindrome"; } else { cout<<"Enter number is not palindrome"; } return 0; } int palindrome(int num) { int number; if(num!=0) { number=num%10; rem=rem*10+number; palindrome(num/10); } return rem; }

8th Sep 2018, 3:40 AM
Test 123
13 Respostas
+ 2
Test 123 below code without global declaration: #include<iostream> using namespace std; int palindrome(int,int&); int main() { int rem = 0; int num,res; cout<<"Enter number:- "; cin>>num; res=palindrome(num,rem); if(num==res) { cout<<"Entered number is palindrome"; } else { cout<<"Enter number is not palindrome"; } return 0; } int palindrome(int num,int &rem) { int number; if(num!=0) { number=num%10; rem=rem*10+number; palindrome(num/10,rem); } return rem; }
8th Sep 2018, 5:18 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Test 123 whatever I mentioned in my previous comment works as you don't need same rem variable outside..you are just returning value as function return.. also static mentioned ask compiler to retain value of same through out application...so, value is retained and initialisation to zero happens only once for first call
8th Sep 2018, 6:06 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Test 123 yup I missed that recursion call inside function...but it's good that you only answered your question of why it is declared globally...
8th Sep 2018, 5:07 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Oh yeah, i just answered it myself, thanks for ur time
8th Sep 2018, 5:13 AM
Test 123
+ 1
const value you can't change..use static as below : static int rem = 0;
8th Sep 2018, 6:01 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
ok thanks for explanation
8th Sep 2018, 6:08 AM
Test 123
+ 1
check once and you will get error
8th Sep 2018, 6:12 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan Lalcheta thanks once again to clarify my doubt , now rem is read only variable we cannot change it value after intilization...
8th Sep 2018, 6:16 AM
Test 123
0
Test 123 it's not required to have something globally only.. you can pass function argument or return variable or use call by reference to access something within different code blocks.. in your case, without doing any changes, you can declare rem locally inside function...
8th Sep 2018, 4:53 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
if i declare it inside palindrome function it's value is changing after each call, this is changing the output
8th Sep 2018, 5:04 AM
Test 123
0
what if we declare it inside palindrome only and make it constant example const rem=0; is this right?
8th Sep 2018, 5:56 AM
Test 123
0
it can be changed inside it's scope i think but we can't change it using a method so our value is preserved while recursive calling of function . correct me if i'm wrong
8th Sep 2018, 6:03 AM
Test 123
0
const will tell the compiler to initialise rem =0 once .
8th Sep 2018, 6:11 AM
Test 123