javascript - 为何有的语言要声明function关键字,有的却不需要呢?
我看了很多语言源代码,突然发现一个有趣的现象,对于function
这个关键字,很多语言设计出来了,而很多语言却没有。这是基于一种什么考虑呢?
而且经过我的统计,出现function
关键字的大多数是脚本语言,比如php
,js
等等,而编译式语言比如java
,c
等等,往往却没有用到这个关键字,这其中有什么内在联系吗?
回复内容:
我看了很多语言源代码,突然发现一个有趣的现象,对于function
这个关键字,很多语言设计出来了,而很多语言却没有。这是基于一种什么考虑呢?
而且经过我的统计,出现function
关键字的大多数是脚本语言,比如php
,js
等等,而编译式语言比如java
,c
等等,往往却没有用到这个关键字,这其中有什么内在联系吗?
很赞的一个问题。
我也看过很多语言源代码,也对不同的语言的语法感兴趣,所以简单的聊聊这个问题。
首先出场的是 LISP 语言。 LISP 语言以逼格高闻名于世。他属于函数式编程语言,所有的语句都是函数,因此不需要 function 这个关键词。
随后的 Fortran 语言,公式翻译的意思 FormulaTranslation。既然是公式翻译语言,因此 数学公式 f(x) = a + b 在 Fortran 的调用就是 x = a + b
。
在定义函数的方式上,使用 function:(代码来自google)
function add(a, b) implicit none real::a, b !声明函数参数类型,这是必需的 real::add !声明函数返回值类型,这是必需的 add = a + b !返回值的表达式 return end
COBOL语言。COBOL 设计的目的和 Fortran 不同,甚至连赋值语句都没有,而且根本就没有函数的概念,取而代之的是 PROCEDURE DIVISION(过程)。
随后就是 Basic:Beginner's All-purpose Symbolic Instruction Code。Basic 可谓是集大成者,不仅仅有函数,还有过程。函数使用 function, 过程使用 sub。(抄袭 fortran 和 cobol?)
C语言改变了这种局面,原因是C语言的一个独辟蹊径的设计理念:函数如何调用,就如何声明。也就是说,我们如果这样调用函数
<code>sum = f(a, b); </code>
那么,我们就这么声明:
<code>double f(a, b) /* 旧式语法 */ double a; double b; {} double f(double a, double b) // 新式语法 {} </code>
随后的 C++ Java C# 走的是C的路线。
但是调用和声明一致的前提是,函数调用之前需要声明。在 C 语言中,不仅仅是函数,即使数据类型定义也需要声明。
不过在很多动态语言中,大部分数据和函数都不需要提前声明,而且有很多函数只调用一次,那么在函数调用之前声明一次,以后这个函数就再不使用了,这样就会显得繁琐。这时,就需要在语法上将函数定义和函数调用分别出来。
还有一个原因,C是种编译性的语言,他的语法是为了更好的提升编译速度和编译完成后的代码性能,比如早期C和pascal编译器要求的数据和代码分离的原则。
而现在的主要动态编程语言都不再需要编译,更无需预编译。
function
是个标记,表明正在声明的是一个函数,这个关键字和编译型或解释型没什么太大关系,使用这个关键字的大多是动态类型语言,对于C/JAVA这一类静态类型语言,标明返回值和参数列表就可以明确的区分函数声明和函数调用,因为函数本身和参数都标明了类型,但动态类型的语言没有类型表示,如果不加入一个特定的关键字,从语法上就不太好区分函数声明和函数调用,比如C语言中:int f(int n)
是一个声明,f(n)
是一个调用,但在JavaScript中,如果你不加function
这个关键字,你该如何解释f(n)
到底是什么呢?
问一个问题, 请问 "打" 的正确含义是什么?
任何人都不应该给出正确的答案, 因为你不知道它被作为动词还是量词, 或者其他意思.
所以我们需要从 "一打" "打手" "打开" 这样的上下文中分析该词的含义.
至于 function 这个关键词需不需要出现, 取决于语言的设计.
合格的程序员都知道代码文本, 经过编译之后, 最终成为机器码或者 vm 用的字节码, 所以跟你写不写 function
没有关系. function
之类的关键字方便在编译前的分析阶段, 能够正确(并且实现上不那么费力)地通过语法和语义分析.
举个例子, 在 5.4 之前, php 不支持 abc()[1]
这种对函数返回值取数组索引(前提是得保证返回了理想长度的数组), 鸟哥给的原因(记不清了,估计是他)是解析代码比较乱, 比较难实现, 到 5.4, 总算有人愿意干这个脏活了, 解决了这个问题. 如果哪天谁把 function 这个关键字的解析工作优化了, 那么可能就会出现一个不需要 function
关键字的 php.
举个栗子, 因为语法被很多人讨厌的 bash
函数定义如下
<code> [ function ] name () compound-command [ redirections ] </code>
定义函数的的时候, function
关键字是可选的, 但, 没有 function
的时候, 需要写上 ()
所以, 意味着 bash 通过 function 或者 () 来定位这是一个函数声明, 而不是语句.
更加准确的回答, 需要从语言的解析具体实现进行分析, 另外还要确定是否存在和 abc(){}
冲突的特性存在, 就不继续深入了
什么叫思而不学则殆?这就是活生生的例子。
你没见过用def定义函数的python吗?没见过用fn定义函数的rust吗?没见过用func定义函数的swift吗?没见过可以加function也可以不加function的bash函数吗?没见过既可以用def也可以用val定义函数的scala吗?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.
