Alright, I am back and I have a new program that I have been working on all day. It's funny because I'm sure most of you guys could do this in 1 minute. But as I said before I am 49 years old and do not use computers, just trying to help my son while he is hospitalized.

I need to know once again, is this a correct use of recursion?? Or, is this even recursion at all?? I sure hope that it is, because I would like to go to sleep. Please let me know, and thanks in advance!

#include <iostream>
using namespace std;

void print(const char *s, int val) 
{
   while (--val >= 0)
   printf("%s",s);
}

void printSpace(const char *t, int space)
{       
   while (--space >= 0)
   printf(" %",t);
}

void recursiveFunction(int val, int space)
{

   if (val<=3){

      printSpace(" ",--space);
      print("* ",val);
      cout <<endl;

      recursiveFunction(val + 1,space);

      printSpace(" ",space);
      print("* ",val);
      cout <<endl;

   }

   else{ 
      print("* ",val);
      cout <<endl;
   }
}

int main()
{
recursiveFunction(1,4); 
return 0;
}

Indeed it is.

Consider the following lecture a bonus :icon_cheesygrin:

If you plan to use printf you should #include <cstdio> at the top of your program.

Recursive functions are best avoided in languages like C++ because they don't have any form of tail recursion. Basically, every time you call the function (again) it uses more stack space. If you recurse too much you can crash your program. (The few cycles in your example here are fine though.)

Almost everything done recursively can be done just as easily imperatively (using loops), the bonus being that you don't use up more space at each cycle.

Keep in mind, recursion isn't evil, and is sometimes necessary. If you are just playing around with it, you might want to try something using mutual recursion next.

Nice job!

Thank you very much. You made my day I will tell you that much. However, just as fast as my spirits were raised, my joy has once again been deflated. I just read at the bottom of the directions in my son's textbook that I have to make it so that a user can input an amount of lines to print. : ( Looks like I will be up longer while I try and figure out how to do that.

It shouldn't that much of a challenge when you're allready able to write recursion :)

- ask a user for input before you call your function
- add a 3th var to the function which is the number of times the user just entered
- in the function count down from the 3th var to zero and then quit

Use

int main(int argc,char *argv[])
let user type ./a.out <arg>

Thanks for the advice niek, I got it figured out rather quickly thanks to you. It is much much too late for me to be awake at my age, so I am going to get some sleep! Thanks to all who helped me on this.

It shouldn't that much of a challenge when you're allready able to write recursion :)

- ask a user for input before you call your function
- add a 3th var to the function which is the number of times the user just entered
- in the function count down from the 3th var to zero and then quit

regards Niek

You don't need a third variable. The two you have are enough. Just think about what those two variables are for, and how they relate to the input value...

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.