I have a beginners C question. I want in the code below...
include <stdio.h>
void iprint();
int i=0;
int main()
{
int j;
for (j=0; j<50; j++)
{
iprint(i);
printf("%d\n",i);
}
}
void iprint(i)
{
i +=1;
//printf("%d\n",i);
}
... the function "iprint" to update the value of i each time is it called, e.g. update i so that it can be used in main with the value 1 for iteration 2, and 3 for iteration 2 etc.
I accomplished this by changing the code to this:
include <stdio.h>
int iprint();
int i=0;
int main()
{
int j;
for (j=0; j<50; j++)
{
i= iprint(i);
printf("%d\n",i);
}
}
int iprint(i)
{
i +=1;
//printf("%d\n",i);
return(i);
}
Do i have to return(i) to make that happen? The reason for asking, is that if i have a lot of functions using i, it's a bit annoying having to pass i between them. If you instead, somehow could update i like you update a global variable in matlab, that'd be great. Is it possible?