1

in delphi7 i have a function that i need to return a array as a result type b

"function createsbox(key:tkey):array[0..255] of byte;" this is not allowed it is expecting a "identifyer expected but array found" is the error throw up. seems to work fine if i declare a record type of array but it seems pointless to do this for one function.

3 Answers 3

15

The issue is that you're not allowed to create a new type in a function declaration. But that's what you're doing when you specify the return type as array[0..255] of Byte. Instead, declare a named type, and then use it for the return type:

type
  TSBox = array[0..255] of Byte;

function CreateSBox(const Key: TKey): TSBox;
Sign up to request clarification or add additional context in comments.

4 Comments

Note that using such a type as a function result is slow, because it will copy the content from the stack to the resulting value. You should have written procedure CreateSBox(const Key: TKey; var Result: TSBox); for faster execution. It will make a difference in case of a huge array size.
@A.Bouchez: Not in Delphi (but maybe in FPC). In Delphi dynamic types such as String/dyn-arrays/interfaces as Result is passed automaticly similar to CreateSBox(const Key: TKey; var Result: TSBox). See example code copypastecode.com/60195 but because of this optimization there is a long-living bug in compiler (not) generating warning, see for details: qc.embarcadero.com/wc/qcmain.aspx?d=894 (it's not only about function Result as String, but also Interfaces, dyn-arrays, records)
@kibab: I didn't speak about reference-countet type, but for a record or static array (like this TSBox = array[0..255] of Byte). In this case, the content is copied from the stack, not passed as reference.
It seems that for static arrays (or even records) like this TSBox is same (at least under D2009) - no copy, just pointer to that variable. Could you try this? I could make some mistake during simple test. In CPU-view I see only simple MOV followed by CALL.
3

There is a subtle reason for that, and it is in Pascal two array type declarations are not the same "type" regardless of how identical their declaration are, and thereby not assignment compatible. If you write:

var
  A: array[1..10] of Integer;
  B: array[1..10] of Integer;

A and B are different types. If you write

A := B;

The code won't compile, A and B are different types.

Thereby, if you write

var
  A: array[1..10] of Integer;
...
function Foo(...): array[1..10] of Integer;

You're actually declaring a type for the function result - that type would be pretty useless because you could not assign it to A or any array no matter how it s declaration is, for example:

  A := Foo(...);

would not work even if the compiler would let you to declare a function that way.

The only way to have a useful function result type thereby is to use a type already declared. Only open arrays are an exception to this rule, but they can be used only as function parameters, not as the result.

Comments

1

okay type TSBox = array of Byte works fine, but using this new type in 2 or more units, could be tricky. 'Cause u will get an error message "incompatible types". There's a partial solution for this situation. U can inform the compiler which unit has that type with this notation: unit.type; Example: Imagine there I have a unit called Web, where I declare the type TDownload. I could do something like this: var fileUrl : Web.TDownload; In this case the compiler will understand that u r using the same type. However, and thats my question for u all: what if u don't want to put that unit in the uses clausule for whatever - circular reference, poo good practices, etc.. What can I do to avoid this problem?

1 Comment

Use procedure CreateSBox(const Key: TKey; var Result: TSBox) as I wrote in the comment above. The var parameter will check for EXACT type match. And it will run faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.