I have to print One time in a C function that use recursion.
The issue is to print End1 at the end of func1 (1 time and not more than 1 time):
to do this i created fend1() that I equipped with a global variable
The following code is my actual solution but I wish to get the same goal,
without using any global variable.
#include<stdio.h>
int cntg1 = 1;
int fend1(){
if(cntg1 < 2){
printf("nEnd1");
cntg1++;
}
return 0;
}
void func1(int i){
if(i == 6) return; // Base Case
printf("%d ", i);
func1(i + 1);
fend1(); // <<< this is factory new
}
int main()
{
func1(1); // *
return 0;
}
The following link open a new website page click here↗???? to view the code run.
As I anticipate, I wish to get the same goal (print 1 time after recursion),
without using any global variable.
I asked before about a similar problem (↗????here can be found the other question) and according to the Problem this above receive the higest score, but I have to find a better solution.
pvt-Tron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1