How to Pass a Function/Procedure to a Function/Procedure

Just starting out? Need help? Post your questions and find answers here.
ppjm99
User
User
Posts: 23
Joined: Mon Jun 02, 2003 7:39 pm
Location: Canada

How to Pass a Function/Procedure to a Function/Procedure

Post by ppjm99 »

This is a rework of other stuff I have very gratefully found here in the forums but perhaps offers a different perspective, this is a key feature for reusability of code in my mind. I hope it helps someone. The code:

Code: Select all

Global sReturn$
Global nReturn

Procedure Sum(ArgList$)

  nReturn = 0

  Arg = 1
  Arg$ = StringField(Arglist$,Arg,",")
  While Arg$ <> ""

    nReturn = nReturn + Val(Arg$) 

    Arg + 1
    Arg$ = StringField(Arglist$,Arg,",")
  
  Wend
  
EndProcedure

Procedure Average(ArgList$)

  nReturn = 0

  Arg = 1
  Arg$ = StringField(Arglist$,Arg,",")
  While Arg$ <> ""

    nReturn = nReturn + Val(Arg$) 

    Arg + 1
    Arg$ = StringField(Arglist$,Arg,",")
  
  Wend
  
  nReturn / (Arg - 1)
  
EndProcedure

Procedure Concatenate(ArgList$)

  sReturn$ = ""

  Arg = 1
  Arg$ = StringField(Arglist$,Arg,",")
  While Arg$ <> ""

    sReturn$ + Arg$

    Arg + 1
    Arg$ = StringField(Arglist$,Arg,",")
  
  Wend
  
EndProcedure

Procedure ProcessArgList( ProcAddress, ArgList$ )

  *ProcPointer = ProcAddress
  CallFunctionFast(*ProcPointer, ArgList$)
  
EndProcedure

ProcessArgList( @Sum(), "1,2,3,4" )
Debug nReturn

ProcessArgList( @Average(), "4,6" )
Debug nReturn

ProcessArgList( @Concatenate(), "J,O,H,N, ,D,O,E" )
Debug sReturn$
Another example of the usefulness of this type of code would be with a general purpose directory walker. Something Like:

Code: Select all

DirWalk(Root$,@FolderChangeFunc,@FileChangeFunc,@ProgressFunc)
Be kind I am just learning PB and I love it. ;)
ppjm99
User
User
Posts: 23
Joined: Mon Jun 02, 2003 7:39 pm
Location: Canada

How do I retain my indentation when posting code samples?

Post by ppjm99 »

Does anybody know how to retain indentation when posting code samples?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: How do I retain my indentation when posting code samples

Post by traumatic »

simply use the code-tags ()
Good programmers don't comment their code. It was hard to write, should be hard to read.
Post Reply