3
$\begingroup$

Why do many programming languages use brackets () for function definitions and calls?

For example, in Python:

def f(): ...

f()

In Go:

func f() { ... }

f()

Many other languages use similar syntax.

$\endgroup$
2

2 Answers 2

12
$\begingroup$

Because of math functions.

Like this:

f(x) = x + 5

Wikipedia

$\endgroup$
1
  • $\begingroup$ That said, many functions, such as sin, are often written without parentheses in math. $\endgroup$
    – xigoi
    Oct 15, 2023 at 18:58
-8
$\begingroup$

Warning: This Idea is not Yet Widely Adopted

Below, I describe why we write functions definitions and function calls with parentheses ().

However, I will warn you, that this newer model for programming languages and is not a popular model at the time of my writing.


Parentheses () indicate that a several distinctly different things should be grouped together into one single unified object

Let us take the pow function as an example.

pow is the name for one of the many canonical function some students write when practicing and learning how to write code.

For example, pow(10, 4) will make the computer leave what it was doing, go calculate somthing, and return with a value of 1000.

In the notation pow(base, exp) we are applying a function named pow to one single input, not two separate inputs.

The input to the pow function is an instance of an an anonymous class such that the anonymous class has fields named base and exp.

That is, we might write,

float base = 0.901;
int   exp  = 30;  

# Below this comment, we create an ***anonymous class*** having attributes named `base` and `exp`
# 
# we instantiate the anonymous class and then assign
# the instance to a memory address with label `args`
# 
# The instance has a name, but the class has no name. 

args   = (base, exp) 
result = pow(args)

# COMMENT FOR HUMAN EYES ONLY:
#    result = 0.04382720037 

Digression

You asked what paratheses () do in the definition and call of a function in a computer programme.

I consider that to be a basic thing students encounter early on in their studies of computer programming.

Therefore, the following digression might help you:

float base = 0.901;
int   exp  = 30;  
args   = (base, exp) 
result = pow(args)  
  • args are arguments or inputs to a function

  • int is integer or whole number 1, 2, 3,891, et cetra...

  • Electronic computers delete all comments before translating code into machine language. Electronic computers cannot read or understand most comments.


In our newer model of programming language syntax, all functions accept ONE input and only one input.

Consider this code written to implement somthing known as a btree:

 def __print_backend(
        self,
        nid=None,
        level=ROOT,
        idhidden=True,
        filter=None,
        key=None,
        reverse=False,
        line_type="ascii-ex",
        data_property=None,
        sorting=True,
        func=print,
    ):
    pass 

In an old-fashioned view of things, the function has 11 inputs.

However, one of many newer views of things is to represent the function as having one single input, such that that single object has 11 separate attributes.

Consider the event in which a computer programmer writes the following piece of code:

foobar(arg1, arg2, arg3)

For our example function named foobar, the input arguments 1, 2 and 3 are all bundled, by a compiler, into a single anonymous class object.

At the time of my writing anonymous class objects are not very popular.

However, functions of no name (anonymous functions) are very popular in some programming languages such as python and the Matrix Laboratory (MatLab).

The purpose of parentheses in functions is to bundle together smaller objects into a larger, more complex, instance of an anonymous class.

In turn, one of the advantages to using parentheses () to create anonymous classes is so that we can use in-fix lexors/tokenizers instead of a pre-fix lexors, tokenizers, and/or parsers.

pow(base, exp) is not a pre-fix function applied to two inputs.

Rather, pow is the left-most argument to the anonymous in-fix operator.

anonymous in-fix operator
├─ pow
├─ instance of an anonymous class (base, exp)
│  ├─ base
│  ├─ exp

When studying arithmetic, children learn that (10)(5) = 10*5.

The product of 10 and 5 need not have a multiplication sign explicitly written.

Likewise, 10y is the same thing as 10 × y

PICTURE OF A SYNTAX TREE FOR PARSING POW(BASE, EXP)

Instead of parsing characters, I recommend parsing transitions between characters.

pow(base, exp)

transition from character to character description
po transition from p to o
ow transition from o to w
w( transition from w to (
(b transition from ( to b
ba transition from b to a

Transitioning from w to ( should create a token for the implicit multiplication operator, or perhaps, we might call it the anonymous infix operator.


$\endgroup$
12
  • $\begingroup$ "pow is the left-most argument to the anonymous in-fix operator." - pow represents a function pointer. I wouldn't necessarily conceive the call operator as an "anonymous infix operator". In most languages, the call operator effectively has a compound syntax (that can't be broken down further into elements), with the left bracket separating the first two operands, commas separating further operands, and the right bracket unambiguously terminating the list of operands on the right (including coping with the case of calling a niladic function, where there is only one operand to the operator). $\endgroup$
    – Steve
    Oct 14, 2023 at 8:20
  • 4
    $\begingroup$ I don't get what this is supposed to say. It seems to be rather about semantic interpretation of function calls, not necessarily of its syntax. For many parts it is hard to tell what is intended to be your own view as opposed to that of (developers) of many programming languages. Please consider to edit for clarity. $\endgroup$ Oct 14, 2023 at 8:37
  • 1
    $\begingroup$ "Anonymous class objects are not very popular" this is false: most languages that are object oriented also have anonymous class syntax. "Anonymous functions are very popular in some programming languages" also false. Most widely used languages these days (except C) have anonymous function syntax. $\endgroup$
    – Seggan
    Oct 14, 2023 at 14:06
  • 1
    $\begingroup$ I really like this approach for handling (), it's one I plan on going with for a future language I'm making (mainly since it means function calls with anonymous structs are a convenient way to do named arguments). But I don't think it this is the reason most, if any, existing languages have for this syntax. $\endgroup$ Oct 14, 2023 at 15:09
  • $\begingroup$ @Steve The transition from alpha-numeric characters to non-alphanumeric characters w(b inside of the string of text pow(base, exp) will eventually resolve to be the call-operator. However, consider 3(1 + 2) and (3)(1) + 2 and 10(x + y) when the string of text located to the left of the parentheses is numbers only, such as 10, then the anonymous infix operator will resolve to multiplication operator for float, integers, or other numeric data-types. $\endgroup$ Oct 16, 2023 at 1:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .