Solved

Procedures

Posted on 1998-02-16
23
282 Views
Last Modified: 2010-04-16
Hi

How could I create a procedure which gets a changed number of parmeters.

For example we can see the Inc procedure :
It can get one parameter (the variable) or two parameters (the variable and the increased number).

Thanks,
Saar Carmi

Email: saar@bigfoot.com
UIN: 3233305
0
Comment
Question by:saar2
  • 6
  • 4
  • 3
  • +7
23 Comments
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1217330
In C/C++ you can use va_list to do this.
I don't that it is possible in Pascal.
Maybe it is possible in assembly, because the assembler-procedure can get the parameters in the stack.
0
 
LVL 4

Author Comment

by:saar2
ID: 1217331
Do you know that it isn't possiable or you just don't know how to do it?

Thanks anyway,
Saar
0
 

Expert Comment

by:bslim
ID: 1217332
Hmm, I may be wrong at this in Pascal, but I managed to find out that in Java,in order to have a procedure which can utilize  one or two parameters was to make 2 procedures,one with one parameter and one with 2 and naming it both the same.
Hopefully you could do it in Pascal as well.

Hope it works!
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1217333
It is not possible to have variable length parameter lists to a procedure/function.  You can do it C/C++.
0
 
LVL 4

Author Comment

by:saar2
ID: 1217334
bslim - I don't believe that compailer will let me do it, but I'll check it out.

kellyii - So how is the procedure Inc was built?

Thanks,
Saar
0
 

Expert Comment

by:bslim
ID: 1217335
Another way is to have 2 procedures....hmmm....
but it takes quite a block of mem...

Hope it works again! :)

0
 

Expert Comment

by:bslim
ID: 1217336
kellyjj i think u're rite about that one
I stand corrected...
:)


0
 
LVL 1

Expert Comment

by:zircon
ID: 1217337
Are all of the possible parameters that may be passed to the procedure of the same type (e.g., all integers or all real)?

If they are, could you just use an array to pass varying numbers of "variables" within the array to the procedure?

This solution would be kind of a back door approach, but it should work if all of the parameters passed to the procedure are of the same type or compatble types.

0
 
LVL 4

Author Comment

by:saar2
ID: 1217338
Zircon - I don't realy need to use a procedure like this - I just want to learn how I can build a procedure like Inc for example.

Saar
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1217339
saar2, you can't do it in standard pascal. Probably one of its implemetations support it.

Inc (and write, etc.) are pascal built-in functions, and so written (somehow) in assembler.
You may also remember some functions which allow different types of arguments (sqr, abs, etc.).

If you realy want to do this you must either use a precompiler, use a procedure which has max. parameters, or use zircon's array suggestion.
0
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1217340
ñ2
You can also send as a parameter only a pointer to a block of data for the procedure. The block may be in any size you want and include any data you want. But then you have much more work to do in the procedure...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:mitchell042997
ID: 1217341
TurboPascal allows Object Oriented Programming, right?  (I haven't tried it out, but I heard.)  In C++ when you build a class you can have two functions with the same name and different parameter lists and the right one will be called.  Try making a class in TP and having two functions in the class with the same name and differing parameter lists.
0
 

Expert Comment

by:bluebird031198
ID: 1217342
I'm PS,
You should create some variable to accept all possible parameters. If the parameter is not passed then assign it to 0(integer) or ''(string)
0
 

Expert Comment

by:Nanos
ID: 1217343
Hey saar... as I recal that is IMPOSSIBLE in pascal, it maybe possible in some circumstances in C/C++, but I'm pretty sure that it is not possible in Pascal!!
with regards.. Nanos
0
 
LVL 4

Author Comment

by:saar2
ID: 1217344
Ok everyone - I'll just erase this question.

Thanks anyway
0
 
LVL 1

Expert Comment

by:zircon
ID: 1217345
Have you tested the the possible "work arounds" suggested by ahoffmann, FuzzyLogic, bluebird, myself and others have provided?  You have received some potentially viable answers here, other than "it can't be done."  I don't think it is right to simply delete the question. If truly "can't be done" then that is the correct answer and the points should be awarded to the first person who said it.  If one of the "work arounds" does the trick, then the person who first suggested the best "work around" should be awarded the points.  You should decide who's comment is the best answer or solution and ask them to repost it as an answer or ask for additional follow-up on a particular concept offered as a comment.

Just my 2 cents...
0
 

Accepted Solution

by:
wy2lam earned 100 total points
ID: 1217346
There's no official way to pass variable number of arguments to a Pascal procedure/function.  However, there are a few possible workarounds:

1. Pass a dynamic array which holds the argument
2. A linked list...
3. Just any type of structured data type if you want.
0
 
LVL 1

Expert Comment

by:zircon
ID: 1217347
e.g., pass an array of records if you want to pass variable number and types of arguments.
0
 
LVL 4

Author Comment

by:saar2
ID: 1217348
wy2lam/zircon/bluebird
Passing an array/record is not a solution because this is not a how INC works.
My problem isn't the programing itself, I just wanted to know how to pass variable number of parameters to a procedure - I don't want to use any trick to do this.

Zircon, I asked "How could I ..." not "Could I ..." - this mean that no answer is not an answer.

Saar

0
 
LVL 4

Author Comment

by:saar2
ID: 1217349
wy2lam, what is a linked list?
how could I do it with it?

Saar
0
 
LVL 1

Expert Comment

by:zircon
ID: 1217350
Saar,

I agree that "it can't be done..." is not a legitimate answer to this question, because of the way you phrased the question and the fact that it can be done.

Passing a data structure such as an array or record is a legitimate answer to the question "How could I..." and does not constitute an answer of "it can't be done..." to the answer "Could I..."

Even if the variable numbers of parameters are not passed in "exactly" the same manner as with the Inc procedure, if the net effect is the ability to pass a variable number of parameters, then the end result is a valid solution. Without the advantages of incorporating the capabilities of assembly languages directly into Pascal code, we may not be able to duplicate the exact method used by the Inc procedure, so a "work-around" is in order.

At any rate, you've gotten some potential solutions that will work.  I think the most comprehensive of them is the current proposed answer from wy2lam.  Perhaps (s)he should get them points.

I hope you find a satisfactory solution.
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1217351
Saar,  I agree with zircon.  You gotten some pretty good advice here.  YOu should give the points to somebody.
0
 

Expert Comment

by:wy2lam
ID: 1217352
A linked list is a dynamic list (ordered) structure that has a property that an element in it is linked to 1 or 2 other elements in the same list via a pointer / reference.  I dun think it's appropriate to discuss this in this thread since a dynamic array holding pointers to different data types should be enough for this purpose.  (i.e. linked list is inferior to an array for this task).

If you want to know more about linked list, any book about elementary data structure & algorithm should be able to explain this concept clearly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now