Is this an example of a recursive function?? Somebody on another forum helped me put this together, as I am trying to make a diamond shape out of asterisks. But he did not know if this was a correct way of using recursion. Can somebody just tell me yes or no, is this a correct example of a recursive function?? Thanks

#include <iostream>
#include <iomanip>
using namespace std;


int spaces = 5;

int main()
{

for (int i = 1; i < 5; i = i + 1)
 {
   cout << setw(spaces);
    for (int j = 0 ; j < i; j++)
     cout << "* ";
       cout << endl;
        --spaces;
        }
          for (int i = 1; i < 4; i = i + 1)
        {
                cout << setw(spaces + 2);
                 for (int j = 4 ; j > i; j--)
                  cout << "* ";
                   cout << endl;
                    ++spaces;
        }
}

I though recursion function wise is something like


void boo()
{
// blah blah blah
boo();
}

I may be wrong though.

Well, no. It's not.
Do you understand recursion? It's essentially another method of looping but rather than using while() or for(), for example, a function recursively calls itself:

void count_down( int num ) {
  if ( num>=0 ) {
    std::cout<< num;
    cout_down( num-1 ); 
  // Calls the function again but this time the 
  // parameter is one less than before. By this 
  // you can iterate through all the values you
  // need to. Use control statements to determine
  // how to proceed. 
   }
}

That's not the most imaginative example of recursion but it should give you an idea.
Also. Using recursion on main() is normally considered not good.

Ok, I understand how recursion works and everything. And some kind guy gave me the following example to show me how a recursive function can decrement through numbers until it hits zero and then go back up and print those numbers. However, my problem is that I cannot figure out how to make it print the asterisks (*). For instance, I am trying to print a diamond shape, so I need line 1 to print 1 *, line 2 to print 2 *'s, line 3 to print 3 *'s...etc until it gets to 4 *'s and then go back down again to make a diamond shape. When I try to apply asterisks to this program, all I get of course is a line of asterisks like so:
*
*
*
*
* etc.....

Below is the code the guy gave me as an example. If you would please, look at that code and look at my code for printing a diamond, and tell me how I might go about putting the two together to print the asterisks. Thanks a lot to whoever saves the day for me.

#include <stdio.h>
void recursiveFunction(int val)
{
// The working area of the function
printf("called value is %d \n", val);
val--; // decrement val
// The test of the end-value
// In this simple example it's simply waiting until val becomes 0
if (val > 0)
{
recursiveFunction(val); // >0 call again
}
else
{
return; // <= 0, done
}
// Returns will come here and val will be restored to
// it's previous value
// Do the next 'work'
printf("return value is %d \n", val);
return;
}
int main()
{
recursiveFunction(5); // start the function off
return 0;
}

Is this an example of a recursive function?? Somebody on another forum helped me put this together, as I am trying to make a diamond shape out of asterisks. But he did not know if this was a correct way of using recursion. Can somebody just tell me yes or no, is this a correct example of a recursive function?? Thanks

No.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.