Questions tagged [function]

A function (also called a procedure, method, subroutine, or routine) is a portion of code intended to carry out a single, specific task. Use this tag for questions which specifically involve creating or calling functions. For help implementing a function to perform a task, use [algorithm] or a task-specific tag instead.

From Wikipedia:

A subroutine, (also known as a procedure, function, routine, method, or subprogram) is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.

The content of a subroutine is its body, which is executed when the subroutine is called or invoked.

A subroutine may be written so that it expects to obtain one or more data values -- known as parameters or arguments -- from the calling program. It may also return a computed value to its caller (its return value), or provide various result values or out(put) parameters. Indeed, a common use of subroutines is to implement mathematical functions, in which the purpose of the subroutine is purely to compute one or more results whose values are entirely determined by the parameters passed to the subroutine. (Examples might include computing the logarithm of a number or the determinant of a matrix.)

However, a subroutine call may also have side effects, such as modifying data structures in the computer's memory, reading from or writing to a peripheral device, creating a file, halting the program or the machine, or even delaying the program's execution for a specified time. A subprogram with side effects may return different results each time it is called, even if it is called with the same arguments. An example is a random number function, available in many languages, that returns a different pseudorandom number each time it is called. The widespread use of subroutines with side effects is a characteristic of imperative programming languages.

A subroutine can be coded so that it may call itself recursively, at one or more places, in order to perform its task. This technique allows direct implementation of functions defined by mathematical induction and recursive divide and conquer algorithms.

A subroutine whose purpose is to compute a single boolean-valued function (that is, to answer a yes/no question) is called a predicate. In logic programming languages, often all subroutines are called "predicates", since they primarily determine success or failure. For example, any type of function is a subroutine but not main().

It is a common unit of code for most other programming languages.

Function also has a mathematical definition, which is important in computer science and statistics. Mathematical function is a one-to-one relationship where for one argument it always return the same one value. In pure functional languages like Haskell there are only mathematical functions allowed.

94703 questions
7629
votes
86 answers

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I have seen the Scheme example given on Wikipedia,…
e-satis
  • 515,820
  • 103
  • 283
  • 322
7174
votes
39 answers

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer used two ways of declaring functions and I can't work out if…
Richard Garside
  • 82,523
  • 9
  • 75
  • 82
3221
votes
25 answers

What is the difference between call and apply?

What is the difference between using call and apply to invoke a function? var func = function() { alert('hello!'); }; func.apply(); vs func.call(); Are there performance differences between the two aforementioned methods? When is it best to use…
John Duff
  • 35,662
  • 4
  • 31
  • 44
2489
votes
28 answers

Set a default parameter value for a JavaScript function

I would like a JavaScript function to have optional arguments which I set a default on, which get used if the value isn't defined (and ignored if the value is passed). In Ruby you can do it like this: def read_file(file, delete_after = false) #…
Tilendor
  • 45,619
  • 15
  • 48
  • 58
2096
votes
26 answers

What is the scope of variables in JavaScript?

What is the scope of variables in javascript? Do they have the same scope inside as opposed to outside a function? Or does it even matter? Also, where are the variables stored if they are defined globally?
lYriCAlsSH
  • 54,286
  • 10
  • 24
  • 20
1911
votes
36 answers

What's the difference between a method and a function?

Can someone provide a simple explanation of methods vs. functions in OOP context?
willc2
  • 36,081
  • 24
  • 84
  • 99
1328
votes
8 answers

What does the exclamation mark do before the function?

!function () {}();
Sebastian Otto
  • 13,783
  • 4
  • 15
  • 20
1128
votes
7 answers

Passing parameters to a Bash function

I am trying to search how to pass parameters in a Bash function, but what comes up is always how to pass parameter from the command line. I would like to pass parameters within my script. I tried: myBackupFunction("..", "...", "xx") function…
stivlo
  • 77,013
  • 31
  • 135
  • 193
950
votes
26 answers

Why are Python lambdas useful?

I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in real life should be forgotten? I'm sure there are some edge cases where it might be needed, but given the obscurity of it, the potential of it…
meade
  • 21,435
  • 12
  • 29
  • 36
885
votes
3 answers

JavaScript plus sign in front of function expression

I’ve been looking for information about immediately invoked functions, and somewhere I stumbled on this notation: +function(){console.log("Something.")}() Can someone explain to me what the + sign in front of the function means/does?
jOpacic
  • 9,083
  • 11
  • 31
  • 57
869
votes
13 answers

How to get a function name as a string?

In Python, how do I get a function name as a string, without calling the function? def my_function(): pass print get_function_name_as_string(my_function) # my_function is not in quotes should output "my_function". Is such function available in…
X10
  • 14,635
  • 7
  • 27
  • 26
866
votes
13 answers

What is the difference between a 'closure' and a 'lambda'?

Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused. And now that we're here, how do they differ from a regular function?
sker
  • 15,764
  • 8
  • 35
  • 41
865
votes
22 answers

Javascript call() & apply() vs bind()?

I already know that apply and call are similar functions which setthis (context of a function). The difference is with the way we send the arguments (manual vs array) Question: But when should I use the bind() method ? var obj = { x: 81, getX:…
Royi Namir
  • 131,490
  • 121
  • 408
  • 714
852
votes
13 answers

What is the naming convention in Python for variable and function names?

Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase: // C# example string thisIsMyVariable = "a" public void ThisIsMyMethod() In Python, I have seen the above but I have also…
Ray
  • 169,974
  • 95
  • 213
  • 200
836
votes
28 answers

Is there a better way to do optional function parameters in JavaScript?

I've always handled optional parameters in JavaScript like this: function myFunc(requiredArg, optionalArg){ optionalArg = optionalArg || 'defaultValue'; // Do stuff } Is there a better way to do it? Are there any cases where using || like that…
Mark Biek
  • 135,050
  • 52
  • 150
  • 195
1
2 3
99 100