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吗?

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

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

SublimeText3 English version
Recommended: Win version, supports code prompts!
