Very confusing example in Function Calls (LearnAda)

In the example, the purpose is to calculate the quadruple given a function Double. In my program (I I have compiled and executed), Double is called only twice, not third time with an extra unusuful evaluation. But perhaps the example was not well chosen and the question was simply how to call a function and ignore the result.

Note that in Ada Quality and Style Guide, we should use function “when the subprogram’s primary purpose is to provide a single value”. If the guidance is strictly applied, calling the function and ignoring the value should not be needed.

Yes, it intends to make a point, as is clearly explained in the “Learn Ada” section that you referenced:

" Function calls

An important feature of function calls in Ada is that the return value at a call cannot be ignored; that is, a function call cannot be used as a statement."

The code shows that when you do use a function call as a statement, this is reported by the compiler as an error.

QED

No, the example is exactly that in Ada you cannot ignore a returned value; this is what the text immediately preceding the example (on the OP-linked page says). Yes, the “correct quadrupling” doesn’t even need the call, this is entirely beside the point.

Doubling, quadrupling, and whatever else is entirely immaterial precisely because the example is illustrating that point. This is exactly why I used the declare-block + renames example/answer: to show how to intentionally, explicitly ignore a returned value.

We should have a new version of this example available shortly, that hopefully is less confusing for a new user of Ada. Admittedly it was confusing for a function called “Quadruple” to call “Double” only once.

1 Like

Read my program… it does what a Quadruple function should do without a unneeded function call.

In this example, the obvious fix is to suppress the unneeded function call. In an other example (typically a function with side-effect…. Which is not recommended), surely, calling it and putting the result in an unused variable is the right thing to do.

You are completely missing the point.
It’s not needed vs unneeded calls, the question is prompted from a tutorial on Ada, this particular tutorial is illustrating the language-feature that you cannot simply ignore a returned value in the language like you can in some other languages.

Go back to the original post and take a look at the error-messages:

  1. Error: cannot use function "double" as a statement.
  2. Error: return value of a function call cannot be ignored.

This is even on the page, immediately before the example is the text:

An important feature of function calls in Ada is that the return value at a call cannot be ignored; that is, a function call cannot be used as a statement.

If you want to call a function and do not need its result, you will still need to explicitly store it in a local variable.

(Emphasis added.)

You can also use Pragma Unreferenced, and I think you can use it as an aspect on the object… though you could also use pragma/aspect Warnings(Off).