Solved

Procedures

Posted on 1998-02-16
23
276 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
kellyjj i think u're rite about that one
I stand corrected...
:)


0
 
LVL 1

Expert Comment

by:zircon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ñ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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:mitchell042997
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok everyone - I'll just erase this question.

Thanks anyway
0
 
LVL 1

Expert Comment

by:zircon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
wy2lam, what is a linked list?
how could I do it with it?

Saar
0
 
LVL 1

Expert Comment

by:zircon
Comment Utility
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
Comment Utility
Saar,  I agree with zircon.  You gotten some pretty good advice here.  YOu should give the points to somebody.
0
 

Expert Comment

by:wy2lam
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

13 Experts available now in Live!

Get 1:1 Help Now