Advantages of passing a function as a parameter
Asked Answered
J

7

6

Just studying for an exam and I can't find the answer to this question in our notes. Any help would be great.

Many languages permit subroutines/functions to be passed as parameters. List two advantages provided by this, and motivate each advantage with a clear explanatory example (this need not be code of pseudo-code).

Jamestown answered 12/12, 2010 at 0:50 Comment(1)
did you get the answer? It's very important to answer this question properly. I am one of many people who are still confused about why this structure is very popular for developers, passing function as a param.Immodest
U
3

Think you are a manager of a charming singer ( in computer life : a program) , in the following two ways to start your morning.

Situation 1: You have to tell some underling to do the following a) get breakfast for the star and take great care with the kind of croissants she likes, remember she is very upset when she wakes up etc.. b) Put all cables on the stages using such and such power this lights but not that one , these colors ...

Situation 2: Ask your underling: Ask the majordomo to give our star her usual breakfast. Then ask the crew to take care of the stage for the usual songs.

Situation One is wrong from a computer point of view, it is typical of a quick and dirty way of doing. Yes you have the guy at hand but he is doing all the errands and handling several responsibilities of different types so he may be confused and moreover the order is long and detailed.

In situation two you are delegating, this handles the complexity , the order are short, we know who is doing the which jobs so we won't find a pink huge light bulb in the tea cup of the star (you think it is a joke but that is exactly what a bug is) . In a few words complexity is partitioned in a meaningful way.

If you do not see why situation two is like calling functions here is a pseudo code.

extern FUNCTION majordomo( client , service , options ) ;
extern FUNCTION crew ( task , options ) ;

FUNCTION startMorning() BEGIN

call ( underling, majordomo( for_ourstar, usual_breakfast, she_is_picky));
call ( underling, crew(usual cables, bright lights));
END

Unidirectional answered 12/12, 2010 at 1:14 Comment(0)
L
2

One of the things passing an 'action' function to a method brings is the ability to perform an action against a collection without exposing the internals of that collection.

A typical use is, iterating over a private collection calling the passed function on each item.

Another is as a callback method.

Landgraviate answered 12/12, 2010 at 0:58 Comment(3)
Such as, for example, using C++'s std::transform to apply the C standard library function toupper() to an entire string at the same time.Lugar
@mitch It's very important to answer this question properly. I am one of many people who are still confused about why this structure is very popular for developers, passing function as paramImmodest
@MitchWheat If my bio doesn't convey the intended message, I'm open to making changes. Regarding actions in event-driven languages and callback functions, which involve passing functions as arguments, I understand their concepts. However, I'm aware that achieving actions can be done without passing functions as arguments, by creating a separate regular function. So, why do developers still prefer the inner structure?Immodest
L
1

The primary advantage is that if the function being called calls another function, you can modify the behavior of the function being called by specifying which other function is called.

Sorry, beyond that, you'll need to do your own homework.

Leatherworker answered 12/12, 2010 at 0:54 Comment(1)
It's very important to answer this question properly. I am one of many people who are still confused about why this structure is very popular for developers, passing a function as a param.Immodest
M
1

Applying a certain action to all members of a collection. (ie. square every number in it).

Mythos answered 12/12, 2010 at 0:54 Comment(1)
It's very important to answer this question properly. I am one of many people who are still confused about why this structure is very popular for developers, passing a function as param.Immodest
L
1

Consider a function that sorts an array of objects based on comparison sorting. Such a function needs a way to compare 2 objects and tell which is greater than the other. You can pass such a general sort function a pointer to the array and a pointer to the function that helps it compare any 2 objects.

See STL's sort for an example.

Leukoderma answered 12/12, 2010 at 1:0 Comment(0)
C
0

I simple answer would be the function passed might get used as a callback function.

When the function completes it's job, it would call the callback function with or w/o arguments.

Coffeepot answered 12/12, 2010 at 0:53 Comment(0)
R
0

If you wanna perform a certain operation when it's needed then it's a good practice to pass a function as an argument.

Rosenda answered 18/4, 2023 at 8:41 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Bareheaded

© 2022 - 2024 — McMap. All rights reserved.