Recent

Author Topic: Creating a function / procedure alike JAVA's void()  (Read 831 times)

itzghee

  • Newbie
  • Posts: 3
Creating a function / procedure alike JAVA's void()
« on: March 05, 2021, 12:06:04 am »
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 :))


jamie

  • Hero Member
  • *****
  • Posts: 4341
Re: Creating a function / procedure alike JAVA's void()
« Reply #1 on: March 05, 2021, 12:13:52 am »
The default settings of FPC should allow you to use a function as a Procedure, thereby ignoring the return results if you don't need it...

So, create a Function that can be used as a function when needed but use it as a procedure anytime when you don't need the return value.

Function MyName(…..):TheType;


MyName(……)

or

Some_TheTYpe := MyName(….)

Will work either way..
The only true wisdom is knowing you know nothing

itzghee

  • Newbie
  • Posts: 3
Re: Creating a function / procedure alike JAVA's void()
« Reply #2 on: March 05, 2021, 12:35:44 am »
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

  • Hero Member
  • *****
  • Posts: 3763
Re: Creating a function / procedure alike JAVA's void()
« Reply #3 on: March 05, 2021, 02:47:52 am »
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][+][-]
  1. interface
  2.  
  3. type
  4.   TForm1: class(TForm)
  5.     choosenOP: TImage;
  6.     {... etc ...}
  7.   private
  8.     function thermite: char;
  9.   end;
  10.  
  11. {...etc...}
  12.  
  13. implementation
  14.  
  15. function TForm1.thermite: char;
  16. begin
  17.   {... whatever ...
  18.    Here, it's known what choosenOP is.}
  19. end;
  20.  
  21. {rest of the unit}
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.

jamie

  • Hero Member
  • *****
  • Posts: 4341
Re: Creating a function / procedure alike JAVA's void()
« Reply #4 on: March 05, 2021, 03:41:58 am »
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

Scope rules of identifiers are mostly looked at like a family history tree, that is, the compiler will automatically see identifiers that way, it can see them currently local and backwards but if does not see them forward, at least not with out some help.


 In your example, you assume you should be able to see those identifiers but you didn't because they most likely live inside a class and your function is outside that class

 Its like being in a house with a one way mirror, you can look out like you could if you were inside a class but those outside the class can not look in, unless there is some help to let you in.

 
Getting to the function return types..


 In the body of the function, setting the value to your returning type can be done two ways...

 RESULT := ?
  or
 Name_OF_Your_FUCNTION := ?


etc..

hope that gives you some more insight,,.,.

The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6999
  • Debugger - SynEdit - and more
    • wiki
Re: Creating a function / procedure alike JAVA's void()
« Reply #5 on: March 05, 2021, 04:35:54 am »
Have a look at this: http://www.delphibasics.co.uk/Article.asp?Name=Routines
There is also  (without return type)
Quote
procedure 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

  • Sr. Member
  • ****
  • Posts: 353
    • http://www.cdbc.dk
Re: Creating a function / procedure alike JAVA's void()
« Reply #6 on: March 05, 2021, 10:55:14 am »
Hi
Try this:
Code: Pascal  [Select][+][-]
  1. { strcase is zero-based }
  2. function StrCase(const S: string;Elements: array of string): ptrint;
  3. var Idx: ptrint;
  4. begin
  5.   Result:= -1; { hence the result ~ ptrint }
  6.   { perform a linear search }
  7.   for Idx:= low(Elements) to high(Elements) do if S = Elements[Idx] then begin
  8.     Result:= Idx;
  9.     break;
  10.   end;
  11. end; { StrCase }
  12.  
  13. procedure TForm1.GetActorFromName(const aName: string);
  14. begin
  15.   case StrCase(aName,['Thermite','Dragon','Wolverine']) of
  16.     0: begin
  17.            choosenOP.Picture.LoadFromFile('aName.png');                    
  18.            //choosenOP is a TImage
  19.            OP_name.Caption := aName;                                      
  20.            //OP_name is a TLabel
  21.         end;
  22.     1: begin
  23.            { do something }
  24.         end;
  25.   end; { case }
  26. end;
  27.  
  28. OR:
  29. procedure TForm1.GetActorFromName(const aName: string);
  30. begin
  31.    choosenOP.Picture.LoadFromFile('aName.png');                    
  32.    //choosenOP is a TImage
  33.    OP_name.Caption := aName;                                      
  34.    //OP_name is a TLabel
  35. end;
  36.  
HTH
Regards Benny
If it ain't broke, don't fix it ;)

Gustavo 'Gus' Carreno

  • Sr. Member
  • ****
  • Posts: 413
  • Professional amateur ;-P
Re: Creating a function / procedure alike JAVA's void()
« Reply #7 on: March 05, 2021, 11:40:24 am »
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][+][-]
  1. type
  2.   TCharacter = record
  3.     Name: String;
  4.     ImageFilename: String;
  5.   end;
  6. { OR }
  7.   TCharater = class(TObject)
  8.   private
  9.     FName: String;
  10.     FImageFilename: String;
  11.   protected
  12.   public
  13.     property Name: String read FName write FName;
  14.     property ImageFilename: String read FImageFilename write FImageFilename;
  15.   published
  16.   end;
  17.  
  18. {...}
  19.  
  20. procedure TForm1.UpdateUIWithCharacterData(const ACharacter: TCharacter);
  21. begin
  22.   choosenOP.Picture.LoadFromFile(Acharacter.ImageFilename);
  23.   OP_name.Caption := ACharacter.Name;
  24. end;
  25.  

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
« Last Edit: March 05, 2021, 11:43:05 am by gcarreno »
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

itzghee

  • Newbie
  • Posts: 3
Re: Creating a function / procedure alike JAVA's void()
« Reply #8 on: March 05, 2021, 12:12:56 pm »
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

 

TinyPortal © 2005-2018