如何解释方法调用?

发布于 2024-08-25 12:45:21 字数 314 浏览 18 评论 0原文

让我们考虑一个小方法:

int MyFunction(string foo, int bar)
{
  ...
}

和一些调用:

MyFunction("",0)
int x = MyFunction(foo1,bar1)

您如何向非技术人员解释这一点?有人有一个很好的比喻吗?

我多次尝试解释方法调用(或函数应用),但都失败了。在这里我似乎找不到合适的词语。

问候, forki

更新:对我来说解释参数如何传递/匹配很重要。

let's consider a small method:

int MyFunction(string foo, int bar)
{
  ...
}

and some calls:

MyFunction("",0)
int x = MyFunction(foo1,bar1)

How would you explain this to a non-technical persons? Has anybody a nice metaphor?

I tried to explain method calling (or function application) several times, but I failed. Seems I can't find the right words here.

Regards,
forki

UPDATE: It is important for me to explain how the parameters are passed / matched.

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

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

发布评论

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

评论(8

染墨丶若流云 2024-09-01 12:45:21

(高度非技术性的解决方案)

这就像下订单:

  • 调用方法=拨打正确的号码
  • 传递参数=提供您的详细信息
  • 该方法所做的就是工作
  • 获取返回值=获取您所订购的内容

(Highly non-technical solution)

It's like making an order:

  • Calling the method = dialing the right number
  • Passing the arguments = giving your details
  • the method does is job
  • Getting a return value = getting what you ordered
惜醉颜 2024-09-01 12:45:21

您可以看出函数是一个可以被其他对象调用的对象的过程。可以说“你”是一个具有“工作”功能的对象。您的“老板”是调用者对象。然后,您的老板可以叫您使用不同的类型(即参数)。

最后,你的“老板”可以要求“你”工作(“对此进行编码”)或工作(“检查电子邮件”)或工作(“完成截止日期”)等。

You could tell function is a process available into an object that could be called by other. Lets say "You" is an object with function "Work". Your "Boss" is the caller object. Your Boss then can call you to Work with different type (which is parameter).

In the end Your "Boss" can ask "You" to Work("encode this") or Work("check email") or Work("finish deadline"), etc.

禾厶谷欠 2024-09-01 12:45:21

委派任务怎么样?想象一下,你正在烤蛋糕,但面粉用完了。您无需自己购买面粉,只需向您的孩子发送购买面粉的说明即可。输入:金钱,输出:面粉。

How about delegating a task? Imagine you’re baking a cake and ran out of flour. Instead of buying some yourself you could just send your kid with instructions to buy flour. Input: money, output: flour.

忘你却要生生世世 2024-09-01 12:45:21

如果你不先了解
,就很难理解“方法调用”的概念
控制流程

一个简单的解释是,方法或例程是打包指令的构造
为了重用它们并使代码更具可读性。
暂时调用一个方法,将执行流程切换到该方法。

It's difficult to understand the "method call" concept if you don't understand first the
flow of control.

A simple explanation is that methods, or routines, is a construct for packeting instructions
in order to reuse them and make the code more readable.
Calling a method, temporarily, switches the execution flow to that method.

萌能量女王 2024-09-01 12:45:21
C:: do(a ,b)

您正在告诉 C 在给定条件 ab 的情况下do 某事。

C:: do(a ,b)

You are telling C to do something , given the condition a and b.

栖竹 2024-09-01 12:45:21

最好的方法可能是提出一个特定领域的示例,您正在向其解释的人可以与该示例相关。如果她在邮局工作,您应该描述“将包含此文本的信件发送给此收件人”功能,其中收件人是一个参数(包含地址),消息是文本内容的参数。

只要每个参数都有一个名称,参数顺序并不重要。试图解释为什么顺序在某些神秘的编程语言中很重要是徒劳的。

The best approach is probably to come up with a domain specific example which the person you are explaining to can relate to. If she is working with the post office, you should describe the function "send letter with this text to this recipient", where recipient is a parameter (containing the address) and message is the parameter for the textual content.

Parameter order is not important as long as you have a name for each parameter. Trying to explain why order is important in some arcane programming language is fruitless.

淡莣 2024-09-01 12:45:21

调用

  • 函数:要求软件执行 xxx 任务
  • 返回值类型函数:要求你的软件执行 xxx 任务,并告诉你操作的结果
  • 带参数调用函数:给定 X 是这个值,Y 是这个值,询问你的软件执行xxx任务(并告诉你操作的结果)

How about

  • Calling a function: Ask the software to perform xxx task
  • Returning value type function: Ask your software to perform xxx task and tell you the outcome of the operation
  • Calling a function with param: Given X is this value and Y is thisvalue, ask your software to perform xxx task (and tell you the outcome of the operation)
別甾虛僞 2024-09-01 12:45:21

将该系统视为办公桌前的出纳员。要调用某个功能,您需要填写一张表格,要求系统执行某项操作,然后将其交给出纳员。他们去做工作,然后递给你一张写有结果的纸。根据您希望系统执行的操作,您可以选择合适的形式。

MyMethod 的形式表示:

MYMETHOD REQUISITION FORM:
String _______
int    _______

这个类比可以以各种方式扩展。如果表单告诉您 String 和 int 的用途,不是很方便吗?这就是带有命名参数的语言的用武之地。

对于面向对象来说,整个系统不是只有一张桌子,而是每个对象都是自己的出纳员,你向他们提供了一份表格,为了完成工作,他们提供了更多的东西彼此之间来回形成。 ETC。

Think of the system as a teller at a desk. To call a function you fill in a form to ask the system to do something, hand it to the teller. They go off and do the work, then hand you back a piece of paper with the result written on it. Depending on what you want the system to do, you pick an appropriate form.

The form for MyMethod says:

MYMETHOD REQUISITION FORM:
String _______
int    _______

This analogy can be extended in all kinds of ways. Wouldn't it be handy if the form told you what the purpose of the String and int were? That's where languages with named parameters come in.

For OO, instead of having one desk for the whole system, each object is its own teller, you hand a form to them, and in order to get the job done, they hand a lot more forms back and forth between each other. Etc.

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