Creating a function / procedure alike JAVA's void() lazarus / 21-General ---------------------------------------------------------------------- itzghee · 2021-03-04 23:06 Hello, I have a question concerning the creation of a function or a procudure i can call from the programm in order to "lighten" the amount of code written. I plan to write a programm which chooses by random a certain role for a videogame and load the name of the character as well as the name. (For reference alike a void() function from JAVA) For this bit I was using the following piece of code. ... choosenOP.Picture.LoadFromFile('thermite.png');                     //choosenOP is a TImage OP_name.Caption := 'THERMITE';                                      //OP_name is a TLabel ... So now I would like to have a little something that can work as a function or a procudure sothat i can "call" a function/procudure of the character's name instead of writing the same piece of code over and over again. I have therefore looked into functions a little bit and found it quite tricky hence I am not working with any variables here which could deliver a result. I am looking forward for any answers or tips on the topic. (Please excuse my english ) ---------------------------------------------------------------------- itzghee · 2021-03-04 23:35 Thank you for your answer already. Now I have come up with this bit of code. function thermite():char; begin      choosenOP.Picture.LoadFromFile('thermite.png');      OP_name.Caption := 'THERMITE'; end; Hence I know next to nothing about the creation of a function I'll have to bother your with my stupidity. For me it sounds as if I MUST assign a datatype to the function witch I did by choosing 'char'. As far as I understand it the datatype is more or less a placeholder as the function does not deliver a result of some sort. Is my assumption correct? However when i start to compile the code, I get error messages saying that the identifiers "choosenOP" and "OP_name" could not be found. What am I doing wrong there? ~itzghee ---------------------------------------------------------------------- lucamar · 2021-03-05 01:47 Supposing choosenOP is a TImage inside a form, what you should to do to reference it is to make thermite() a method of the form: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i interface type  TForm1: class(TForm)    choosenOP: TImage;    {... etc ...}  private    function thermite: char;  end;  {...etc...} implementation function TForm1.thermite: char;begin  {... whatever ...   Here, it's known what choosenOP is.}end; {rest of the unit} ---------------------------------------------------------------------- Martin_fr · 2021-03-05 03:35 Have a look at this: http://www.delphibasics.co.uk/Article.asp?Name=Routines There is also  (without return type) Quoteprocedure MakeATermite(); And you can make methods / OO http://www.delphibasics.co.uk/Article.asp?Name=OO So the code in your procedure/function can access other data. ---------------------------------------------------------------------- cdbc · 2021-03-05 09:55 Hi Try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { strcase is zero-based }function StrCase(const S: string;Elements: array of string): ptrint;var Idx: ptrint;begin  Result:= -1; { hence the result ~ ptrint }  { perform a linear search }  for Idx:= low(Elements) to high(Elements) do if S = Elements[Idx] then begin    Result:= Idx;    break;  end;end; { StrCase } procedure TForm1.GetActorFromName(const aName: string);begin  case StrCase(aName,['Thermite','Dragon','Wolverine']) of    0: begin         choosenOP.Picture.LoadFromFile('aName.png');                              //choosenOP is a TImage           OP_name.Caption := aName;                                               //OP_name is a TLabel      end;    1: begin           { do something }        end;  end; { case }end; OR:procedure TForm1.GetActorFromName(const aName: string);begin   choosenOP.Picture.LoadFromFile('aName.png');                      //choosenOP is a TImage   OP_name.Caption := aName;                                       //OP_name is a TLabelend; HTH Regards Benny ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-03-05 10:40 Hi All, I'm sorry if I'm putting my foot in my mouth, but IMO, what itzghee needs to implement is a Data Structure to contain his data and then all is good. So if you have your character data in either a record or a class, you can the use the data contained in said data structure to do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TCharacter = record    Name: String;    ImageFilename: String;  end;{ OR }  TCharater = class(TObject)  private    FName: String;    FImageFilename: String;  protected  public    property Name: String read FName write FName;    property ImageFilename: String read FImageFilename write FImageFilename;  published  end; {...} procedure TForm1.UpdateUIWithCharacterData(const ACharacter: TCharacter);begin  choosenOP.Picture.LoadFromFile(Acharacter.ImageFilename);  OP_name.Caption := ACharacter.Name;end;  And then have an Array of TCharacter or a TList or even a generics collection to accommodate for all the characters. Doesn't this make a lot more sense of a solution than what itzghee is asking? Cheers, Gus ---------------------------------------------------------------------- itzghee · 2021-03-05 11:12 Thank you for the many answers. I tried lucamar's option already and it worked just fine. But I'll have a proper look into each of therm soon. Until then, have a good one ~itzghee