Link to home
Start Free TrialLog in
Avatar of saar071697
saar071697

asked on

make a procedure with unspecified nuber of parmeters

is there any way to make a procedure like readln or writeln that are'nt limited in the nubers of parameters.
Avatar of graham_k
graham_k

I can tell you how to do it in C. So, if no-one else comes up with an answer, maybe you can code a separate unit in C & link it in with your Pascal?

Sorry not to be of more help, but I don't know how to do it in Pascal.
I don't think it is possible. At least not in Pascal.

What you *could* do to emulate this (but it's a lot of work) is

Type
  PExtParam=^TExtParam;
  TExtParam=Record
    Id:Word;
    Data:Pointer;
[... Next:PExtParam...]
  End;

Then you could (just as you wish) pass a dynamic array or a linked list of these records.
The Id could be something like ID_STRING or ID_INTEGER, which tells the procedure how to interpret the value.

If you used a linked list that ultimately gets destroyed in the called function, you could then do something (assuming Pa(id,data,next) is a function that adds a record to a list and returns a pointer to the record) like:

MyString:='The value of MyInteger is: ';
String2:='. Have a nice day!';
MyInteger:=33;
MyFunction(
  Pa(ID_STRING,@MyString,
  Pa(ID_INTEGER,@MyInteger,
  Pa(ID_STRING,@String2,
  Nil
  ))));

But as I said, it's a lot of work and I don't know if you want to go to such an extent.
ASKER CERTIFIED SOLUTION
Avatar of smartkid
smartkid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
About is a routine to decode an Integer into an array of boolean. In fact, each element in the array do not have to be the same type. You can combine strings, Integers, Floats, etc, in one array and pass the array as a hole to a procedure. It's a very useful tech.
well, if you _really_ want to do it the hard way - the caller allocates a block of memory. The first 4 bytes hold the length of the memory block. Tehn come a series of entries. Each has one byte to say what type of variable - char, int, etc, then a veriable number of bytes for the data (strings would have length as byte zer0, just like normal Pascal strings).

The caller apsses a pointer to this memory & the routine decodes it & frees teh memory.
The Answer is NO WAY... :(
Yeah, whatboy, but he didn't come here to hear that ;-).
Though Pascal doesn't extended parameters by default, the functionality _can_ be emulated in a number of ways, mine and graham's answers as examples.
The Answer is NO WAY... :(
hey, nrico, I must have missed your comment when posting mine. It's basically the same thing. So, if saar really want to do it, he must follow that method. You can have the points.
Stubborn guy! >-(

saar, if you want I can write some sample code (or a unit).
<-)