Solved

Procedures

Posted on 1998-02-16
23
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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