Questions tagged [functions]

For questions about the design or implementation of functions. This could include the syntax of defining and calling functions or implementing function calls at a lower level.

Filter by
Sorted by
Tagged with
21 votes
3 answers
4k views

Colored vs uncolored functions

I've heard people say "colored" functions (e.g. async functions, or functions marked with throws) are bad because they ...
Zizheng Tai's user avatar
3 votes
1 answer
209 views

Concept Overloading in C++

The meaning of function overloading is that two or more functions can have the same name but different parameters. In C++20, concept has been introduced. I found ...
JimmyHu's user avatar
  • 133
-2 votes
3 answers
212 views

How about support function return itself? [closed]

It seems that no language supports this feature: Define a function that can return the function itself. This function will continue to be called and continue to return itself. For example: ...
Aster's user avatar
  • 2,688
9 votes
2 answers
3k views

How do languages chain higher-order functions while still keeping performance?

How do different programming languages deal with the chaining of higher-order functions (ex. [1,2,3].foldl((a, b) => a+b, 0), ...
apropos's user avatar
  • 897
4 votes
6 answers
1k views

Why tag function definition with def, fn, fun, func or function etc?

What makes it hard for compilers/interpreters that we need to tag the function definition with keyword fn/fun/func/function? I can understand that types for numbers: int, int32, float32, float64... ...
Anubhav's user avatar
  • 173
3 votes
0 answers
84 views

Is it more readable to declare the return type of a function on the left side of it's name? [duplicate]

A lot of C-like programming languages (Java, C#, ...) use the following syntax for defining functions: returnType functionName(parameters...) { } Is there a ...
tigrou's user avatar
  • 131
12 votes
0 answers
250 views

Why were OS/360 PL/I procedure calls so expensive?

In Guy Steele’s famous paper Debunking the “expensive procedure call” myth or, procedure call implementations considered harmful or, LAMBDA: The Ultimate GOTO, he describes the poor performance of ...
Alexis King's user avatar
  • 8,026
12 votes
5 answers
868 views

Prior art on pipelines of function calls

Say I have a loop that looks like this: for i in range(1, 10) { print(i) } Now I want to take the same sequence in reverse, and filter out even numbers. I might ...
Rob N's user avatar
  • 1,073
14 votes
4 answers
3k views

Could function parameters be placed inside names?

While in the shower thinking about my code, my mind went to C#'s type parameters, and I wondered what stops the type param from being placed in the middle of the function name like this: ...
flumperious's user avatar
2 votes
2 answers
252 views

Architecture for overriding "trait" implementations many times in different contexts?

I just asked this question about Rust: Is it possible to create a default trait implementation in Rust, and then override that trait implementation somewhere else? My problem is, in my custom lang, I ...
Lance's user avatar
  • 695
7 votes
6 answers
822 views

Do variadic functions need to have a different calling convention to regular functions?

In C, calling a variadic functions are 'special' and not to be treated as regular functions or vice versa. Calling a variadic function through a regular function pointer, or vice-versa invokes ...
user16217248's user avatar
  • 7,535
4 votes
2 answers
263 views

What are the disadvantages of making methods just functions with a 'this' first parameter?

In Lua, a function is no different from a method, except that a method has this as the first parameter, and calling a method on an object automatically passes the ...
user16217248's user avatar
  • 7,535
14 votes
1 answer
761 views

How can we define a denotational semantics for recursive functions?

Motivation I want to define a denotational semantics (see here or here) for recursive functions in my language, by representing them as mathematical functions. Background Consider a simple language ...
David Young's user avatar
  • 1,967
7 votes
5 answers
2k views

What are the pros/cons of having multiple ways to print?

In C++, there are two ways of printing something: printf() or cout <<. However, in Python, there's only one way: ...
Redz's user avatar
  • 900
10 votes
5 answers
2k views

Return statements vs passing a pointer

Returning a value from a function like int add(int,int); allows for a subset of the functionality of simply passing a pointer ...
Jonathan Woollett-light's user avatar
2 votes
3 answers
307 views

What are the differences between designing a print function Python-style as opposed to C-style?

A regular Python print statement might look like this (Dart printing looks similar to this): print(a_number) # for a number print(a_string) # for a string These ...
Redz's user avatar
  • 900
3 votes
2 answers
180 views

What are the disadvantages of automatically inlining all functions?

Function inlining is when the function called is directly substituted in the calling code. This avoids the overhead of setting up the call stack, managing any relevant pointers that need to be tracked ...
FireTheLost's user avatar
  • 1,583
5 votes
4 answers
231 views

What are the pros and cons of letting users define functions with the same name as builtin functions?

What are the pros and cons of allowing users to define functions with the same name as built-in functions? For example, defining: ...
Redz's user avatar
  • 900
6 votes
3 answers
292 views

Why is implicitly declaring functions problematic?

I believe one used to be able to do this in C: int main(void) { return printf("Hello world!") > 0; } Now this became invalid in C99, and one must ...
user16217248's user avatar
  • 7,535
3 votes
2 answers
609 views

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

Why do many programming languages use brackets () for function definitions and calls? For example, in Python: def f(): ... f() ...
nchistov's user avatar
  • 1,585
3 votes
2 answers
253 views

Entry point of low level languages

I noticed C defines the entry point as a main() function. So does Java. So does C++ (or not, because functions called as global variable initializers are called ...
user16217248's user avatar
  • 7,535
5 votes
2 answers
254 views

Syntax for optionally specifying the return type of any statement, instead of only having this ability when declaring a function?

Background Following a set of simple rules, I can determine the output type of any statement. For example, adding two integers gives me an integer, list.get() gives ...
Dummi26's user avatar
  • 101
1 vote
3 answers
149 views

How can I get around ambiguity with prefix notation?

I’m designing a language which uses Prefix (Polish) notation when applying functions. However, I’ve run into a problem: I’m not sure how to distinguish between function calls and variable references, ...
Jacob's user avatar
  • 341
10 votes
1 answer
177 views

How is fixpoint of type exponentiation implemented?

Fixpoint of type addition or type multiplication is no big deal. You just need to store data in heap. Haskell does this well for lists for example: ...
Dannyu NDos's user avatar
15 votes
3 answers
385 views

What are the trade-offs in supporting Tail Recursion Optimization, but not Tail Call Optimization?

Tail Call Optimization allows a function call as the returned value of a function to be optimized to a goto, preventing the stack from growing. Among other things, ...
Rydwolf Programs's user avatar
11 votes
14 answers
553 views

What are common options for lambda function syntax?

A lot of languages have anonymous functions, also known as lambda functions. Anonymous, in-line, functions that can be used inside expressions for things like mapping and filtering. What are common ...
Ginger's user avatar
  • 2,579
5 votes
1 answer
154 views

Which functions should a compiler cache outputs for?

Because a bare function object might not have constant time complexity, and because the function object might be called with the same argument repeatedly, I thought it might be good to cache its ...
Dannyu NDos's user avatar
7 votes
2 answers
119 views

Must async functions always be marked as such?

In a lot of modern languages (such as Python), asynchronous functions (those that return coroutines) must be explicitly marked as such: ...
Ginger's user avatar
  • 2,579
13 votes
3 answers
423 views

What are the pros and cons of automatically curried functions?

In several functional languages like Haskell and OCaml, functions are automatically curried. This means that a function taking two arguments (lambda x, y: x + y) is ...
naffetS's user avatar
  • 1,027
11 votes
1 answer
197 views

What are the advantages to placing functions in a separate namespace?

In many modern languages, a function is just a specific kind of value that happens to be callable. However, some languages, even if they support first-class functions, assign functions to their own ...
Silvio Mayolo's user avatar
11 votes
2 answers
226 views

Capturing from outer stack to lambda in a stack-based language?

I'm working on a stack based lang that has "wraps", essentially lambdas, that by default run with an isolated stack (though some commands can push things to a wrap's stack, effectively ...
RubenVerg's user avatar
  • 1,412
9 votes
7 answers
417 views

What are the pros and cons of dynamic scoping?

In dynamic scoping, if a function accesses a variable in a higher scope, it does so relative to the location it's called rather than the location it's defined. For example: ...
Rydwolf Programs's user avatar
7 votes
2 answers
262 views

Are there any technical limitations against adding anonymous functions in C?

An anonymous function behaves the same as a regular function with no identifier attached. One could be passed as a callback or stored in a function pointer variable. In C++ they can be written as ...
user16217248's user avatar
  • 7,535
12 votes
2 answers
425 views

Which calling convention should my compiler use?

The LLVM-IR Reference Manual's section on Calling Conventions lists as many as 15 supported calling conventions. These vary in ways including whether they support tail call optimization, how many and ...
Rydwolf Programs's user avatar
17 votes
8 answers
1k views

What are the advantages/disadvantages of prefix type syntax?

Famously, C’s functions and variables have their types specified before the function/variable name: bar foo() {...} bar foo = ...; Most older C derivatives also ...
Seggan's user avatar
  • 2,603
10 votes
5 answers
299 views

What are some options for function definition syntax?

What are some ways that existing programming languages handle function definitions, and what are the pros and cons of each method? By "function definitions" I mean either named (i.e. ...
Jacob's user avatar
  • 341
-4 votes
2 answers
277 views

Why does Python not allow us to call functions like other languages do?

What many other languages do In many languages, I can have a function defined in a file called get_xyz.m (for example) and then other functions or scripts can use ...
Nike Dattani's user avatar
21 votes
5 answers
693 views

What are the advantages of requiring forward declaration of methods/fields like C/C++ does?

In C and C++ (and probably other languages), you need to have at least a declaration of functions and fields before you can use them: ...
kouta-kun's user avatar
  • 1,349