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
394
votes
3 answers

How can I call a function within a class?

I have this code which calculates the distance between two coordinates. The two functions are both within the same class. However, how do I call the function distToPoint in the function isNear? class Coordinates: def distToPoint(self, p): …
Steven
  • 4,193
  • 3
  • 17
  • 11
390
votes
12 answers

Ignore python multiple return value

Say I have a Python function that returns multiple values in a tuple: def func(): return 1, 2 Is there a nice way to ignore one of the results rather than just assigning to a temporary variable? Say if I was only interested in the first value,…
Mat
  • 82,161
  • 34
  • 89
  • 109
373
votes
11 answers

PHP append one array to another (not array_push or +)

How to append one array to another without comparing their keys? $a = array( 'a', 'b' ); $b = array( 'c', 'd' ); At the end it should be: Array( [0]=>a [1]=>b [2]=>c [3]=>d ) If I use something like [] or array_push, it will cause one of these…
Danil K
  • 3,777
  • 2
  • 16
  • 7
365
votes
17 answers

How can I handle the warning of file_get_contents() function in PHP?

I wrote a PHP code like this $site="http://www.google.com"; $content = file_get_content($site); echo $content; But when I remove "http://" from $site I get the following warning: Warning: file_get_contents(www.google.com) …
Waseem
  • 11,741
  • 15
  • 41
  • 45
363
votes
12 answers

Passing an array as a function parameter in JavaScript

I'd like to call a function using an array as parameters: const x = ['p0', 'p1', 'p2']; call_me(x[0], x[1], x[2]); // I don't like it function call_me (param0, param1, param2 ) { // ... } Is there a better way of passing the contents of x into…
Robert
  • 4,324
  • 2
  • 18
  • 22
356
votes
4 answers

Can bash show a function's definition?

Is there a way to view a bash function's definition in bash? For example, say I defined the function foobar function foobar { echo "I'm foobar" } Is there any way to later get the code that foobar runs? $ # non-working pseudocode $ echo…
k107
  • 15,882
  • 11
  • 61
  • 59
345
votes
13 answers

How to get the return value of a function passed to multiprocessing.Process?

In the example code below, I'd like to get the return value of the function worker. How can I go about doing this? Where is this value stored? Example Code: import multiprocessing def worker(procnum): '''worker function''' print…
342
votes
5 answers

Proper use of const for defining functions

Are there any limits to what types of values can be set using const in JavaScript, and in particular, functions? Is this valid? Granted it does work, but is it considered bad practice for any reason? const doSomething = () => { ... } Should all…
David Sinclair
  • 4,187
  • 3
  • 17
  • 12
339
votes
3 answers

How to return a result from a VBA function

How do I return a result from a function? For example: Public Function test() As Integer return 1 End Function This gives a compile error. How do I make this function return an integer?
Mike
  • 58,961
  • 76
  • 175
  • 221
337
votes
22 answers

How to get the function name from within that function?

How can I access a function name from inside that function? // parasitic inheritance var ns.parent.child = function() { var parent = new ns.parent(); parent.newFunc = function() { } return parent; } var ns.parent = function() { // at…
Scott Klarenbach
  • 37,171
  • 15
  • 62
  • 91
333
votes
10 answers

Meaning of = delete after function declaration

class my_class { ... my_class(my_class const &) = delete; ... }; What does = delete mean in that context? Are there any other "modifiers" (other than = 0 and = delete)?
Pat O'Keefe
  • 3,333
  • 3
  • 15
  • 5
330
votes
11 answers

strdup() - what does it do in C?

What is the purpose of the strdup() function in C?
Manoj Doubts
  • 13,579
  • 15
  • 42
  • 45
324
votes
11 answers

Defining private module functions in python

According to http://www.faqs.org/docs/diveintopython/fileinfo_private.html: Like most languages, Python has the concept of private elements: Private functions, which can't be called from outside their module However, if I define two…
olamundo
  • 23,991
  • 34
  • 108
  • 149
318
votes
11 answers

Jump to function definition

How can I jump to a function definition using Vim? For example with Visual Assist, I can type Alt+g under a function and it opens a context menu listing the files with definitions. How can I do something like this in vim?
Jon Clegg
  • 3,870
  • 4
  • 25
  • 22
316
votes
6 answers

What is the difference between functions and classes to create reusable widgets?

I have realized that it is possible to create widgets using plain functions instead of subclassing StatelessWidget. An example would be this: Widget function({ String title, VoidCallback callback }) { return GestureDetector( onTap: callback, …
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432