## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14016

This Forum Only

can I determine a function by myself? Posted by tomer_g on 16 Feb 2005 at 9:49 AM
Hi!
I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
of course I need to determine it before (I thing in a unit or in a library) but can I?
please...if it possible - tell me how to do it, OK?
Thank you very much!
Tomer
Re: can I determine a function by myself? Posted by zibadian on 16 Feb 2005 at 4:50 PM
: Hi!
: I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: of course I need to determine it before (I thing in a unit or in a library) but can I?
: please...if it possible - tell me how to do it, OK?
: Thank you very much!
: Tomer
:
Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
```procedure Sort(var a: array[1..10] of integer);
var { <= Optional }
{ local variables }
begin
{ Normal Pascal statements }
end;

function SumArray(a: array[1..10] of integer): integer;
begin
end;

begin
{ Rest of program using SumArray() and Sort() }
end.
```

These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
Re: can I determine a function by myself? Posted by tomer_g on 17 Feb 2005 at 3:34 AM
: : Hi!
: : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : please...if it possible - tell me how to do it, OK?
: : Thank you very much!
: : Tomer
: :
: Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
:
```: procedure Sort(var a: array[1..10] of integer);
: var { <= Optional }
:   { local variables }
: begin
:   { Normal Pascal statements }
: end;
:
: function SumArray(a: array[1..10] of integer): integer;
: begin
: end;
:
: begin
:   { Rest of program using SumArray() and Sort() }
: end.
: ```

: These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
:

Thank you vary much...but I didn't understand the SumArray...y do I need it?

And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
thank you vary much
Tomer

Re: can I determine a function by myself? Posted by zibadian on 17 Feb 2005 at 4:18 AM
: : : Hi!
: : : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : : please...if it possible - tell me how to do it, OK?
: : : Thank you very much!
: : : Tomer
: : :
: : Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
: :
```: : procedure Sort(var a: array[1..10] of integer);
: : var { <= Optional }
: :   { local variables }
: : begin
: :   { Normal Pascal statements }
: : end;
: :
: : function SumArray(a: array[1..10] of integer): integer;
: : begin
: : end;
: :
: : begin
: :   { Rest of program using SumArray() and Sort() }
: : end.
: : ```

: : These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: : For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
: :
:
: Thank you vary much...but I didn't understand the SumArray...y do I need it?
:
: And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
: thank you vary much
: Tomer
:
:
SumArray() was an example of a function defintion. You don't need it for the sort itself.
```unit MySort;

interface

procedure Sort(var a: array[1..10] of integer);

implementation

procedure Sort(var a: array[1..10] of integer);
var { <= Optional }
{ local variables }
begin
{ write the sort code here }
end;

end.
```

In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
```program Example;

uses
Crt, MySort;

var
a: array[1..10] of integer
begin
{ Fill a with numbers }
Sort(a);
{ Show sorted a }
end.
```

Re: can I determine a function by myself? Posted by tomer_g on 18 Feb 2005 at 5:56 AM
: : : : Hi!
: : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : : : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : : : please...if it possible - tell me how to do it, OK?
: : : : Thank you very much!
: : : : Tomer
: : : :
: : : Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
: : :
```: : : procedure Sort(var a: array[1..10] of integer);
: : : var { <= Optional }
: : :   { local variables }
: : : begin
: : :   { Normal Pascal statements }
: : : end;
: : :
: : : function SumArray(a: array[1..10] of integer): integer;
: : : begin
: : : end;
: : :
: : : begin
: : :   { Rest of program using SumArray() and Sort() }
: : : end.
: : : ```

: : : These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: : : For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
: : :
: :
: : Thank you vary much...but I didn't understand the SumArray...y do I need it?
: :
: : And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
: : thank you vary much
: : Tomer
: :
: :
: SumArray() was an example of a function defintion. You don't need it for the sort itself.
:
```: unit MySort;
:
: interface
:
: procedure Sort(var a: array[1..10] of integer);
:
: implementation
:
: procedure Sort(var a: array[1..10] of integer);
: var { <= Optional }
:   { local variables }
: begin
:   { write the sort code here }
: end;
:
: end.
: ```

: In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
:
```: program Example;
:
: uses
:   Crt, MySort;
:
: var
:   a: array[1..10] of integer
: begin
:   { Fill a with numbers }
:   Sort(a);
:   { Show sorted a }
: end.
: ```

