Can the values of normal variable created inside a function be stored throughout the code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

20th Jan 2020, 6:15 AM
Y AD Ù
Y AD Ù - avatar
11 Answers
+ 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
20th Jan 2020, 8:45 PM
Mahmud Nabil
Mahmud Nabil - avatar
+ 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; }
20th Jan 2020, 9:25 AM
HonFu
HonFu - avatar
+ 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.
20th Jan 2020, 9:03 AM
HonFu
HonFu - avatar
+ 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; }
20th Jan 2020, 6:47 AM
Ipang
+ 1
So Ipang will mult have its initialized value or its new value?
20th Jan 2020, 7:04 AM
Y AD Ù
Y AD Ù - avatar
+ 1
Y AD Ù Wait, I was just reading your reply What number do I test the code with? 371 and 3?
20th Jan 2020, 7:05 AM
Ipang
+ 1
Ipang by the new value i mean the value that mult gets after it has gone through the iterations inside the for loop
20th Jan 2020, 8:41 AM
Y AD Ù
Y AD Ù - avatar
0
Ipang Enter the values as said in code..wen u see the output u'll get my question
20th Jan 2020, 7:02 AM
Y AD Ù
Y AD Ù - avatar
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.
20th Jan 2020, 8:05 AM
Ipang
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
20th Jan 2020, 8:43 AM
Y AD Ù
Y AD Ù - avatar
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..
20th Jan 2020, 9:12 AM
Y AD Ù
Y AD Ù - avatar