问题
This is a sample program for my problem, I am using VisualStudio 2008
void abc()
{
static int i = 0;
if (i==0)
{
xyz();
i++;
}
abc();
}
The static variable retain the value one in next debug session also, thus not calling xyz(), how can I call a function just once without using static variable??
回答1:
How about this:
void abc(int init)
{
if(init == 1) xyz();
abc(0);
}
int main(void) {
abc(1);
}
It has the advantage of showing clearly what is going on. You could even declare an enum:
enum INIT {FIRST_TIME, RECURSING};
and do
void abc(enum INIT init) {
if(init == FIRST_TIME) xyz();
abc(RECURSING):
}
You can see a complete example at work at http://codepad.org/7euiC5LQ
#include <stdio.h>
enum INIT {FIRST, RECURSING};
void abc(enum INIT init) {
if(init == FIRST) {
printf("first time\n");
abc(RECURSING);
}
else {
printf("last time\n");
}
}
int main(void) {
abc(FIRST);
}
In this example, the second time is the last time. Obviously you can embellish from there; usually you will want to pass a parameter to your abc function that might decrease with each call until you reach some point that says "this is the end of the recursion" (think factorials, Fibonacci, etc). In that case, passing an "invalid" parameter (e.g. -1) for the initial call would be a good solution. You still have only one parameter.
Finally - when you are using C++, you could consider overloading your function. Call it with a parameter, and you include xyz; call it without, and you don't. A bit like the abcStart of one of the other answers. But since you tagged your question both C and C++, and there was no evidence in your code that you really intended C++, I am not even going there...
回答2:
You can pass a callflag to abc() function as an indication that whether to call xyz() function or not.
void abc(int callflag){
// do somwork
if(callflag)
xyz(); // xyz() willbe called when callflag = !0
// do other stuff
abc(0)
}
void abcStart(){
abc(1);
//abc(0); If you don't want to call xzy even for first time.
}
I think this is flexible call xyz() within abc() whenever you wants.
回答3:
Not sure this is what you're looking for but it works
void abc(){
abc();
}
void abcStart(){
xyz();
abc();
}
int main(){
abcStart();
}
Doing this you don't need to specify any flag or use any if. You just call the "start" function of your recursion
回答4:
Use a Boolean variable in the caller and pass it to the called function.
#include <stdbool.h> // C99 and latter supports.
void abc(bool flag)
{
if (flag)
{
xyz();
flag = false;
}
abc(flag);
}
int main(void) // T is return type
{
...
bool flag = true;
abc(flag);
...
}
回答5:
Here's a solution which doesn't hard code it to running xyz a single time i.e. you could later trivially change it to run it an arbitrary number of times (credit to Floris whose answer I adapted):
void abc(int i)
{
if(i > 0)
{
xyz();
i--;
}
abc(i);
}
int main()
{
abc(1);
}
来源:https://stackoverflow.com/questions/20904264/how-to-call-another-function-just-once-from-recursive-function-without-using-the