:
Thank you very much!!! Ihave just a few questions:
1.Why do I need the interface? the same procedure is in the implementation!!
2.Where to save this unit, how to call this unit(in the file) and where can I make this unit(can I use turbo pascal 7?)
3.what if I want to change the a settings? (like...I won't need array of integer all the time...how can I change the type of the variable? and the size??? like 1..5 17..300 I need all the time an other array so how can I change it?)

Thank you very much about all!!!

Re: can I determine a function by myself? Posted by zibadian on 18 Feb 2005 at 6:45 AM
: : : : : Hi!
: : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : : : : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : : : : please...if it possible - tell me how to do it, OK?
: : : : : Thank you very much!
: : : : : Tomer
: : : : :
: : : : Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
: : : :
```: : : : procedure Sort(var a: array[1..10] of integer);
: : : : var { <= Optional }
: : : :   { local variables }
: : : : begin
: : : :   { Normal Pascal statements }
: : : : end;
: : : :
: : : : function SumArray(a: array[1..10] of integer): integer;
: : : : begin
: : : : end;
: : : :
: : : : begin
: : : :   { Rest of program using SumArray() and Sort() }
: : : : end.
: : : : ```

: : : : These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: : : : For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
: : : :
: : :
: : : Thank you vary much...but I didn't understand the SumArray...y do I need it?
: : :
: : : And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
: : : thank you vary much
: : : Tomer
: : :
: : :
: : SumArray() was an example of a function defintion. You don't need it for the sort itself.
: :
```: : unit MySort;
: :
: : interface
: :
: : procedure Sort(var a: array[1..10] of integer);
: :
: : implementation
: :
: : procedure Sort(var a: array[1..10] of integer);
: : var { <= Optional }
: :   { local variables }
: : begin
: :   { write the sort code here }
: : end;
: :
: : end.
: : ```

: : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: :
```: : program Example;
: :
: : uses
: :   Crt, MySort;
: :
: : var
: :   a: array[1..10] of integer
: : begin
: :   { Fill a with numbers }
: :   Sort(a);
: :   { Show sorted a }
: : end.
: : ```

: :
: Thank you very much!!! Ihave just a few questions:
: 1.Why do I need the interface? the same procedure is in the implementation!!
: 2.Where to save this unit, how to call this unit(in the file) and where can I make this unit(can I use turbo pascal 7?)
: 3.what if I want to change the a settings? (like...I won't need array of integer all the time...how can I change the type of the variable? and the size??? like 1..5 17..300 I need all the time an other array so how can I change it?)
:
: Thank you very much about all!!!
:
:
1: The declarations listed in the interface can be called from other units. The implementation part holds the actual implementations of those declarations and possibly support variables, functions and procedures, which are not designed to be called from other units.
2: YOu need to save it in the same directory in which the program (and other units) are, or in the directory specified in the Unit Directory Options. This unit should be usable by all Turbo Pascal versions.
3: Turbo Pascal 7 does not support dynamic arrays, which makes it virtually impossible to design a global sorting method. Each array you have would need another sorting routine. You can simply duplicate the declaration and implementation of this procedure and make the necessary changes. Remember that TP7 also doesn't feature procedure overloading, so each new procedure must have a new name.
Re: can I determine a function by myself? Posted by tomer_g on 21 Feb 2005 at 5:37 AM
: : : : : : Hi!
: : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : : : : : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : : : : : please...if it possible - tell me how to do it, OK?
: : : : : : Thank you very much!
: : : : : : Tomer
: : : : : :
: : : : : Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
: : : : :
```: : : : : procedure Sort(var a: array[1..10] of integer);
: : : : : var { <= Optional }
: : : : :   { local variables }
: : : : : begin
: : : : :   { Normal Pascal statements }
: : : : : end;
: : : : :
: : : : : function SumArray(a: array[1..10] of integer): integer;
: : : : : begin
: : : : : end;
: : : : :
: : : : : begin
: : : : :   { Rest of program using SumArray() and Sort() }
: : : : : end.
: : : : : ```

: : : : : These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: : : : : For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
: : : : :
: : : :
: : : : Thank you vary much...but I didn't understand the SumArray...y do I need it?
: : : :
: : : : And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
: : : : thank you vary much
: : : : Tomer
: : : :
: : : :
: : : SumArray() was an example of a function defintion. You don't need it for the sort itself.
: : :
```: : : unit MySort;
: : :
: : : interface
: : :
: : : procedure Sort(var a: array[1..10] of integer);
: : :
: : : implementation
: : :
: : : procedure Sort(var a: array[1..10] of integer);
: : : var { <= Optional }
: : :   { local variables }
: : : begin
: : :   { write the sort code here }
: : : end;
: : :
: : : end.
: : : ```

: : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : :
```: : : program Example;
: : :
: : : uses
: : :   Crt, MySort;
: : :
: : : var
: : :   a: array[1..10] of integer
: : : begin
: : :   { Fill a with numbers }
: : :   Sort(a);
: : :   { Show sorted a }
: : : end.
: : : ```

: : :
: : Thank you very much!!! Ihave just a few questions:
: : 1.Why do I need the interface? the same procedure is in the implementation!!
: : 2.Where to save this unit, how to call this unit(in the file) and where can I make this unit(can I use turbo pascal 7?)
: : 3.what if I want to change the a settings? (like...I won't need array of integer all the time...how can I change the type of the variable? and the size??? like 1..5 17..300 I need all the time an other array so how can I change it?)
: :
: : Thank you very much about all!!!
: :
: :
: 1: The declarations listed in the interface can be called from other units. The implementation part holds the actual implementations of those declarations and possibly support variables, functions and procedures, which are not designed to be called from other units.
: 2: YOu need to save it in the same directory in which the program (and other units) are, or in the directory specified in the Unit Directory Options. This unit should be usable by all Turbo Pascal versions.
: 3: Turbo Pascal 7 does not support dynamic arrays, which makes it virtually impossible to design a global sorting method. Each array you have would need another sorting routine. You can simply duplicate the declaration and implementation of this procedure and make the necessary changes. Remember that TP7 also doesn't feature procedure overloading, so each new procedure must have a new name.
:
But I can't save it in the same directory in which the program (and other units) are because the units in the folder TP/units and the programs in the folder TP/work so where to save it?
When I tried to run the program (that call to the unit) there was an error message (Unit file format error (c:\Program Files\TP\units\tomer.tpu).).
I hope you can help me...
Re: can I determine a function by myself? Posted by zibadian on 21 Feb 2005 at 5:48 AM
: : : : : : : Hi!
: : : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : : : : : : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : : : : : : please...if it possible - tell me how to do it, OK?
: : : : : : : Thank you very much!
: : : : : : : Tomer
: : : : : : :
: : : : : : Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
: : : : : :
```: : : : : : procedure Sort(var a: array[1..10] of integer);
: : : : : : var { <= Optional }
: : : : : :   { local variables }
: : : : : : begin
: : : : : :   { Normal Pascal statements }
: : : : : : end;
: : : : : :
: : : : : : function SumArray(a: array[1..10] of integer): integer;
: : : : : : begin
: : : : : : end;
: : : : : :
: : : : : : begin
: : : : : :   { Rest of program using SumArray() and Sort() }
: : : : : : end.
: : : : : : ```

: : : : : : These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: : : : : : For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
: : : : : :
: : : : :
: : : : : Thank you vary much...but I didn't understand the SumArray...y do I need it?
: : : : :
: : : : : And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
: : : : : thank you vary much
: : : : : Tomer
: : : : :
: : : : :
: : : : SumArray() was an example of a function defintion. You don't need it for the sort itself.
: : : :
```: : : : unit MySort;
: : : :
: : : : interface
: : : :
: : : : procedure Sort(var a: array[1..10] of integer);
: : : :
: : : : implementation
: : : :
: : : : procedure Sort(var a: array[1..10] of integer);
: : : : var { <= Optional }
: : : :   { local variables }
: : : : begin
: : : :   { write the sort code here }
: : : : end;
: : : :
: : : : end.
: : : : ```

: : : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : : :
```: : : : program Example;
: : : :
: : : : uses
: : : :   Crt, MySort;
: : : :
: : : : var
: : : :   a: array[1..10] of integer
: : : : begin
: : : :   { Fill a with numbers }
: : : :   Sort(a);
: : : :   { Show sorted a }
: : : : end.
: : : : ```

: : : :
: : : Thank you very much!!! Ihave just a few questions:
: : : 1.Why do I need the interface? the same procedure is in the implementation!!
: : : 2.Where to save this unit, how to call this unit(in the file) and where can I make this unit(can I use turbo pascal 7?)
: : : 3.what if I want to change the a settings? (like...I won't need array of integer all the time...how can I change the type of the variable? and the size??? like 1..5 17..300 I need all the time an other array so how can I change it?)
: : :
: : : Thank you very much about all!!!
: : :
: : :
: : 1: The declarations listed in the interface can be called from other units. The implementation part holds the actual implementations of those declarations and possibly support variables, functions and procedures, which are not designed to be called from other units.
: : 2: YOu need to save it in the same directory in which the program (and other units) are, or in the directory specified in the Unit Directory Options. This unit should be usable by all Turbo Pascal versions.
: : 3: Turbo Pascal 7 does not support dynamic arrays, which makes it virtually impossible to design a global sorting method. Each array you have would need another sorting routine. You can simply duplicate the declaration and implementation of this procedure and make the necessary changes. Remember that TP7 also doesn't feature procedure overloading, so each new procedure must have a new name.
: :
: But I can't save it in the same directory in which the program (and other units) are because the units in the folder TP/units and the programs in the folder TP/work so where to save it?
: When I tried to run the program (that call to the unit) there was an error message (Unit file format error (c:\Program Files\TP\units\tomer.tpu).).
: I hope you can help me...
:
You can specify additional places in the Options|Directories|Unit Directories. Pascal uses the folders listed here to find the units. See help files for more info. Also note that Pascal doesn't use long filenames. This means that the folder "c:\program files\TP\units" will be "c:\progra~1\TP\units". You can always save the unit source code (*.pas file) in the same directory as the program source code (*.pas). The TPU file is not necessary for the program to run. If you want to compile another program using this unit, you must have either the *.pas or the *.tpu file.
Re: can I determine a function by myself? Posted by tomer_g on 22 Feb 2005 at 10:48 PM
: : : : : : : : Hi!
: : : : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : : : : : : : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : : : : : : : please...if it possible - tell me how to do it, OK?
: : : : : : : : Thank you very much!
: : : : : : : : Tomer
: : : : : : : :
: : : : : : : Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
: : : : : : :
```: : : : : : : procedure Sort(var a: array[1..10] of integer);
: : : : : : : var { <= Optional }
: : : : : : :   { local variables }
: : : : : : : begin
: : : : : : :   { Normal Pascal statements }
: : : : : : : end;
: : : : : : :
: : : : : : : function SumArray(a: array[1..10] of integer): integer;
: : : : : : : begin
: : : : : : : end;
: : : : : : :
: : : : : : : begin
: : : : : : :   { Rest of program using SumArray() and Sort() }
: : : : : : : end.
: : : : : : : ```

: : : : : : : These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: : : : : : : For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
: : : : : : :
: : : : : :
: : : : : : Thank you vary much...but I didn't understand the SumArray...y do I need it?
: : : : : :
: : : : : : And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
: : : : : : thank you vary much
: : : : : : Tomer
: : : : : :
: : : : : :
: : : : : SumArray() was an example of a function defintion. You don't need it for the sort itself.
: : : : :
```: : : : : unit MySort;
: : : : :
: : : : : interface
: : : : :
: : : : : procedure Sort(var a: array[1..10] of integer);
: : : : :
: : : : : implementation
: : : : :
: : : : : procedure Sort(var a: array[1..10] of integer);
: : : : : var { <= Optional }
: : : : :   { local variables }
: : : : : begin
: : : : :   { write the sort code here }
: : : : : end;
: : : : :
: : : : : end.
: : : : : ```

: : : : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : : : :
```: : : : : program Example;
: : : : :
: : : : : uses
: : : : :   Crt, MySort;
: : : : :
: : : : : var
: : : : :   a: array[1..10] of integer
: : : : : begin
: : : : :   { Fill a with numbers }
: : : : :   Sort(a);
: : : : :   { Show sorted a }
: : : : : end.
: : : : : ```

: : : : :
: : : : Thank you very much!!! Ihave just a few questions:
: : : : 1.Why do I need the interface? the same procedure is in the implementation!!
: : : : 2.Where to save this unit, how to call this unit(in the file) and where can I make this unit(can I use turbo pascal 7?)
: : : : 3.what if I want to change the a settings? (like...I won't need array of integer all the time...how can I change the type of the variable? and the size??? like 1..5 17..300 I need all the time an other array so how can I change it?)
: : : :
: : : : Thank you very much about all!!!
: : : :
: : : :
: : : 1: The declarations listed in the interface can be called from other units. The implementation part holds the actual implementations of those declarations and possibly support variables, functions and procedures, which are not designed to be called from other units.
: : : 2: YOu need to save it in the same directory in which the program (and other units) are, or in the directory specified in the Unit Directory Options. This unit should be usable by all Turbo Pascal versions.
: : : 3: Turbo Pascal 7 does not support dynamic arrays, which makes it virtually impossible to design a global sorting method. Each array you have would need another sorting routine. You can simply duplicate the declaration and implementation of this procedure and make the necessary changes. Remember that TP7 also doesn't feature procedure overloading, so each new procedure must have a new name.
: : :
: : But I can't save it in the same directory in which the program (and other units) are because the units in the folder TP/units and the programs in the folder TP/work so where to save it?
: : When I tried to run the program (that call to the unit) there was an error message (Unit file format error (c:\Program Files\TP\units\tomer.tpu).).
: : I hope you can help me...
: :
: You can specify additional places in the Options|Directories|Unit Directories. Pascal uses the folders listed here to find the units. See help files for more info. Also note that Pascal doesn't use long filenames. This means that the folder "c:\program files\TP\units" will be "c:\progra~1\TP\units". You can always save the unit source code (*.pas file) in the same directory as the program source code (*.pas). The TPU file is not necessary for the program to run. If you want to compile another program using this unit, you must have either the *.pas or the *.tpu file.
:
So...I didn't understand how do I need to save it...as a *.pas or as *.tpu???
There is no problem with finding the unit, because the error message is Unit file format error (c:\Program Files\TP\units\tomer.tpu).
so if I understand there is problem with the format of the unit. Anyway I am attaching my unit:
```unit Tomer;

interface

procedure sort(var a: array [1..10] of integer);

implementation

procedure sort(var a: array [1..10] of integer);
var
i,j,small,cell:integer;
begin
for i:=1 to 9 do
begin
cell:=i;
small:=a[i];
for j:=i+1 to 10 do
begin
if a[j]<small then
begin
small:=a[j];
cell:=j;
end;
end;
a[cell]:=a[i];
a[i]:=small;
end;
end;

end.

```
Re: can I determine a function by myself? Posted by zibadian on 23 Feb 2005 at 12:11 AM
: : : : : : : : : Hi!
: : : : : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write sort(a) and it will sort the array - a.
: : : : : : : : : of course I need to determine it before (I thing in a unit or in a library) but can I?
: : : : : : : : : please...if it possible - tell me how to do it, OK?
: : : : : : : : : Thank you very much!
: : : : : : : : : Tomer
: : : : : : : : :
: : : : : : : : Yes, you can define a function or procedure yourself. For that you need to use the function/procedure reserved words. Here is an example:
: : : : : : : :
```: : : : : : : : procedure Sort(var a: array[1..10] of integer);
: : : : : : : : var { <= Optional }
: : : : : : : :   { local variables }
: : : : : : : : begin
: : : : : : : :   { Normal Pascal statements }
: : : : : : : : end;
: : : : : : : :
: : : : : : : : function SumArray(a: array[1..10] of integer): integer;
: : : : : : : : begin
: : : : : : : : end;
: : : : : : : :
: : : : : : : : begin
: : : : : : : :   { Rest of program using SumArray() and Sort() }
: : : : : : : : end.
: : : : : : : : ```

: : : : : : : : These user-defined procedures/functions are called exactly the same way as predefined procedures/functions.
: : : : : : : : For making a unit check this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=291740&Setting=A0007F0001
: : : : : : : :
: : : : : : :
: : : : : : : Thank you vary much...but I didn't understand the SumArray...y do I need it?
: : : : : : :
: : : : : : : And I mean that I want to write sort(a) every time without this procedure...like in any time I want to...so how do I write a unit with the code? from the message you gave me I couldn't understand how to do that I will use the sort() everytime I want...without to put this function or procedure...
: : : : : : : thank you vary much
: : : : : : : Tomer
: : : : : : :
: : : : : : :
: : : : : : SumArray() was an example of a function defintion. You don't need it for the sort itself.
: : : : : :
```: : : : : : unit MySort;
: : : : : :
: : : : : : interface
: : : : : :
: : : : : : procedure Sort(var a: array[1..10] of integer);
: : : : : :
: : : : : : implementation
: : : : : :
: : : : : : procedure Sort(var a: array[1..10] of integer);
: : : : : : var { <= Optional }
: : : : : :   { local variables }
: : : : : : begin
: : : : : :   { write the sort code here }
: : : : : : end;
: : : : : :
: : : : : : end.
: : : : : : ```

: : : : : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : : : : :
```: : : : : : program Example;
: : : : : :
: : : : : : uses
: : : : : :   Crt, MySort;
: : : : : :
: : : : : : var
: : : : : :   a: array[1..10] of integer
: : : : : : begin
: : : : : :   { Fill a with numbers }
: : : : : :   Sort(a);
: : : : : :   { Show sorted a }
: : : : : : end.
: : : : : : ```

: : : : : :
: : : : : Thank you very much!!! Ihave just a few questions:
: : : : : 1.Why do I need the interface? the same procedure is in the implementation!!
: : : : : 2.Where to save this unit, how to call this unit(in the file) and where can I make this unit(can I use turbo pascal 7?)
: : : : : 3.what if I want to change the a settings? (like...I won't need array of integer all the time...how can I change the type of the variable? and the size??? like 1..5 17..300 I need all the time an other array so how can I change it?)
: : : : :
: : : : : Thank you very much about all!!!
: : : : :
: : : : :
: : : : 1: The declarations listed in the interface can be called from other units. The implementation part holds the actual implementations of those declarations and possibly support variables, functions and procedures, which are not designed to be called from other units.
: : : : 2: YOu need to save it in the same directory in which the program (and other units) are, or in the directory specified in the Unit Directory Options. This unit should be usable by all Turbo Pascal versions.
: : : : 3: Turbo Pascal 7 does not support dynamic arrays, which makes it virtually impossible to design a global sorting method. Each array you have would need another sorting routine. You can simply duplicate the declaration and implementation of this procedure and make the necessary changes. Remember that TP7 also doesn't feature procedure overloading, so each new procedure must have a new name.
: : : :
: : : But I can't save it in the same directory in which the program (and other units) are because the units in the folder TP/units and the programs in the folder TP/work so where to save it?
: : : When I tried to run the program (that call to the unit) there was an error message (Unit file format error (c:\Program Files\TP\units\tomer.tpu).).
: : : I hope you can help me...
: : :
: : You can specify additional places in the Options|Directories|Unit Directories. Pascal uses the folders listed here to find the units. See help files for more info. Also note that Pascal doesn't use long filenames. This means that the folder "c:\program files\TP\units" will be "c:\progra~1\TP\units". You can always save the unit source code (*.pas file) in the same directory as the program source code (*.pas). The TPU file is not necessary for the program to run. If you want to compile another program using this unit, you must have either the *.pas or the *.tpu file.
: :
: So...I didn't understand how do I need to save it...as a *.pas or as *.tpu???
: There is no problem with finding the unit, because the error message is Unit file format error (c:\Program Files\TP\units\tomer.tpu).
: so if I understand there is problem with the format of the unit. Anyway I am attaching my unit:
:
```unit Tomer;
:
: interface
:
: procedure sort(var a: array [1..10] of integer);
:
: implementation
:
: procedure sort(var a: array [1..10] of integer);
: var
:    i,j,small,cell:integer;
: begin
:      for i:=1 to 9 do
:        begin
:          cell:=i;
:          small:=a[i];
:          for j:=i+1 to 10 do
:            begin
:              if a[j]<small then
:                begin
:                  small:=a[j];
:                  cell:=j;
:                end;
:            end;
:          a[cell]:=a[i];
:          a[i]:=small;
:        end;
: end;
:
: end.
:
:
As a .pas. Pascal makes the tpu for you.
```

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic