can I determine a function by myself? - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# can I determine a function by myself?

Posts: 17Member
Hi!
I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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

• Posts: 6,349Member
: Hi!
: I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
[code]
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.
[/code]
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
• Posts: 17Member
: : Hi!
: : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: [code]
: 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.
: [/code]
: 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

• Posts: 6,349Member
: : : Hi!
: : : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: : [code]
: : 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.
: : [/code]
: : 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.
[code]
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.
[/code]
In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
[code]
program Example;

uses
Crt, MySort;

var
a: array[1..10] of integer
begin
{ Fill a with numbers }
Sort(a);
{ Show sorted a }
end.
[/code]
• Posts: 17Member
: : : : Hi!
: : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: : : [code]
: : : 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.
: : : [/code]
: : : 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.
: [code]
: 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.
: [/code]
: In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: [code]
: program Example;
:
: uses
: Crt, MySort;
:
: var
: a: array[1..10] of integer
: begin
: { Fill a with numbers }
: Sort(a);
: { Show sorted a }
: end.
: [/code]
:
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!!!

• Posts: 6,349Member
: : : : : Hi!
: : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: : : : [code]
: : : : 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.
: : : : [/code]
: : : : 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.
: : [code]
: : 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.
: : [/code]
: : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : [code]
: : program Example;
: :
: : uses
: : Crt, MySort;
: :
: : var
: : a: array[1..10] of integer
: : begin
: : { Fill a with numbers }
: : Sort(a);
: : { Show sorted a }
: : end.
: : [/code]
: :
: 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.
• Posts: 17Member
: : : : : : Hi!
: : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: : : : : [code]
: : : : : 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.
: : : : : [/code]
: : : : : 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.
: : : [code]
: : : 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.
: : : [/code]
: : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : : [code]
: : : program Example;
: : :
: : : uses
: : : Crt, MySort;
: : :
: : : var
: : : a: array[1..10] of integer
: : : begin
: : : { Fill a with numbers }
: : : Sort(a);
: : : { Show sorted a }
: : : end.
: : : [/code]
: : :
: : 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 [b] TP/units [/b] and the programs in the folder [b] TP/work [/b] so where to save it?
When I tried to run the program (that call to the unit) there was an error message ([b][italic]Unit file format error (c:Program FilesTPunits omer.tpu).[/italic][/b]).
I hope you can help me...
• Posts: 6,349Member
: : : : : : : Hi!
: : : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: : : : : : [code]
: : : : : : 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.
: : : : : : [/code]
: : : : : : 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.
: : : : [code]
: : : : 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.
: : : : [/code]
: : : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : : : [code]
: : : : program Example;
: : : :
: : : : uses
: : : : Crt, MySort;
: : : :
: : : : var
: : : : a: array[1..10] of integer
: : : : begin
: : : : { Fill a with numbers }
: : : : Sort(a);
: : : : { Show sorted a }
: : : : end.
: : : : [/code]
: : : :
: : : 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 [b] TP/units [/b] and the programs in the folder [b] TP/work [/b] so where to save it?
: When I tried to run the program (that call to the unit) there was an error message ([b][italic]Unit file format error (c:Program FilesTPunits omer.tpu).[/italic][/b]).
: 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 filesTPunits" will be "c:progra~1TPunits". 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.
• Posts: 17Member
: : : : : : : : Hi!
: : : : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: : : : : : : [code]
: : : : : : : 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.
: : : : : : : [/code]
: : : : : : : 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.
: : : : : [code]
: : : : : 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.
: : : : : [/code]
: : : : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : : : : [code]
: : : : : program Example;
: : : : :
: : : : : uses
: : : : : Crt, MySort;
: : : : :
: : : : : var
: : : : : a: array[1..10] of integer
: : : : : begin
: : : : : { Fill a with numbers }
: : : : : Sort(a);
: : : : : { Show sorted a }
: : : : : end.
: : : : : [/code]
: : : : :
: : : : 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 [b] TP/units [/b] and the programs in the folder [b] TP/work [/b] so where to save it?
: : When I tried to run the program (that call to the unit) there was an error message ([b][italic]Unit file format error (c:Program FilesTPunits omer.tpu).[/italic][/b]).
: : 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 filesTPunits" will be "c:progra~1TPunits". 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 [b][italic]Unit file format error (c:Program FilesTPunits omer.tpu).[/italic][/b]
so if I understand there is problem with the format of the unit. Anyway I am attaching my unit:
[code]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.

• Posts: 6,349Member
: : : : : : : : : Hi!
: : : : : : : : : I wanted to know if I can determine a function by myself...I mean that I will just need to write [italic][b]sort(a)[/b][/italic] 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:
: : : : : : : : [code]
: : : : : : : : 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.
: : : : : : : : [/code]
: : : : : : : : 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.
: : : : : : [code]
: : : : : : 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.
: : : : : : [/code]
: : : : : : In your prorgam you can use this unit like any other unit and call Sort() like any other procedure:
: : : : : : [code]
: : : : : : program Example;
: : : : : :
: : : : : : uses
: : : : : : Crt, MySort;
: : : : : :
: : : : : : var
: : : : : : a: array[1..10] of integer
: : : : : : begin
: : : : : : { Fill a with numbers }
: : : : : : Sort(a);
: : : : : : { Show sorted a }
: : : : : : end.
: : : : : : [/code]
: : : : : :
: : : : : 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 [b] TP/units [/b] and the programs in the folder [b] TP/work [/b] so where to save it?
: : : When I tried to run the program (that call to the unit) there was an error message ([b][italic]Unit file format error (c:Program FilesTPunits omer.tpu).[/italic][/b]).
: : : 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 filesTPunits" will be "c:progra~1TPunits". 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 [b][italic]Unit file format error (c:Program FilesTPunits omer.tpu).[/italic][/b]
: so if I understand there is problem with the format of the unit. Anyway I am attaching my unit:
: [code]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.