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
572
votes
7 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
  • 5,937
  • 3
  • 16
  • 11
557
votes
21 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
  • 7,226
  • 6
  • 27
  • 36
557
votes
11 answers

Return value in a Bash function

I am working with a bash script and I want to execute a function to print a return value: function fun1(){ return 34 } function fun2(){ local res=$(fun1) echo $res } When I execute fun2, it does not print "34". Why is this the case?
mindia
  • 6,991
  • 4
  • 22
  • 20
556
votes
18 answers

Run function from the command line

I have this code: def hello(): return 'Hi :)' How would I run this directly from the command line?
Steven
  • 5,633
  • 3
  • 15
  • 5
556
votes
30 answers
537
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
  • 10,737
  • 9
  • 38
  • 58
509
votes
5 answers

What is the difference between a function expression vs declaration in JavaScript?

What is the difference between the following lines of code? //Function declaration function foo() { return 5; } //Anonymous function expression var foo = function() { return 5; } //Named function expression var foo = function foo() { return 5;…
Faisal Vali
  • 32,723
  • 8
  • 42
  • 45
479
votes
8 answers

Static vs class functions/variables in Swift classes?

The following code compiles in Swift 1.2: class myClass { static func myMethod1() { } class func myMethod2() { } static var myVar1 = "" } func doSomething() { myClass.myMethod1() myClass.myMethod2() myClass.myVar1 =…
Senseful
  • 86,719
  • 67
  • 308
  • 465
475
votes
4 answers

Passing a dictionary to a function as keyword parameters

I'd like to call a function in python using a dictionary with matching key-value pairs for the parameters. Here is some code: d = dict(param='test') def f(param): print(param) f(d) This prints {'param': 'test'} but I'd like it to just print…
Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
462
votes
14 answers

How do Python functions handle the types of parameters that you pass in?

Unless I'm mistaken, creating a function in Python works like this: def my_func(param1, param2): # stuff However, you don't actually give the types of those parameters. Also, if I remember, Python is a strongly typed language, as such, it seems…
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
456
votes
46 answers

Simplest/cleanest way to implement a singleton in JavaScript

What is the simplest/cleanest way to implement the singleton pattern in JavaScript?
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
420
votes
13 answers

Is there a way to provide named parameters in a function call in JavaScript?

C#, for example, allows using named parameters like so: calculateBMI(70, height: 175); I find this quite useful. How can I get a similar effect in JavaScript? I've tried doing things like myFunction({ param1: 70, param2: 175 }); function…
Robin Maben
  • 22,194
  • 16
  • 64
  • 99
414
votes
7 answers

How do I define a function with optional arguments?

I have a Python function which takes several arguments. Some of these arguments could be omitted in some scenarios. def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None): #code The arguments d through h are strings…
Thalia
  • 13,637
  • 22
  • 96
  • 190
405
votes
34 answers

How to explain callbacks in plain english? How are they different from calling one function from another function?

How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can their power be explained to a novice programmer?
Yahoo-Me
  • 4,933
  • 5
  • 27
  • 26
394
votes
15 answers

How to add an object to an array

How can I add an object to an array (in javascript or jquery)? For example, what is the problem with this code? function() { var a = new array(); var b = new object(); a[0] = b; } I would like to use this code to save many objects in the…
naser
  • 4,373
  • 4
  • 18
  • 13