What is the difference between callback and recursion function?

beer9
beer9 used Ask the Experts™
on
I read about them in wikipedia but not getting the clear picture. Could someone please provide me some practical example in perl to understand it better. Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
A recursive function is a function that calls itself from within itself. There is no need to pass the function as a callback; you can simply call the function. For example:

#!/usr/bin/perl

print factorial(5);
print "\n";

sub factorial
{
	my ($x) = @_;

	if ($x == 0) { return 1; }
	else { return $x * factorial($x - 1); }		# Recursive call is here because we call "factorial" within the function itself
}

Open in new window



A callback is a function that is passed as a parameter to another function. Inside the callee, you invoke the callback via its pointer (i.e. the parameter variable).

#!/usr/bin/perl

print callbackExample(factorial, 5);                        # pass the callback function as a parameter.
print "\n";

sub callbackExample
{
	my ($callback, $argument) = @_;
	
	$callback->($argument);
}

sub factorial
{
	my ($x) = @_;

	if ($x == 0) { return 1; }
	else { return $x * factorial($x - 1); }		# Recursive call is here because we call "factorial" within the function itself
}

Open in new window

When passing a sub, you should always pass it as a reference.

All Perl scripts should include the warnings and strcit pragmas which would catch problems such as not passing the sub as a reference.

With warnings enabled, you'd get:
Unquoted string "factorial" may clash with future reserved word at kaufmed.pl line 3.

With strict enabled, the script won't even compile.
Bareword "factorial" not allowed while "strict subs" in use at D:\perl\EE\kaufmed.pl line 4.
Execution of D:\perl\EE\kaufmed.pl aborted due to compilation errors.

change:
print callbackExample(factorial, 5);

Open in new window

to:
print callbackExample(\&factorial, 5);

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
@FishMonger
When passing a sub, you should always pass it as a reference.

All Perl scripts should include the warnings and strcit pragmas which would catch problems such as not passing the sub as a reference.
Thanks for the info. I am by no means a Perl guru, or even an intermediate. My post was strictly for demonstration of the difference between the two concepts. Good to know the real experts are keeping me honest  = )

Author

Commented:
Thank you! :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial