I read a lot of language source code, suddenly found an interesting phenomenon, for
function
This keyword, many languages are designed, and many languages are not. What is the basis of this consideration?
And after my statistics, function
most of the keywords are scripting languages, such as, php
and js
so on, and the compiler language, such as, and java
c
so on, often do not use this keyword, what is the intrinsic connection?
Reply content:
I read a lot of language source code, suddenly found an interesting phenomenon, for function
this keyword, many languages have been designed, and many languages are not. What is the basis of this consideration?
And after my statistics, function
most of the keywords are scripting languages, such as, php
and js
so on, and the compiler language, such as, and java
c
so on, often do not use this keyword, what is the intrinsic connection?
It's a great question.
I have seen a lot of language source code, but also interested in different language syntax, so simply talk about this problem.
The first appearance is the LISP language. LISP is famous for its high-quality language. He is a functional programming language, and all statements are functions, so there is no need for the keyword function.
Subsequently the Fortran language, the formula translated meaning for MulaTranslation. Since it is the Formula translation language, the mathematical formula F (x) = a + B is called in Fortran x = a + b
.
Use function on how functions are defined: (Code from Google)
function add(a, b)implicit nonereal::a, b !声明函数参数类型,这是必需的real::add !声明函数返回值类型,这是必需的add = a + b !返回值的表达式returnend
COBOL language. The purpose of COBOL design is different from Fortran, even the assignment statement is not, and there is no concept of function, instead of PROCEDURE division (process).
Followed by Basic:Beginner ' s All-purpose symbolic Instruction COde. Basic is a synthesizer, not just a function, but a process. Functions use function, and procedures use sub. (Copy Fortran and COBOL?) )
The C language has changed this situation because of a inventive design concept in C: How functions are invoked and how they are declared. In other words, if we call the function like this
sum = f(a, b);
So, let's just say:
double f(a, b) /* 旧式语法 */ double a; double b;{}double f(double a, double b) // 新式语法{}
The following C + + Java C # goes through the route of C.
But the invocation and declaration are consistent if the function call needs to be declared before it is called. In the C language, not just functions, even data type definitions need to be declared.
However, in many dynamic languages, most of the data and functions do not need to be declared in advance, and many functions are called only once, and then declared once before the function call, the function will not be used again, this will appear cumbersome. At this point, you need to separate the function definitions and function calls in the syntax.
Another reason is that C is a compiled language, and his syntax is to better improve the speed of compilation and the performance of the code after compilation, such as the principles of data and code separation required by the early C and Pascal compilers.
Today's major dynamic programming languages no longer need to be compiled, much less precompiled.
function
is a token, indicating that a function is being declared, this keyword is not too much related to a compiler or an interpreter, most of which use this keyword is dynamic type language, and for a static type language such as C/java, Specifying the return value and the parameter list makes it clear that function declarations and function calls are distinguished, because the function itself and the arguments are all typed, but the language of the dynamic type does not have a type representation, and if you do not add a specific keyword, the syntax is not very good for distinguishing the function declaration and function calls , such as the C language:
int f(int n)
is a declaration, f(n)
is a call, but in JavaScript, if you don't add function
this keyword, how do you explain f(n)
exactly what it is?
Ask a question, what is the correct meaning of "hit"?
No one should give the right answer, because you don't know if it's being used as a verb or a quantifier, or something else.
So we need to analyze the meaning of the word from the context of "a dozen" "thugs" and "open".
As for the function of the keyword need not appear, depending on the language design.
Qualified programmers know the code text, after compiling, eventually become the machine code or the bytecode of the VM, so it doesn't matter if you write or not write. Keywords that are convenient for the function
function
pre-compilation analysis phase can be correctly (and implemented less laborious) through syntax and semantic analysis.
For example, before 5.4, PHP does not support the abc()[1]
return value of the function of the array index (the premise is to ensure that the return of the ideal length of the array), the reason for the bird brother (can't remember, it is estimated that he) is the resolution of the code is messy, more difficult to achieve, to 5.4, finally someone willing to do this dirty work, A problem. If one day someone optimizes the parsing of the function keyword, then there may be a PHP that doesn't need a function
keyword.
To raise a chestnut, because the grammar is hated by many people bash
The function is defined as follows
[ function ] name () compound-command [ redirections ]
When defining a function, the function
keyword is optional, but, no function
time, you need to write the()
So, it means that Bash uses function or () to locate this is a functional declaration, not a statement.
More accurate answers need to be analyzed from the specific implementation of language analysis, and also to determine the existence and abc(){}
conflict of the characteristics of the existence, it will not continue to go deeper
What does it mean to think but not to learn? This is a living example.
Have you ever seen python that defines a function with Def? Have you ever seen rust that defines a function with FN? Have you ever seen Swift that defines a function with Func? Have you ever seen a bash function that can add function or not function? Have you ever seen a Scala that can define functions using either Def or Val?