Javascript-why do some languages need to declare the function keyword, but some do not?

Source: Internet
Author: User
I read a lot of language source code and suddenly found an interesting phenomenon. For the function keyword, many languages have been designed, but many languages do not. What is this based on? According to my statistics, most of the function keywords are scripting languages, such as php,... I 've read a lot of language source code and suddenly found an interesting phenomenon. functionThis keyword is designed in many languages, but not in many languages. What is this based on?

And after my statisticsfunctionMost of the keywords are script languages, suchphp,jsAnd so on.java,cAnd so on, but this keyword is often not used. Is there any internal link in it?

Reply content:

After reading the source code of many languages, I suddenly found an interesting phenomenon.functionThis keyword is designed in many languages, but not in many languages. What is this based on?

And after my statisticsfunctionMost of the keywords are script languages, suchphp,jsAnd so on.java,cAnd so on, but this keyword is often not used. Is there any internal link in it?

A very good question.

I have also read a lot of language source code and are also interested in the syntax of different languages. So let's talk about this question briefly.

The first appearance is the LISP language. The LISP Language is famous for its popularity. It is a functional programming language. All statements are functions, so the function keyword is not required.

The meaning of formula translation in the subsequent Fortran languageForMulaTranSlation. Since it is a formula translation language, the call of the mathematical formula f (x) = a + B in Fortran isx = a + b.

To define a function, use function: (the code is from google)

Function add (a, B) implicit nonereal: a, B! Declare the function parameter type. This is required. real: add! Declares the type of the function return value. This is required. add = a + B! Return Value expression returnend

The COBOL language. The purpose of the COBOL design is different from that of Fortran, and even the assignment statement is absent, and there is no function concept at all. Instead, procedure division (process) is used ).

Followed by Basic:BEginner'sALl-purposeSYmbolicINstructionCOde. Basic is a set builder, not only a function, but also a process. The function uses the function, and sub is used in the process. (Copy fortran and cobol ?)

C language has changed this situation because it is a proprietary design concept of C language: how to call a function, how to declare it. That is to say, if we call a function like this

sum = f(a, b);

So we declare:

Double f (a, B)/* Old syntax */double a; double B; {} double f (double a, double B) // new syntax {}

The subsequent C ++ Java C # follows the C route.

HoweverThe call and declaration are consistent.The premise is that it must be declared before a function is called. In C language, it is not just a function, but must be declared even if the data type is defined.

However, in many dynamic languages, most data and functions do not need to be declared in advance, and many functions are called only once, so they are declared once before function calls, this function will no longer be used in the future, which will become cumbersome. In this case, you need to define the function and call the function separately in syntax.

Another reason is that C is a compilation language. Its syntax is to improve the compilation speed and the code performance after compilation, for example, the principles of data and code separation required by C and pascal compilers in the early days.

Currently, major dynamic programming languages do not need to be compiled or pre-compiled.

functionIt is a flag, indicating that a function is being declared. This keyword has nothing to do with the compilation or interpretation type. Most of the keywords used areDynamic typeFor C/JAVAStatic typeLanguage, indicating the return value and parameter list can be clearly differentiatedFunction DeclarationAndFunction callBecause both the function and the parameter indicate the type, but the language of the dynamic type does not have the type representation. If a specific keyword is not added, the syntax is not very good.Function DeclarationAndFunction callFor example, in C:
int f(int n)Is a statement,f(n)Is a call, but in JavaScript, if you do not addfunctionHow do you explain this keyword?f(n)What is it?

Ask a question. What is the correct meaning of "playing?

No one should give the correct answer, because you do not know whether it is used as a verb, quantizer, or other meaning.

Therefore, we need to analyze the meaning of the word from context such as "a dozen" "hitting" "open.

The function keyword does not need to appear. It depends on the language design.

Qualified programmers know the code text. After compilation, the code will eventually become the machine code or the bytecode used by the vm.functionIt does not matter.functionSuch keywords facilitate the analysis phase before compilation, and can be correctly (and not so laborious) through syntax and semantic analysis.

For example, php does not support 5.4abc()[1]In this way, the function returns an array index (the premise is to ensure that an array of the ideal length is returned). The reason that laruence gave (cannot remember, it is estimated that it is) is that the parsing code is messy, it is hard to achieve. By 5.4, someone was willing to do the dirty work and solved the problem. if someone has optimized the parsing of the function keyword one day, there may be an unnecessaryfunctionPhp.

For example, the syntax is annoying by many people.
The function is defined as follows:

 [ function ] name () compound-command [ redirections ]

When defining a function,functionThe keyword is optional, but there is nofunctionYou need to write()
So, it means bash uses function or () to identify this as a function declaration, not a statement.

For more accurate answers, you need to analyze the specific implementation of language parsing, and determine whether or notabc(){}If conflicting features exist, we will not proceed further.

What is thinking? This is a living example.

Have you ever seen python that uses def to define a function? Have you ever seen a rust using fn to define a function? Have you ever seen swift that uses func to define a function? Have you ever seen bash functions that can be added with or without functions? Have you ever seen scala that can either use def or use val to define functions?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.