Questions tagged [function]

A function (also called a procedure, method, subroutine, or routine or macro) 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. By creating function the logic can be isolated and can be called repeatedly.

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.

110371 questions
311
votes
8 answers

jQuery's .click - pass parameters to user function

I am trying to call a function with parameters using jQuery's .click, but I can't get it to work. This is how I want it to work: $('.leadtoscore').click(add_event('shot')); which calls function add_event(event) { blah blah blah } It works if I…
Paul Hoffer
  • 12,606
  • 6
  • 28
  • 37
307
votes
8 answers

How do I list the functions defined in my shell?

I can type alias to show a list of all the aliases. But for functions, all I can do is grep my .bash_profile. That only gets the ones in that file, not those defined in subsidiary files or dynamically. Is there a more convenient way to find out what…
Dov
  • 8,000
  • 8
  • 46
  • 75
306
votes
2 answers

Is there a math nCr function in Python?

Is there a built-in nCr (n choose r) function included in the Python math library like the one shown below? I understand that the computation can be programmed, but I thought I'd check to see if it's built-in before I do.
James Mertz
  • 8,459
  • 11
  • 60
  • 87
306
votes
19 answers

PHP function to generate v4 UUID

So I've been doing some digging around and I've been trying to piece together a function that generates a valid v4 UUID in PHP. This is the closest I've been able to come. My knowledge in hex, decimal, binary, PHP's bitwise operators and the like is…
anomareh
  • 5,294
  • 4
  • 25
  • 22
304
votes
15 answers

Calling a function every 60 seconds

Using setTimeout() it is possible to launch a function at a specified time: setTimeout(function, 60000); But what if I would like to launch the function multiple times? Every time a time interval passes, I would like to execute the function (every…
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
304
votes
8 answers

How to use "get_or_create()" in Django?

I'm trying to use get_or_create() for some fields in my forms, but I'm getting a 500 error when I try to do so. One of the lines looks like this: customer.source = Source.objects.get_or_create(name="Website") The error I get for the above code…
Stephen
  • 5,959
  • 10
  • 33
  • 43
292
votes
21 answers

Return array in a function

I have an array int arr[5] that is passed to a function fillarr(int arr[]): int fillarr(int arr[]) { for(...); return arr; } How can I return that array? How will I use it, say I returned a pointer how am I going to access it?
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114
292
votes
10 answers

jQuery 1.9 .live() is not a function

I recently updated jQuery from 1.8 to 2.1. I suddenly discovered that the .live() stops working. I get the error TypeError: $(...).live is not a function. Is there any method I can use in place of .live()?
ngplayground
  • 20,365
  • 36
  • 94
  • 173
287
votes
4 answers

What is "lifting" in Scala?

Sometimes when I read articles in the Scala ecosystem I read the term "lifting" / "lifted". Unfortunately, it is not explained what that exactly means. I did some research, and it seems that lifting has something to do with functional values or…
user573215
  • 4,679
  • 5
  • 22
  • 25
284
votes
13 answers

How to turn a String into a JavaScript function call?

I got a string like: settings.functionName + '(' + t.parentNode.id + ')'; that I want to translate into a function call like so: clickedOnItem(IdofParent); This of course will have to be done in JavaScript. When I do an alert on…
SpoiledTechie.com
  • 10,515
  • 23
  • 77
  • 100
281
votes
9 answers

Passing functions with arguments to another function in Python?

Is it possible to pass functions with arguments to another function in Python? Say for something like: def perform(function): return function() But the functions to be passed will have arguments like: action1() action2(p) action3(p,r)
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
281
votes
12 answers

Difference between method and function in Scala

I read Scala Functions (part of Another tour of Scala). In that post he stated: Methods and functions are not the same thing But he didn't explain anything about it. What was he trying to say?
Anantha Kumaran
  • 10,101
  • 8
  • 33
  • 36
276
votes
5 answers

How can I use optional chaining with arrays and functions?

I'm trying to use optional chaining with an array instead of an object but not sure how to do that: Here's what I'm trying to do myArray.filter(x => x.testKey === myTestKey)?[0]. Also trying similar thing with a function: let x = {a: () => {}, b:…
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
275
votes
14 answers

Set type for function parameters?

Is there a way to let a javascript function know that a certain parameter is of a certain type? Being able to do something like this would be perfect: function myFunction(Date myDate, String myString) { //do stuff } Thank you! Update: Being…
dmr
  • 21,811
  • 37
  • 100
  • 138
272
votes
4 answers

How do I get the function name inside a function in PHP?

Is it possible? function test() { echo "function name is test"; }
omg
  • 136,412
  • 142
  • 288
  • 348