1. I could learn a thing or to about Recursion

I've been tasked with making a program that prints the astri "*" character in a certain pattern. It's been a little difficult for me because I don't quite understand Recursion in Cpp.

This is what my function looks like. What I was hoping for was "lines" to eventually reach 0 actiavating my else if statement but my mind set was completely wrong. Next I decided a better idea would be to activate an else if statement when lines = i. The problem is lines always equal i. I'm not asking for the anwser but I will ask anyone to explain my code in great detail so I can try better ideas.

What I'm trying to do is to get my code to print this
*
**
***
****
****
***
**
*
Function
Code:
```void printStars(int lines)
{
int i;

if (lines > 0)
{
printStars(lines - 1);

for (i = 0; i < lines; i++)
cout << "*"<<lines;
cout<< endl<< i;
}
}```

2. One way to implement this is to use two parameters, one for the current number of stars, one for the maximum number of stars. Only the current number of stars would change during the recursive calls.

3. I would also advise that it is a great practice to write a recursive version of your code first and then implement it as a non-recursive version meaning you use a for or while loop instead. I've been burned in the past by code suddenly failing to work for sufficiently deep recursive calls which happens more than I'd have thought.

4. Part 2 of 3 is complete. I don't feel good about this code, but I couldn't spend anymore time on this. Thanks rcgldr I got the idea reading your post.

Code:
```//Chapter 6: Programming Exercise 1
#include<iostream>
usingnamespace std;
void printStars(int lines);
void printStars2(int lines);
int main()
{
int lines;
cout << "Enter the number of lines in the grid: " << flush;
cin >> lines;
cout << endl;
printStars(lines);
printStars2(lines);
cout << endl;
system ("pause");
return 0;
}
void printStars(int lines)
{
int i;
if (lines > 0)
{
printStars(lines - 1);
for (i = 0; i < lines; i++)
cout << "*";
cout<< endl;
}
}
void printStars2(int lines)
{
int i;
if (lines > 0)
{
for (i = 0; i < lines; i++)
cout << "*";
cout<< endl;
printStars2(lines - 1);
}
}```

5. This is what I meant by using two parameters with a single recursive function:

Code:
```#include<iostream>

using namespace std;

void printStars(int stars, int maxstars);

int main()
{
int lines;
cout << "Enter the number of lines in the grid: " << flush;
cin >> lines;
cout << endl;
printStars(1, lines);
cout << endl;
system ("pause");
return 0;
}

void printStars(int stars, int maxstars)
{
if(stars > maxstars)
return;
for(int i = 0; i < stars; i++)
cout << "*";
cout << endl;
printStars(stars+1, maxstars);
for(int i = 0; i < stars; i++)
cout << "*";
cout << endl;
}```

6. Originally Posted by rcgldr
This is what I meant by using two parameters with a single recursive function:

Code:
```#include<iostream>

using namespace std;

void printStars(int stars, int maxstars);

int main()
{
int lines;
cout << "Enter the number of lines in the grid: " << flush;
cin >> lines;
cout << endl;
printStars(1, lines);
cout << endl;
system ("pause");
return 0;
}

void printStars(int stars, int maxstars)
{
if(stars > maxstars)
return;
for(int i = 0; i < stars; i++)
cout << "*";
cout << endl;
printStars(stars+1, maxstars);
for(int i = 0; i < stars; i++)
cout << "*";
cout << endl;
}```
There are a few things I don't get.

*could explain how a void function can have the return statement within it?

*What about the parameter on the function within the main function?

*printStars(stars+1, maxstars); I just don't get this statement.

BTW I love how neat your code looks. I want to follow this style.

7. Originally Posted by LAURENT*
*could explain how a void function can have the return statement within it?
The return statement does not return a value, so that's fine. It becomes just a way to pass control back to the caller.

Originally Posted by LAURENT*
*What about the parameter on the function within the main function?

Originally Posted by LAURENT*
*printStars(stars+1, maxstars); I just don't get this statement.