Recursive function with no parameters

Mar 26, 2015 at 11:52am
Hello everyone. Is here anyone who can explain to me what a recursive function with no parameters is???
I must write a function that will count the sum of array util the first negative one, and must do this by using recursion. The function gets no parameters.
Thanks in advance.
Last edited on Mar 26, 2015 at 11:57am
Mar 26, 2015 at 11:57am
A recursive function is a function that calls itself.

1
2
3
4
void doSomething()
{
     // Somethings Done
}


Thats a normal function without parameters.

1
2
3
4
5
6
void doSomething()
{
     //Somethings Done
    
    doSomething();
}


Thats a recursive function with no parameters
Mar 26, 2015 at 12:04pm
But how to write the code I asked about above?
Mar 26, 2015 at 12:04pm
Thats something your professor wants you to write, not me. Get something started, and I'll help you out if you get stuck on some problem.
Mar 26, 2015 at 12:06pm
Use global variables:
1
2
3
4
5
6
7
8
int x = 10;

void decrease()
{
  --x;
  if(x > 0)
    decrease();
}
Mar 26, 2015 at 12:08pm
I missunderstood calm down peeps
Last edited on Mar 26, 2015 at 12:23pm
Mar 26, 2015 at 12:14pm
It's actually not my homework dear Tarik Neaj don't worry.
Mar 26, 2015 at 12:14pm
Coder777 just showed how to use a recursive function with global variables. The suffocated still need to understand how it works to be able to solve his homework.
Mar 26, 2015 at 12:16pm
As I understand this function decreases 10 until it's 0 am I right Coder?
Mar 26, 2015 at 12:18pm
exactly Peter87. With one but, not his but her eh:D

Mar 26, 2015 at 12:22pm
const int n = 10;
static int a[n];

void sum ()
{
static int i;
if ( n == 0 )
return a[0];
return sum() + a[i];
}
Is this something close to my needed solution?
Mar 26, 2015 at 1:44pm
As I understand this function decreases 10 until it's 0 am I right Coder?
Yes.

Is this something close to my needed solution?
Well, you need to initialize i and change it somehow (either i++ then the upper limit is n or i-- -> 0)

n is constant und doesn't change.

if sum() return a value you need write the non void return type before the function.

Compile + test it.
Mar 26, 2015 at 4:16pm
Ok thank you really much
Mar 26, 2015 at 4:34pm
The suffocated wrote:
It's actually not my homework

I disagree. The purpose of homework is that you learn by doing. If you have to write code with no other apparent purpose than to learn something, then you IMHO do have homework.


It is a bit suspiciuos that one has to resort to a global variable, even if just as educational experiment. If there were input instead of array, then iterative version would be trivial (and a direct hint about the recursive version):
1
2
3
4
5
int sum = 0;
int value;
while ( in >> value && 0 <= value ) {
  sum += value;
}

Edit: That has just a semantic difference. Both an array and istream in have to be global, although std::cin is one of the somewhat acceptable "global variables". In other words, we sternly warn about use of globals, yet write "Hello world" without hesitation ...

The suffocated wrote:
sum of array util the first negative one

There is thus one required condition for ending the recursion and it is not about indices.
Checking against the size of array is a safety measure, should the array lack that negative value.


The static int i is a nice touch.
Last edited on Mar 26, 2015 at 4:55pm
Mar 26, 2015 at 4:54pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
const int arr[5] = {2,0,9,-3,7};

void rrr ()
{
	static int i=0, sum=0;
	if(!(arr[i]<0) && i<5)
	{
		sum += arr[i++];
		rrr();
	}
	else std::cout << "sum= " << sum << "\n";
}

int main ()
{
	rrr();
	
}
Mar 26, 2015 at 4:57pm
Thank you I think now I got it. It wasn't even a homework I just heard about that problem from a guy and decided to try it. That's the whole case
Mar 26, 2015 at 5:03pm
decided to try

I.e. you assigned homework for yourself.

Now, for getting more benefit from your try, do show us a version that does not print anything, but returns the sum to the caller.

Better yet, make two: one that reads values from array and another, that reads values from std::cin.


@anup30: Not you.
Mar 26, 2015 at 7:12pm
i agree with keskiverto. you learn by coding yourself.

however i am not erasing the solution because the OP clearly didn't know the technique/didn't figure it out in five hours. sometimes we can learn techniques from others code.

i appreciate the OP to solve the task keskiverto given.
Topic archived. No new replies allowed.