Recent

Author Topic: Using a Procedure to modify the value of any variable passed in  (Read 3331 times)

Micayah Ritchie

  • New Member
  • *
  • Posts: 18
Good Day,
I'm not sure if this is possible or if I'm just being stupid here but I have this procedure that reads groups of data from a file I stored the data in. I want to use the procedure to change the value of variables which the program uses. The thing is that I have multiple variables that could potentially need to be assigned the values from the file. I don't want to turn it into a function since there are multiple things to output and multiple functions would require accessing the file multiple times to get the things in the right order. Of course I could just have one procedure for every combination of variables that are supposed to get the values from the file but that would require writing the same code multiple times. So that being said, is there some way to just pass into the procedure the variables I want to get the values and have their values permanently changed?
I tried doing this by letting the procedure require the procedure to take in variables and assign the values from the file to them
Code: Pascal  [Select][+][-]
  1. procedure ColorChanger (ColorFile:string; ColorName:string; ColorValue:integer);
For example, above I would want the procedure to take in some string variable for ColorName and the value for ColorName from the file be assigned to it.

I am using Lazarus v1.8.4 on Windows 10

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Using a Procedure to modify the value of any variable passed in
« Reply #1 on: January 28, 2019, 03:06:26 am »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure ColorChanger (ColorFile:string; var ColorName:string; var ColorValue:integer);
or this:
Code: Pascal  [Select][+][-]
  1. procedure ColorChanger (ColorFile:string; out ColorName:string; out ColorValue:integer);

Maybe you should read the Language Guide and the rest of the docs, or find a good tutorial on Object Pasca (search the wiki).
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Micayah Ritchie

  • New Member
  • *
  • Posts: 18
Re: Using a Procedure to modify the value of any variable passed in
« Reply #2 on: January 28, 2019, 03:45:02 am »
Thank you, I will try that, but I've been doing significant amounts of searching for explanations but sometimes it's difficult to know where to look for what and sometimes it would seem that explanations (reasonable) assume you already know a lot of things. That being said I should still try to read all the documentation. Thanks a lot.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Using a Procedure to modify the value of any variable passed in
« Reply #3 on: January 28, 2019, 04:01:14 am »
Yeah, programming is a complex thing. The documentation is massive, we can get lost trying to find the information we need.

Here are the things you need to know:

https://www.freepascal.org/docs-html/current/ref/refsu66.html#x178-20000014.4.3
https://www.freepascal.org/docs-html/current/ref/refsu65.html#x177-19900014.4.2
https://www.freepascal.org/docs-html/current/ref/refsu64.html#x176-19800014.4.1

 

TinyPortal © 2005-2018