Avatar billede dsj Nybegynder
21. juni 2003 - 17:04 Der er 2 kommentarer og
1 løsning

Funktions-erklæring

Hvad er forskellen på disse to erklæringer af en funktion:

1:
function Sum (const A: array of Double): Double;

2:
function Sum (A: array of Double): Double;

Jeg ved godt der ikke står const i nr.2, men hvad er den praktiske betydning?
Avatar billede athlon-pascal Juniormester
21. juni 2003 - 17:36 #1
Fra hjælpen:
"A constant (const) parameter is like a local constant or read-only variable. Constant parameters are similar to value parameters, except that you can’t assign a value to a constant parameter within the body of a procedure or function, nor can you pass one as a var parameter to another routine. (But when you pass an object reference as a constant parameter, you can still modify the object’s properties.)
Using const allows the compiler to optimize code for structured- and string-type parameters. It also provides a safeguard against unintentionally passing a parameter by reference to another routine.

Here, for example, is the header for the CompareStr function in the SysUtils unit:

function CompareStr(const S1, S2: string): Integer;

Because S1 and S2 are not modified in the body of CompareStr, they can be declared as constant parameters."
Avatar billede athlon-pascal Juniormester
21. juni 2003 - 17:40 #2
Se også følgende (fra hjælpen igen):
"Most parameters are either value parameters (the default) or variable (var) parameters. Value parameters are passed by value, while variable parameters are passed by reference. To see what this means, consider the following functions.

function DoubleByValue(X: Integer): Integer;    // X is a value parameter

begin
  X := X * 2;
  Result := X;
end;

function DoubleByRef(var X: Integer): Integer;  // X is a variable parameter

begin
  X := X * 2;
  Result := X;
end;

These functions return the same result, but only the second one—DoubleByRef—can change the value of a variable passed to it. Suppose we call the functions like this:

var

  I, J, V, W: Integer;
begin
  I := 4;
  V := 4;
  J := DoubleByValue(I);  // J = 8, I = 4
  W := DoubleByRef(V);    // W = 8, V = 8
end;

After this code executes, the variable I, which was passed to DoubleByValue, has the same value we initially assigned to it. But the variable V, which was passed to DoubleByRef, has a different value.
A value parameter acts like a local variable that gets initialized to the value passed in the procedure or function call. If you pass a variable as a value parameter, the procedure or function creates a copy of it; changes made to the copy have no effect on the original variable and are lost when program execution returns to the caller.

A variable parameter, on the other hand, acts like a pointer rather than a copy. Changes made to the parameter within the body of a function or procedure persist after program execution returns to the caller and the parameter name itself has gone out of scope.
Even if the same variable is passed in two or more var parameters, no copies are made. This is illustrated in the following example.

procedure AddOne(var X, Y: Integer);

begin
  X := X + 1;
  Y := Y + 1;
end;

var I: Integer;

begin
  I := 1;
  AddOne(I, I);
end;

After this code executes, the value of I is 3.
If a routine’s declaration specifies a var parameter, you must pass an assignable expression—that is, a variable, typed constant (in the {$J+} state), dereferenced pointer, field, or indexed variable—to the routine when you call it. To use our previous examples, DoubleByRef(7) produces an error, although DoubleByValue(7) is legal.
Indexes and pointer dereferences passed in var parameters—for example, DoubleByRef(MyArray[I])—are evaluated once, before execution of the routine."
Avatar billede athlon-pascal Juniormester
21. juni 2003 - 17:57 #3
"const" betyder at parametrene ikke kan ændres, at de er skrivebeskyttet.

function IncInt(const Hest: Integer): Integer;
begin
  Hest := Hest + 1;
  Result := Hest;
end;

Denne funktion vil give fejl, da der er forsøgt at ændre i constanten (Hest := Hest + 1).
Derimod vil følgende funktion ikke give fejl:

function IncInt(Hest: Integer): Integer;
begin
  Hest := Hest + 1;
  Result := Hest;
end;

Bruges den viste funktion, kan det se sådan ud:

var
  I, J: Integer;
begin
  I := 5;
  J := IncInt(I);
  ShowMessage('I: ' + IntToStr(I) + ', J: ' + IntToStr(J));
end;

ShowMessage vil vise en besked med teksten "I: 5, J: 6".
Nu prøver vi med "var":

function IncInt(var Hest: Integer): Integer;
begin
  Hest := Hest + 1;
  Result := Hest;
end;

Igen prøver vi med ShowMessage:

var
  I, J: Integer;
begin
  I := 5;
  J := IncInt(I);
  ShowMessage('I: ' + IntToStr(I) + ', J: ' + IntToStr(J));
end;

Nu vil ShowMessage vise en besked med teksten "I: 6, J: 6"!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester