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.
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.
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.
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.