Reputation: 23
For example, if I write a ftn called "add"
that consumes a int "a"
and produce a value of a + b
, b
is a value that is produced by the last called.
eg
add(1) ==> 1 (first called b=0,then 1 + 0 = 1)
add(2) ==> 3 (second called b=1, then 2 + 1 = 3)
add(4) ==> 7 (third called b=3, then 3 + 4 =7)
EDIT: even without using "static"
Upvotes: 0
Views: 148
Reputation: 2517
Consider:
#include <stdio.h>
int test1(){
static int localVar=0;
localVar++;
printf("test1 %i\n",localVar);
}
int test2(){
static int localVar=0;
localVar++;
printf("test2 %i\n",localVar);
}
int main(){
test1();
test2();
test1();
}
Running this prints
test1 1
test2 1
test1 2
because static applies to the variable in the scope in which it is defined, in this case, both inside test1 and inside test2, both start with a value of 0 and increment each time the function is called, as you can see, even after the first call to test1, the value inside test2 has not incremented. Calling test1 again shows that the value is remembered. If you're not convinced, adjust the number of times each is called and try it yourself.
Upvotes: 1
Reputation: 64308
How about a static local variable?
int add(int a)
{
static int b = 0;
b += a;
return b;
}
or maybe a lambda:
int main(int,char**)
{
int b = 0;
auto add = [&](int a) { b += a; return b; };
std::cout << add(1) << "\n";
std::cout << add(2) << "\n";
std::cout << add(4) << "\n";
return 0;
}
outputs:
1
3
7
Upvotes: 1
Reputation: 40145
#include <stdio.h>
int add(int value){
FILE *fp;
int pv = 0;
fp = fopen("temp.bin", "r+b");
if(fp==NULL){
fp = fopen("temp.bin", "wb+");
fwrite(&pv, sizeof(int), 1, fp);
rewind(fp);
}
fread(&pv, sizeof(int), 1, fp);
pv += value;
rewind(fp);
fwrite(&pv, sizeof(int), 1, fp);
fclose(fp);
return pv;
}
int main() {
printf("%d\n", add(1));
printf("%d\n", add(2));
printf("%d\n", add(4));
return 0;
}
Upvotes: 1
Reputation: 420
Why without static? It's exactly what you need, isn't it? Otherwise, you can pass a parameter as a pointer and change it anywhere you want.
void f( int * a )
{
*a += 5;
}
and in main, or wherever you call
int a = 0;
f( &a );
f( &a );
a
will be 10 and you can also work with it in function.
Is this helpful?
Upvotes: 4
Reputation: 2534
If you need to maintain state, but you don't want the function to store it in a static
variable, there are limited options.
You could just pass the state in, eg:
/* Be careful with the first call - the caller must initialise
previous_result to 0 */
int add(int a, int *prev_result)
{
int result = a + *prev_result;
*prev_result = result;
return result;
}
Or, you could store it in a file, eg:
/* Incomplete, and completely untested! */
int add(int a)
{
FILE *state_file;
int result
int prev_result;
state_file = fopen("state.txt", "r");
prev_result = get_value_from_file(state_file); /* write this yourself */
fclose(state_file);
result = a + prev_result;
state_file = fopen("state.txt", "w");
write_value_to_file(state_file, result); /* write this yourself */
fclose(state_file);
return result;
}
Upvotes: 1
Reputation: 48290
Will this do what you want?
int add(int a) {
static int b = 0;
return b += a;
}
The static variable b
is local to the add()
function but keeps its value between calls.
Upvotes: 2