[fpc-pascal] Function to create a record ?

Ralf Quint freedos.la at gmail.com
Fri Jun 2 02:43:58 CEST 2023


On 6/1/2023 5:36 PM, Steve Litt via fpc-pascal wrote:
> Hi all,
>
> I'm trying to create a function (called newperson()) that creates a new
> instance of a record. The following compiles with no errors or warnings
> in FPC, but multiple calls to newperson() produce exactly the same
> variable, so in the preceding changing person changes person2, and vice
> versa:
>
> ===================================================
> program test2;
>
> const
> 	junkvar_size = 20000000;
>
> type
> 	personrecord = record
> 	name: string;
> 	end;
> var
> 	person: personrecord;
> 	person2: personrecord;
> 	junkvar: array[1..junkvar_size] of char;
>
> function newperson(): personrecord;
> var newp: personrecord;
> begin
> 	newp.name := '';
> 	newperson := newp;
> end;
>
> function modperson(person: personrecord; name: string): personrecord;
> begin
> 	person.name := name;
> 	modperson := person;
> end;
>
> procedure writeperson(person: personrecord);
> begin
> 	writeln(person.name);
> end;
>
> begin
> 	person := newperson();
> 	fillchar(junkvar, junkvar_size, 'a');
> 	person2 := newperson();
> 	fillchar(junkvar, junkvar_size, 'b');
> 	person := modperson(person, 'Martin');
> 	person := modperson(person2, 'Maria');
> 	{writeln(junkvar);}
> 	writeperson(person);
> 	writeperson(person2);
> end.
> ===================================================
>
> What is the best way for me to construct newperson() so that every time
> called it would return a new variable?
W.T.F.?

Sorry, but for how long are you programming, in general, and for how 
long in Pascal?

Seriously, you should take some very basic Pascal tutorials, work 
through them, and in particular, pay close attention to the "scope" of 
variables as well as parameter passing. You seem to be completely 
oblivious to those very basics here... :(

Ralf




More information about the fpc-pascal mailing list