函数和过程之间的区别?

发布于 2024-10-22 07:42:47 字数 114 浏览 0 评论 0原文

我有一个疑问 我知道函数和过程之间的主要区别是 函数强制返回一个值,而过程可能返回值,也可能不返回值。 但是当我们使用 void 类型的函数时,它不会返回任何内容。 你们能澄清我的疑问吗?

I had a doubt
I know that main difference between a function and procedure is
The function compulsory returns a value where as a procedure may or may not returns value.
But when we use a function of type void it returns nothing.
Can u people please clarify my doubt.

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

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

发布评论

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

评论(3

伊面 2024-10-29 07:42:47

传统上,返回值的过程被称为函数(见下文),但是,许多现代语言完全放弃了术语“过程”,而更喜欢对所有命名的代码块使用术语“函数”。

请阅读 Suite101:过程、子例程还是函数?:编程术语 101 - 了解过程、子例程和函数的方法和定义的差异。 http://www.suite101.com/content/procedure --子程序或函数--a8208#ixzz1GqkE7HjE

Traditionally, a procedure returning a value has been called a function (see below), however, many modern languages dispense with the term procedure altogether, preferring to use the term function for all named code blocks.

Read more at Suite101: Procedure, subroutine or function?: Programming terminology 101 - a look at the differences in approach and definition of procedures, subroutines and functions. http://www.suite101.com/content/procedure--subroutine-or-function--a8208#ixzz1GqkE7HjE

怪我入戏太深 2024-10-29 07:42:47

在C及其派生语言中,很少使用术语“过程”。 C 的函数有些返回值,有些则不返回值。我认为这是 C 传统的产物,在 ANSI C 中引入 void 之前,没有办法返回值。默认情况下,函数返回一个 int ,您可以忽略它(仍然可以),如果没有指定显式返回值,它可能是一些随机数。

在 Pascal 语言家族中,区别是明确的,函数返回值,而过程则不返回值。每种情况都使用不同的关键字进行定义。 Visual Basic 还区分函数和子例程(?)。

既然我们谈论的是 Objective-C,那么还有一些进一步的问题会让您感到困惑。与类或对象关联的函数称为“方法”(分别为类方法和实例方法)。

另外,如果我们比较迂腐的话,你不会调用 Objective-C 方法,而是通过向对象发送消息来调用它们。这种区别实际上非常重要,因为消息名称(又名“选择器”)不一定总是引用相同的方法,它可以在运行时更改。这与 Java 和 C++ 等语言有根本的不同,其中特定类的特定方法名称实际上只是构成方法主体的代码块地址的符号名称。

In C and its derivatives, the term "procedure" is rarely used. C has functions some of which return a value and some of which don't. I think this is an artefact of C's heritage where before the introduction of void in ANSI C, there was no way to not return a value. By default functions returned an int which you could ignore (can still) and might be some random number if no explicit return value was specified.

In the Pascal language family, the difference is explicit, functions return a value and procedures don't. A different keyword is used in each case for the definition. Visual Basic also differentiates with functions and subroutines(?).

Since we are talking about Objective-C, there are some further issues to confuse you. Functions associated with a class or object are known as "methods" (class methods and instance methods respectively).

Also, if we are being pedantic, you don't call Objective-C methods, you invoke them by sending a message to the object. The distinction is actually quite important because the message name (aka "selector") does not necessarily always refer to the same method, it can be changed at run time. This is fundamentally different to languages like Java and C++ where a particular method name for a particular class is really just a symbolic name for the address of the block of code constituting the body of the method.

も让我眼熟你 2024-10-29 07:42:47

根据编程语言的不同,区别可能不那么明显。我们以保守的语言来说,Pascal

  • procedure确实没有返回值。它用于没有返回值或有多个返回值的操作。在后一种情况下,多个参数(返回参数或输出参数)通过引用传递(使用 var 关键字),并且它们的值直接从过程内部修改。 (请注意,根据具体情况,后一种情况可能不被视为良好实践)。
  • function 有一个返回值,通常我们不希望它改变任何参数的值(这些参数可以按值传递,或者通过 const 传递) > 关键字)。可以通过将多个返回值捆绑到一条记录中来返回它们。

CJava 在语法上不区分,因此返回类型为 void 的函数可以被认为是一个过程。 Scala 通过方法头和方法体之间存在等号来区分它们。

通常,无论实际语言如何调用其构造,我们理想希望

  • 函数接受参数,不会修改任何状态(例如改变参数、全局变量或将用户的信息打印到控制台),并返回计算结果。
  • 过程接受参数,执行可能产生副作用的操作(写入数据库,打印到控制台,可能会改变变量),但希望不会改变任何参数。

然而,在实践中,根据具体情况,可以观察到这些期望的混合。我认为坚持这些指导方针会有所帮助。

Depending on the programming language, the distinction may be not so clear. Let's take a conservative language, Pascal:

  • procedure indeed has no return value. It is used for operations which do not have a return value, or have multiple return values. In the latter case, multiple arguments (the return-arguments or output-arguments) are passed by reference (using the var keyword) and their values are directly modified from inside the procedure. (Note that this latter case may not be considered good practice, depending on the circumstances).
  • function has a single return value, and usually we do not expect it to change the value of any of its arguments (which arguments may then be passed by value, or via the const keyword). Multiple return values may be returned by bundling them into a record.

C or Java does not distinguish syntactically, so a function of return type void can be thought of as a procedure. Scala distinguished between them by the presence of an equals sign between the method head and method body.

Generally, no matter how an actual language calls its construct, we would ideally expect that

  • A function takes arguments, doesn't modify any state (like mutating arguments, global variables, or printing info for the user to the console), and returns the result of computation.
  • A procedure takes arguments, performs operations which can have side-effects (writing to a database, printing to the console, maybe mutating variables), but hopefully doesn't mutate any arguments.

In practice however, depending on the situation, blends of these expectations can be observed. Sticking to these guidelines helps I think.

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