Benefits of passing a function as a parameter

Just study for the exam, and I can’t find the answer to this question in our notes. Any help would be great.

Many languages ​​allow the execution of routines / functions as parameters. List the two benefits provided by this, and motivate each advantage with a clear explanatory example (this does not have to be a pseudo-code code).

+5
source share
6 answers

The main advantage is that if the called function calls another function, you can change the behavior of the called function by specifying which other function to call.

Sorry, in addition, you will need to do your homework.

+2

, "" , - , .

- , .

.

+1

, ( : ) , .

1. , , a), , , , .. b) , ​​ ​​, , , ...

2: : . .

, . , , , , , , .

, , , , , , ( , , , ). .

, , , .

extern FUNCTION majordomo (, , )

extern FUNCTION (, );

startMorning() BEGIN

call (underling, majordomo (for_ourstar, normal_breakfast, she_is_picky))

(, ( , ))

END

+1

, .

, .

0

. (.. ).

0
source

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

See STL sorting for an example.

0
source

All Articles