解释方法和函数之间的区别? [英] Explian the difference between a method and a function ?

查看:87
本文介绍了解释方法和函数之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要详细了解方法和函数之间的区别.据我所知,没有什么区别,但是方法是预定义的,函数是用户定义的代码/行集.

预先感谢..

I need to know the difference between method and function in detail. As i know, there is no such difference but methods are predefined and functions are user defined set of codes/lines.

Thanks in advance ..

推荐答案

粗略地说:函数仅对传递的参数进行操作,而方法(由对象公开)则对对象的操作也是内部状态.
Roughly speaking: a function operates just on the passed parameters while a method, being exposed by an object, operates on the object''s internal state too.


答案是语义学...

后续问题是,您的老师期望什么答案?

但是正如上面回答的...通常,方法是与对象关联的函数.

在C ++中,它定义为类的成员函数.
The answer is Semantics...

The followup question is, what answer is your instructor expecting?

But as was answered above..., usually..., a method is a function associated with an object.

In C++ it''s defined as a member function of a class.


关于您可以肯定地说的唯一一件事是,方法与类相关联,而函数需要不可以.否则,这两个词在不同上下文中将用于表示相似的事物.

数学和函数编程的世界中,函数仅从其参数获取输入值,并且仅通过其返回值影响环境.从数学上讲,函数将值转换为其他值,而不是将运算符转换为其他函数的运算符.例如,+和-是函数(不是运算符),{x => x + 2}.该术语在函数式语言和APL语言家族中使用,但使用起来比较宽松,因此即使它读取或写入全局状态,您仍称其为函数".

常规过程编程(即C和非OO C ++)的世界中,任何子例程都称为函数".在C ++中,它扩展到语言的OO部分,例如在成员函数",纯虚函数"等中–并且在这种情况下,单词与方法"完全重叠. />
常规的面向对象编程中,即Java和C#,任何子例程都称为方法".在具有函数式方法经验的代码团队中,仅根据其参数操作并仅通过其结果写入的方法有时被称为函数"或纯函数";这些方法通常(尽管并非总是如此)将是静态的,因为它们不需要读取或写入实例状态.

Pascal和Delphi 中,无论关于状态的考虑如何,如果子例程不返回任何值,则将其称为过程",如果其返回一个值,则将其称为函数".该定义包含在语言本身中;您可以使用过程"或功能"一词来定义例程.

在具有闭包并且能够传递对子例程的引用(例如JavaScript)的动态类型语言中,可以以这种方式合理传递的例程通常称为函数",并且对其进行了扩展到所有其他子例程.
About the only thing that you can say for certain is that a method is associated with a class, whereas a function need not be. Otherwise, the two words are used to mean similar things in different contexts.

In the world of mathematics and functional programming, a function takes its input values from its parameters only, and affects the environment only by its return value. Mathematically, a function transforms values to other values, as opposed to an operator which transforms functions to other functions. For example, + and - are functions (not operators), as is { x => x + 2 }. This terminology is used in functional languages and in the APL family of languages, but loosely, so that you still call something a ''function'' even if it reads or writes global state.

In the world of conventional procedural programming, i.e. C and non-OO C++, any subroutine is called a ''function''. In C++ this is extended to the OO parts of the languages, as in ''member functions'', ''pure virtual functions'' etc – and in those cases the word has a complete overlap with ''method''.

In conventional object oriented programming, i.e. Java and C#, any subroutine is called a ''method''. In code teams which have experience of a functional approach, methods which operate only on their parameters and write only through their result are sometimes called ''functions'' or ''pure functions''; these methods will generally, though not always, be static, because they don''t need to read or write instance state.

In Pascal and Delphi, a subroutine is called a ''procedure'' if it returns no value, and a ''function'' if it returns one, regardless of considerations about state. This definition is included in the language itself; you define the routine with the word ''procedure'' or ''function''.

In dynamically typed languages with closures and the ability to pass references to subroutines around (e.g. JavaScript), routines which can reasonably be passed around in this way are often called ''functions'', and that gets extended to all other subroutines.


这篇关于解释方法和函数之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