将函数作为参数传递的优点

发布于 2024-10-07 01:33:09 字数 147 浏览 0 评论 0原文

刚刚准备考试,我在笔记中找不到这个问题的答案。任何帮助都会很棒。

许多语言允许子例程/函数作为 参数。列出由此提供的两个优点,并激励每个优点 具有清晰的解释性示例的优点(这不必是代码 伪代码)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

樱娆 2024-10-14 01:33:09

想象你是一位迷人歌手的经理(在计算机生活中:一个程序),可以通过以下两种方式开始你的早晨。

情况1:你必须告诉一些下属执行以下操作a)为明星准备早餐,并特别注意她喜欢吃的羊角面包,记住她很喜欢吃羊角面包。当她醒来时心烦意乱等..b)将所有电缆放在舞台上,使用这样那样的电源,这个灯亮,但不是那个灯,这些颜色......

情况2:询问你的下属:请主管给我们的明星提供她平常的早餐。然后请工作人员照管舞台,播放平常的歌曲。

从计算机的角度来看,情况一是错误的,它是典型的快速而肮脏的做法。是的,您手头上有这个人,但他正在做所有的差事并处理不同类型的多种职责,因此他可能会感到困惑,而且订单又长又详细。

在你委托的情况二中,这处理了复杂性,订单很短,我们知道谁在做哪些工作,所以我们不会在星星的茶杯里找到一个粉红色的巨大灯泡(你认为这是一个笑话但这正是错误的本质)。
简而言之,复杂性以有意义的方式进行划分。

如果您不明白为什么情况二就像调用函数,这里是伪代码。

extern FUNCTION Majordomo( 客户端 , 服务 , 选项 ) ;
extern FUNCTION 人员(任务、选项);

FUNCTION startMorning() BEGIN

call (underling, Majordomo(for_ourstar,usual_breakfast,she_is_picky));
呼叫(下属,船员(普通电缆,明亮的灯光));
结尾

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

没有你我更好 2024-10-14 01:33:09

将“操作”函数传递给方法带来的好处之一是能够对集合执行操作,而无需暴露该集合的内部结构。

典型的用途是迭代私有集合,在每个项目上调用传递的函数。

另一种是作为回调方法。

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.

池木 2024-10-14 01:33:09

主要优点是,如果被调用的函数调用另一个函数,您可以通过指定调用哪个其他函数来修改被调用函数的行为。

抱歉,除此之外,您还需要做自己的作业。

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.

北城孤痞 2024-10-14 01:33:09

将特定操作应用于集合的所有成员。 (即对其中的每个数字进行平方)。

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

沒落の蓅哖 2024-10-14 01:33:09

考虑一个基于比较排序对对象数组进行排序的函数。这样的函数需要一种方法来比较两个对象并判断哪个对象大于另一个对象。您可以向这样的通用排序函数传递一个指向数组的指针和一个指向帮助其比较任意两个对象的函数的指针。

请参阅 STL 的排序示例。

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.

温柔戏命师 2024-10-14 01:33:09

我简单的回答是传递的函数可能会用作回调函数。

当函数完成其工作时,它将调用带或不带参数的回调函数。

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.

怀中猫帐中妖 2024-10-14 01:33:09

如果您想在需要时执行某个操作,那么将函数作为参数传递是一个很好的做法。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文