10
I would like you to explain this function, I do not understand this syntax:
double sum_arithmetic(double x0, double r, int n)
{
return n == 0 ? 0 : x0 + sum_arithmetic(x0 + r, r, n-1);
}
10
I would like you to explain this function, I do not understand this syntax:
double sum_arithmetic(double x0, double r, int n)
{
return n == 0 ? 0 : x0 + sum_arithmetic(x0 + r, r, n-1);
}
14
The ternary operator resembles a if
but it is an expression and not a statement. So you can use anywhere that accepts an expression, as long as your results are also expressions. I actually prefer to call it probation, since ternary is circumstantial and doesn’t define what he does.
It is composed of three parts (for this is erroneously called ternary operator):
if
, equal any condition), in his example n == 0
.x0 + sum_arithmetic(x0 + r, r, n-1)
.Always only one of them will be considered.
Then you will read it as follows:
If n
is equal to zero, consider as zero result, otherwise make the following calculation x0 + sum_arithmetic(x0 + r, r, n-1)
and consider the outcome of it.
Your code could be written with if
in this way:
if (n == 0) {
return 0;
} else {
return x0 + sum_arithmetic(x0 + r, r, n-1);
}
The correct name should be conditional operator, but as it is the only ternary operator known in languages mainstream, ended up taking that name.
Some additional information in that reply and that one and in the Wikipedia.
It is a concept where a function calls itself and in general has a condition where it is no longer necessary to call it (this is not mandatory but if it does not have an output form, the calls will be infinite and you end up with an error of stack overflow).
There’s a joke that helps you understand a little:
To understand recursion, you need to understand what recursion is. Got it?
Now Google for the word recursion
. You’ll find another joke.
A classic example of recursion is the factorial calculation:
To learn more see in that reply and in the Wikipedia.
While the third parameter n
is not zero he will calculate the sum of x0
with the result of the function itself sum_arithmetic()
passing as parameters the first parameter x0
sum of the second parameter r
, then it will pass itself r
and finally passes to the last parameter o n
received minus one. As with each call n
will always be reduced to one, an hour will reach zero and then it will simply return zero according to the ternary operator, and will no longer call the function.
If you have a very, very large number of recursive function calls sum_arithimetic()
it is possible that you have a stack overflow to fill all memory reserved for the stack of its application.
Thank you, all explained!!! D
+1 for the answer and +1 for the reference to google recursion. hahahahaha
Browser other questions tagged c operators syntax recursion
You are not signed in. Login or sign up in order to post.
Is there something specific you don’t understand? It’s the ternary operator
? e :
, is the call of the function itself?– Maniero
this function does the same as this (n/2) * ((2 * x0) + ((n - 1) * (r)), but I don’t understand how it can calculate this formula. And I don’t understand the ternary operator either :/
– Bruno Rodrigues
Recursion in one line. The guy who did this is pretty hardcore :D
– gmsantos
could explain what recursion is?
– Bruno Rodrigues
@Brunorodrigues recursiveness is basically a function that calls itself as return.
– gmsantos
Actually it’s someone who spent some time in the trenches with functional languages. Bruno, see the article on Recursiveness on Wikipedia.
– Anthony Accioly
so as I understood it is something more ancestral to the for or if cycle for example :D
– Bruno Rodrigues