Question

I have a question about function, with Ada.

I look a code about rational number, and I don't understand how the program is running.

FOR EXAMPLE:

This is rational.ads

function "+"(R1,R2: Rationnel) return Rationnel;
function "-"(R1,R2: Rationnel) return Rationnel;
function "-"(R : in Rationnel) return Rationnel;
function "*"(R1,R2: Rationnel) return Rationnel;
function "/"(R1,R2: Rationnel) return Rationnel;

This is rational.adb

function "+" (R1,R2: Rationnel) return Rationnel is
    N,D : Integer;
begin
    N:= R1.num *R2.den + R2.num*R1.den;
    D:= R1.den*R2.Den;
    return Simplification ((N,D));
end "+";

function "-" (R1,R2: Rationnel) return Rationnel is
    N : Integer; 
    D : Positive;
begin
    N:= R1.num *R2.den - R2.num*R1.den;
    D:= R1.den*R2.Den;
    return Simplification ((N,D));
end "-";

The realization is not a problem for me. I don't understand the following line

R1,R2,R3 : rationnel;
R1 := R2 + R3;

How is it possible to use the function "+", with this order of arguments?

[A+B : Argument1 = A, Argument2 = B]

And not for example : R1 := +(R2,R3), as we usually do with function.

I hope I was clear enough and that my English was not too unbearable to read ;-)

Was it helpful?

Solution

What you're dealing with is the difference between functional notation and infix notation.
Given:

Function "+"(Left, Right: Rational) return Rational;

we know that the above can be called like this:

some_value:= "+"( Left => R1, Right => R2 );

this is actually the same as:

-- In infix-notation R1 is on the Left, and R2 is on the Right.
some_value:= R1 + R2;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top