Begin

advertisement
CSC 145
Subprograms in Ada
Two types of Subprograms:
1. Function – only gives back one value
2. Procedure – performs a task – may give back more than one value or none at all
Must have a declaration (in the declaration section of the program) & a call for the subprogram.
Declaring a Function: (general syntax)
Function name_of_function (formal parameter list) Return type_of_result Is
Declaration of any variables, etc.
Begin
Statements (including a Return statement)
End name_of_function;
The formal parameter list is like a declaration of the variables being used to calculate the return
value. These variables only exist while the function is actually being executed.
Declaring a Function: (example)
Function Cube (X1 : integer) Return Integer Is
Cube_of_num : integer;
Begin
Cube_of_num := X1 ** 3;
Return Cube_of_num;
End Cube;
Or
Function Cube (X1 : integer) Return Integer Is
Begin
Return X1 ** 3;
End Cube;
Calling a Function: (example)
To call Cube just use it in place of the value you want…include a value in the actual parameter
list that matches (in type) the formal parameter list.
Assume Num has been declared an integer in the program.
Get(Num);
Put(“the number raised to the third power is “); Put(Cube(Num));
Calls Cube and send it the
value in Num
After the function
Cube is executed, it
sends the result back
in place of Cube
Declaring a procedure: (general syntax)
Procedure name_of_procedure (formal parameter list) Is
Declaration of variables, etc.
Begin
Statements
End name_of_procedure;
Declaring a procedure: (example)
Procedure Output_Asterisks (Y : integer) Is
Begin
For I in 1 to Y loop
Put(‘*’);
End loop;
End Output_Asterisks; -- notice not value is sent back to the calling program, only a task is performed
To call the procedure:
Put(“enter how many asterisks you want”);
Get(num);
Output_Asterisks(Num);
Another example: (to center text on the screen) this shows the entire program!!
With text_io;
Use text_io;
Procedure Example_Program is
Title, name, author : string(1..50);
N : integer;
Procedure Center_text (Str_Text : string) is
Length_of_line: constant := 80;
Begin
Set_col((length_of_line - Str_text’length)/2);
Put(Str_text);
End Center_text;
Begin
New_line;
Put_line(“Enter the title of the book”);
Get_line(title,N);
Center_text(Title(1..N));
New_line;
Put_line(“Enter the author’s name”);
Get_line(name,N);
Author(1..N+3) := “by ” & name(1..N);
Center_text(Author(1..N+3));
End Example_Program;
Declaring a procedure that sends values back to the calling program:
You can use In, out, and in out to tell how the variable is to be used. In denotes a one-way
street for values to be given to the subprogram from the calling program. Out denotes a one-way
steet for values to be given back to the calling program from the subprogram. In Out allows for
values to be given to the subprogram by the calling program; changes can then be made and the
result will be sent back in the same variable.
See example for computing the roots of an equation
of type x2 + px + q = 0 below:
P & Q will receive values
from the calling program
Procedure Compute_roots (P,Q : in float; root1,root2 : out float; real_roots : out Boolean) Is
D: float;
Begin
D := p ** 2 / 4.0 – Q;
If D < 0.0 Then
Real_roots := false;
Else
Real_roots := true;
root1 & root2 & real_roots
Root1 := p / 2.0 + sqrt(D);
will give values back to the
Root2 := p / 2.0 + sqrt(D);
calling program (they cannot
End if;
receive values from the call
End Computer_roots;
statement!)
Using this procedure:
With text_io, float.text_io;
Use text_io, float.text_io;
Procedure Example2 is
Num1, Num2 , R1,R2: float;
Ok : Boolean;
Procedure Compute_roots (P,Q : in float; root1,root2 : out float; real_roots : out Boolean) Is
D: float;
Begin
D := p ** 2 / 4.0 – Q;
If D < 0.0 Then
Real_roots := false;
Else
Real_roots := true;
Root1 := p / 2.0 + sqrt(D);
Root2 := p / 2.0 + sqrt(D);
End if;
End Compute_roots;
Begin
Put(“Enter the second coefficient in the equation: ”); Get(Num1);
Put(“Enter the constant in the equation: ”); Get(Num2);
Compute_roots(Num1,Num2,R1,R2,Ok);
If Ok then
Put(“the equation has roots ”);
Put(R1); put(“ and ” ); put(R2);
Else
Put(“The equation has no real roots”)
End if;
End Example2;
Download