patternModerate
Why, in programming languages in general, must function declarations be followed by a parameter set, external to the sub-routine scope?
Viewed 0 times
whythemustlanguagessetprogrammingdeclarationsfunctionfollowedsub
Problem
I understand that in programming languages in general, function routines have what I can generally name a parameter set (or "list"), usually symbolized with parenthesis and if it is not empty, it is also utilized in function calls.
In JavaScript:
My problem
I never learned in programming courses:
Interim note
-
One could claim it is needed for a compiler to know what is a function but a
-
One can claim it is needed for separation of
My question
Why, in programming languages in general, must function declarations be followed by a parameter set, external to the sub-routine scope?
In JavaScript:
function myFunction (passed_parameter_1, passed_parameter_2) {
// Some code...
}
myFunction(corresponding_passed_argument_1, corresponding_passed_argument_2);
My problem
I never learned in programming courses:
- What is the name of that feature
- why, in programming languages in general, a user must have this feature, even if the parameter set in
routineand the corresponding argument set incall, are both empty.
- Why not let a user define a group of parameters (or not) per need, only inside a routine's scope instead
Interim note
-
One could claim it is needed for a compiler to know what is a function but a
keyword such as function reinforce the opinion that it isn't.-
One can claim it is needed for separation of
internal implementation and external interface but such separation could be done as two different sub-scopes of the main scope ({}), for example:function myFunction {
par myPar1
par myPar2
// some code
}
myFunction {1,2}My question
Why, in programming languages in general, must function declarations be followed by a parameter set, external to the sub-routine scope?
Solution
In my opinion, your question is formed on the basis of working with a small number of programming languages and extrapolating conclusions that may not be valid.
Here are some assumptions I have recognised that may not be valid which I will later explore in greater detail:
I further understand you hinted that you have no significant background in Computer Science theory, but some of the background material to why functions are usually written like this, is about the theoretical evolutionary route that most current languages came from;
So, lets review some of that, as it may help readers (such as students of Computer Science and programming per se) understand your question and our answers more fully.
Consider an early computer language, such as Fortran, which was devised in 1954. It had functions and used parentheses. Actually, at that time the character set available on computer was so limited that it had no other bracketing symbols, and only had upper case letters. It did not have access to
You can see that we have used parentheses for
Algol 60 also has a similar situation, shown in this example:
integer function one;
begin
one := 1
end;
integer function two (integer p);
begin
two := p
end;
begin
integer array a [ 1: 10 ];
a[one] := two(1)
end
In the above example
We also get some symbol overloading in more modern advanced languages. Take Algol 68 for example. It also uses parentheses for arrays and functions (but also almost everything else), but also like Algol 60 that proceeded it, it uses square brackets
( [] int a := (1,2,3);
print((a[1])) )
Here '[]' are used to indicate the array declaration, whereas
One more historical language is important here, which is LISP. Using LISP we can explore functions as first class objects, lambda functions and other aspects of functions and the notation associated with them. In LISP we can store a function in a named variable like this:
Notice that everything is using parentheses; that is the nature of LISP. LISP is constructed entirely of lists denoted by the tuples in parentheses like this:
Those lists can be either or both data and functions, it makes no difference. Now that the name
We can invoke the function like this:
This leads into coverage of the notation used in command command languages such as Windows/DOS command prompt and unix shells. In them each line is essentially a function call:
Function calls can be nested by using backtick notation:
Here no parentheses have been used at all, yet it is a notation familiar to many experienced computer users.
I have not really covered other uses of functions and their notations, such as functions as first class object
Here are some assumptions I have recognised that may not be valid which I will later explore in greater detail:
- All function calls must use parenthesis.
- All programming languages use these syntactic notations to mean the same thing
- There is some common underlying nature that requires things to be like this
I further understand you hinted that you have no significant background in Computer Science theory, but some of the background material to why functions are usually written like this, is about the theoretical evolutionary route that most current languages came from;
So, lets review some of that, as it may help readers (such as students of Computer Science and programming per se) understand your question and our answers more fully.
Consider an early computer language, such as Fortran, which was devised in 1954. It had functions and used parentheses. Actually, at that time the character set available on computer was so limited that it had no other bracketing symbols, and only had upper case letters. It did not have access to
[]{}<> symbols. Originally all comparisons were made to zero so that comparison operators were not needed, but when they were later introduced they had to be written as .GT. and .LT. due to the lack of characters. The result of this was we could write:FUNCTION B
B=1.0
RETURN
FUNCTION C(D)
C = D
RETURN
DIMENSION A(10)
A(1) = C(1) + B
STOP
ENDYou can see that we have used parentheses for
A(1) (which is an array) and C(1) which is a function call, but not for + B which is also a function call. We have used parentheses for A(10) which is an array declaration and also for C(D) which is a function declaration with a formal parameter. Thee design on the language is such that the compiler can work out what is intended and instruct the computer in its machine code to take appropriate actions. However, sometimes this is not convenient for the compiler writer as that may require multiple passes (or readings) of the program to determine what use of parenthesis was intended before determining the semantics of that fragment of code.Algol 60 also has a similar situation, shown in this example:
integer function one;
begin
one := 1
end;
integer function two (integer p);
begin
two := p
end;
begin
integer array a [ 1: 10 ];
a[one] := two(1)
end
In the above example
one is a function that uses no parentheses in its declaration or invocation, whereas two is a function that used parentheses to indicate the formal parameter list (integer p) and also the actual argument (1). this time they are not confused with arrays at all, because the array exclusively used [] in both its declaration and application. So thus invalidating your assumption on what () might mean.We also get some symbol overloading in more modern advanced languages. Take Algol 68 for example. It also uses parentheses for arrays and functions (but also almost everything else), but also like Algol 60 that proceeded it, it uses square brackets
[]:( [] int a := (1,2,3);
print((a[1])) )
Here '[]' are used to indicate the array declaration, whereas
(1,2,3) indicates the array constant used to initialise it. The print statement is even more complex. The inner parentheses (a[1]) indicate an array constant containing one element and the outer print() indicates a function call, and the [1] indicates an array selection operation. The all-encompassing () are used for the program begin and end.One more historical language is important here, which is LISP. Using LISP we can explore functions as first class objects, lambda functions and other aspects of functions and the notation associated with them. In LISP we can store a function in a named variable like this:
(setq double (function (lambda (x) (+ x x)) ))Notice that everything is using parentheses; that is the nature of LISP. LISP is constructed entirely of lists denoted by the tuples in parentheses like this:
(x x x x)Those lists can be either or both data and functions, it makes no difference. Now that the name
double contains a function we can do various things with it.We can invoke the function like this:
(funcall double 11)This leads into coverage of the notation used in command command languages such as Windows/DOS command prompt and unix shells. In them each line is essentially a function call:
cd path/fileFunction calls can be nested by using backtick notation:
cd `which file`Here no parentheses have been used at all, yet it is a notation familiar to many experienced computer users.
I have not really covered other uses of functions and their notations, such as functions as first class object
Code Snippets
FUNCTION B
B=1.0
RETURN
FUNCTION C(D)
C = D
RETURN
DIMENSION A(10)
A(1) = C(1) + B
STOP
END(setq double (function (lambda (x) (+ x x)) ))(funcall double 11)cd path/filecd `which file`Context
StackExchange Computer Science Q#120797, answer score: 19
Revisions (0)
No revisions yet.