Difficulty in Syntax

Asked

Viewed 269 times

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);
}
  • Is there something specific you don’t understand? It’s the ternary operator ? e :, is the call of the function itself?

  • 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 :/

  • Recursion in one line. The guy who did this is pretty hardcore :D

  • could explain what recursion is?

  • 1

    @Brunorodrigues recursiveness is basically a function that calls itself as return.

  • Actually it’s someone who spent some time in the trenches with functional languages. Bruno, see the article on Recursiveness on Wikipedia.

  • so as I understood it is something more ancestral to the for or if cycle for example :D

Show 2 more comments

1 answer

14


Ternary operator

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):

  • The first part (before interrogation) is the condition, is what will determine true or false (1 is true and 0 is false, equal to if, equal any condition), in his example n == 0.
  • Then he has (after the question and before the two points) the expression that will be considered if the condition is true, in his example 0.
  • And the last part (after the two points) is the expression that will be considered if the condition is false, in your example 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.

Recursiveness

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:

  • The factorial is 4 is:
  • 4 times the factor of 3. It didn’t help much, now I need to know which is the factor of 3:
  • 3 X 2!. So now I calculate the factorial of 2:
  • 2.1!. And by convention 1! is 1, so I no longer need to continue with recursion and the result is:
  • 4.3.2.1 = 24.

To learn more see in that reply and in the Wikipedia.

Your example

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

You are not signed in. Login or sign up in order to post.