+ 2
Can the values of normal variable created inside a function be stored throughout the code?
For eg we created a fucntn with 'int' as the return type and then created a variable say 'x' in int and we assigns a value say 7 to x and thn returns it...when we call the functn for the SECOND TIME why do we still get the value 7 shouldn't the value 7 be destroyed after the first call since 'x' isnt a STATIC VARIABLE..? here in my code the value of 'mult' still persist even in the second call https://code.sololearn.com/ct37qkSG54Ln/?ref=app
11 odpowiedzi
+ 3
In the last call of the function in the loop - value of digit and x was 3(for example). Since the values of digit and x is not changed after that, you will get the same value 27 each time you run power(digit, x). So this doesn't prove that the value is not destroyed.
However the value is not destroyed indeed. Run the following code to see-
https://code.sololearn.com/c5bJ9LsG2vc6/?ref=app
+ 4
One of the reasons why we modularize (break up the code in smaller pieces) is that we can test, if the parts - isolatedly - do what they're supposed to.
Please run this little code featuring your function, and you'll see it works as it's supposed to.
Which means the problem lies somewhere else.
#include <stdio.h>
int power (int a,int b)
{
int i=0;
int mult=1;
for(i=0;i<b;i++){
mult=mult*a;
}
return(mult);
}
int main() {
for(int i=2; i<10; ++i)
for(int j=2; j<6; ++j)
printf("%d^%d = %d\n", i, j, power(i, j));
return 0;
}
+ 2
After a function call ended, everything what's in that function call, will be gone.
If you call the function again, mult will be reinitialized just like the first time you called it.
+ 1
But you define the variable <mult> and initialize it with a value. This is how <mult> gets a value again in successive calls. Or you meant something else?
int mult = 1;
int power (int a,int b)
{
int i = 0;
int mult = 1; // <mult> initialized
for(i = 0; i < b; i++){
mult = mult * a;
}
return mult;
}
+ 1
So Ipang will mult have its initialized value or its new value?
+ 1
Y AD Ù
Wait, I was just reading your reply
What number do I test the code with? 371 and 3?
+ 1
Ipang by the new value i mean the value that mult gets after it has gone through the iterations inside the for loop
0
Ipang Enter the values as said in code..wen u see the output u'll get my question
0
Y AD Ù
What do you mean "its initialized value or new value"? When you do this
int mult = 1;
<mult> is given a value (initialized) by 1. What new value did you mean?
And I don't think the code at line 32 is necessary, you can remove it.
0
Ipang yea bro i knw...i gave that statement at 32 to show that the functn returns that same value..it has returned at the last wen it was called(line21 and 22)which shows that it remembers it
0
HonFu lets fix a number say 371
so input ll be 371 and 3
now wen the last call of fuctn occurs in the loop the value returned by the functn is 27...and in line 32 wen i again call the functn...its value 27 itself is being returned..