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
738
votes
13 answers

Pass a JavaScript function as parameter

How do I pass a function as a parameter without the function executing in the "parent" function or using eval()? (Since I've read that it's insecure.) I have this: addContact(entityId, refreshContactList()); It works, but the problem is that…
imperium2335
  • 20,168
  • 36
  • 103
  • 181
729
votes
7 answers

How to pass all arguments passed to my bash script to a function of mine?

Let's say I have defined a function abc() that will handle the logic related to analyzing the arguments passed to my script. How can I pass all arguments my bash script has received to it? The number of params is variable, so I can't just hardcode…
devoured elysium
  • 90,453
  • 117
  • 313
  • 521
721
votes
22 answers

What is the use of the JavaScript 'bind' method?

What is the use of bind() in JavaScript?
Sandeep Kumar
  • 12,284
  • 19
  • 66
  • 100
691
votes
8 answers

How do you pass a function as a parameter in C?

I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?
andrewrk
  • 27,002
  • 25
  • 87
  • 105
633
votes
17 answers

Call a function from another file?

Set_up: I have a .py file for each function I need to use in a program. In this program, I need to call the function from the external files. I've tried: from file.py import function(a,b) But I get the error: ImportError: No module named…
user2977230
  • 6,993
  • 4
  • 12
  • 8
625
votes
1 answer

"Parameter" vs "Argument"

I got parameter and argument kind of mixed up and did not really pay attention to when to use one and when to use the other. Can you please tell me?
dummy
607
votes
10 answers

How can I view the source code for a function?

I want to look at the source code for a function to see how it works. I know I can print a function by typing its name at the prompt: > t function (x) UseMethod("t") In this case, what does…
Joshua Ulrich
  • 163,034
  • 29
  • 321
  • 400
571
votes
11 answers

JavaScript variable number of arguments to function

Is there a way to allow "unlimited" vars for a function in JavaScript? Example: load(var1, var2, var3, var4, var5, etc...) load(var1)
Timo
  • 6,427
  • 6
  • 22
  • 25
566
votes
12 answers

What is a "static" function in C?

The question was about plain c functions, not c++ static methods, as clarified in comments. I understand what a static variable is, but what is a static function? And why is it that if I declare a function, let's say void print_matrix, in let's say…
Slava V
  • 14,804
  • 13
  • 53
  • 60
563
votes
16 answers

Define a global variable in a JavaScript function

Is it possible to define a global variable in a JavaScript function? I want use the trailimage variable (declared in the makeObj function) in other functions. …
hamze
  • 6,041
  • 6
  • 32
  • 42
559
votes
20 answers

Determine function name from within that function (without using traceback)

In Python, without using the traceback module, is there a way to determine a function's name from within that function? Say I have a module foo with a function bar. When executing foo.bar(), is there a way for bar to know bar's name? Or better…
Rob
  • 5,818
  • 3
  • 15
  • 12
495
votes
20 answers

How to return a string value from a Bash function

I'd like to return a string from a Bash function. I'll write the example in java to show what I'd like to do: public String getSomeString() { return "tadaa"; } String variable = getSomeString(); The example below works in bash, but is there a…
Tomas F
  • 6,160
  • 5
  • 22
  • 31
488
votes
12 answers

How can I get the source code of a Python function?

Suppose I have a Python function as defined below: def foo(arg1,arg2): #do something with args a = arg1 + arg2 return a I can get the name of the function using foo.func_name. How can I programmatically get its source code, as I typed…
david
477
votes
11 answers

Difference between return and exit in Bash functions

What is the difference between the return and exit statement in Bash functions with respect to exit codes?
lecodesportif
  • 9,527
  • 8
  • 31
  • 53
463
votes
6 answers

Why do some functions have underscores "__" before and after the function name?

This "underscoring" seems to occur a lot, and I was wondering if this was a requirement in the Python language, or merely a matter of convention? Also, could someone name and explain which functions tend to have the underscores, and why (__init__,…
Chuck Testa
  • 4,847
  • 3
  • 14
  • 11