R
R
roskoshinsky2016-01-20 22:57:42
PHP
roskoshinsky, 2016-01-20 22:57:42

What are functions with such arguments called?

Hey! The question is purely academic. Is there a name for such a principle of forming arguments in functions, when, depending on the type of the argument and / or their number of arguments, they are interpreted as different variables. For example,
foo(a = false, b = false);
If b === false, then a squared is returned. If a and b are defined, then the result of their multiplication with each other is returned.
Maybe "floating arguments"? or some "super-arguments"?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Zakhar Storozhuk, 2016-01-20
@Phell

I have not seen in my experience (although it is relatively small) that someone calls such functions something special.
You are creating a function that has optional arguments.

R
roskoshinsky, 2016-01-20
@roskoshinsky

Colleagues, it's not a matter of obligation or their default. The fact is that, depending on the combination, they are interpreted in their own way - as the values ​​of different internal variables of the function. To make it clearer, I am exaggerating the above example.
foo(a,b);
If the argument a is defined, but b is not defined, then the function returns the square of a. If a and b are defined, then b is returned to the power of a, that is, in a sense, the arguments change in meaning.
In server-side programming, I stick to procedural design in solving problems, that is, around the function. And this kind of approach shows its stable efficiency and elegance. I thought that because of its brightness, it probably has a name, so I decided to ask.

6
65536, 2016-01-21
@65536

never heard of it being called something. I do this for all sorts of complex functions that can be called in different ways. The "magic" call is purely for convenience. for example, there is a function f(a, b). her logic needs these a and b. a as a string or fals, b as an array or null. and let's say in more than half of the cases you need to call with a fals in the first argument, you will have to write f(false, $array) every time, which is unpleasant, and I would like to write just f($array). to do this, just before executing the main logic of the function, you need to artificially remake the arguments. well, it’s better not to prescribe defaults in the declaration, but to collect them using func_get_args, or it’s better to immediately apply the spell list($a, $b, $c) = array_pad(func_get_args(), 3, null);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question