Pointer Tutorial Request

BlitzMax Forums/BlitzMax Tutorials/Pointer Tutorial Request

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I was fiddling around, trying to get function pointers to take arguments, and I think I've got it. (Yay!)
So, just in case it hasn't been mentioned here:
Function CallMyFunction(MyFunc(In:String) , VarToSend:String)
	Rem
	This function takes a function pointer as an argument.
	It expects that the function pointed to takes one argument , which is a String.
	
	To demonstrate the magic of function pointers , this function takes another argument , 
	which is the variable to pass along to the function it is sent in the MyFunct argument.
	End Rem
	Print "Calling a function passed as an argument..."
	MyFunc(VarToSend)
End Function

Function MyFunctionToPass(In:String = 1)
	Rem
	Prints the In variable which is passed as an argument
	End Rem
	Print In:String
End Function

Rem
Call the function CallMyFunction with a pointer to MyFunctionToPass as an argument and a regular String as an argument
End Rem
CallMyFunction(MyFunctionToPass,"This variable is passed via a function pointer!")


What happens here is that the program has to know what arguments the pointed to function takes! (Otherwise it, err, can't figure it out).
To do this, just have the necessary variables in the relevant places (where you have parentheses) as demonstrated above.


Edit:
Daniel explains it infinitely better (of course!) here:
http://www.blitzbasic.com/Community/posts.php?topic=48316#537481


Here is the above example, slightly modified to store MyFunctionToPass in a variable as well.
Global FunctionPointerVariable(In:String) 'This "dummy function" can be pointed to any function that takes one string as an argument



Function CallMyFunction(MyFunc(In:String) , VarToSend:String)
	Rem
	This function takes a function pointer as an argument.
	It expects that the function pointed to takes one argument , which is a String.
	
	To demonstrate the magic of function pointers , this function takes another argument , 
	which is the variable to pass along to the function it is sent in the MyFunct argument.
	End Rem
	Print "Calling a function passed as an argument..."
	MyFunc(VarToSend:String)
End Function

Function MyFunctionToPass(In:String)
	Rem
	Prints the In variable which is passed as an argument
	End Rem
	Print In:String
End Function


FunctionPointerVariable=MyFunctionToPass 'Set FunctionPointerVariable to point to MyFunctionToPass

Rem
Call the function CallMyFunction with a pointer to MyFunctionToPass as an argument and a regular String as an argument
End Rem
CallMyFunction(FunctionPointerVariable,"This variable is passed via a function pointer!")