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
16
votes
2 answers

How deep do compilers inline functions?

Say I have some functions, each of about two simple lines of code, and they call each other like this: A calls B calls C calls D ... calls K. (So basically it's a long series of short function calls.) How deep will compilers usually go in the call…
Paul Manta
  • 27,853
  • 27
  • 109
  • 192
16
votes
1 answer

In vim, how do I define a function that can be called without :call?

How do I define a function so that I can call it from command-line mode without :call in front of it? Right now, I have to do this: :call TrimWhitespace() I want to define it so that I can do this: :TrimWhitespace
kenny
  • 2,979
  • 3
  • 23
  • 28
16
votes
2 answers

Logging current function name

I have a few custom logfunctions that are extensions of cat. A basic example is something like this: catt<-function(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE) { cat(..., format(Sys.time(), "(%Y-%m-%d %H:%M:%S)"),…
Nick Sabbe
  • 11,354
  • 1
  • 40
  • 56
16
votes
4 answers

Help needed with Median If in Excel

I need to return a median of only a certain category on a spread sheet. Example Below Airline 5 Auto 20 Auto 3 Bike 12 Airline 12 Airline 39 ect. How can I write a formula to only return a median value of the Airline…
Alan
  • 161
  • 1
  • 1
  • 3
16
votes
7 answers

get the current function name in javascript

I have this piece of code: function MyFunction() { $.ajax({ type: "POST", url: "ajax.php", dataType: "json", data: "foo=bar", error:function(XMLHttpRequest, textStatus, errorThrown) { …
Matías Cánepa
  • 5,201
  • 4
  • 49
  • 88
16
votes
6 answers

PHP Function Arguments: Array of Objects of a Specific Class

I have a function that takes a member of a particular class: public function addPage(My_Page $page) { // ... } I'd like to make another function that takes an array of My_Page objects: public function addPages($pages) { // ... } I need to…
Niko Efimov
  • 2,063
  • 2
  • 19
  • 29
16
votes
3 answers

Check if a function is a method of some object

How to check if a function is a method of some object? For example: def check_method(f): ... check_method(lambda x: x + 1) # >>> False check_method(SomeClass().some_method) # >>> True There are some special attributes in methods in…
Roman Bodnarchuk
  • 26,469
  • 11
  • 56
  • 73
16
votes
8 answers

Is returning a whole array from a Perl subroutine inefficient?

I often have a subroutine in Perl that fills an array with some information. Since I'm also used to hacking in C++, I find myself often do it like this in Perl, using references: my @array; getInfo(\@array); sub getInfo { my ($arrayRef) = @_; …
Frank
  • 58,417
  • 87
  • 227
  • 317
16
votes
1 answer

Return None from python function annotated with mypy, multiple return types

I come from a Typescript background. I'm bringing static type checking into a python project I'm working on (using mypy). In Typescript, it is valid to return null from a function that is annotated to return something else, i.e. a string: function…
Corey Cole
  • 1,562
  • 13
  • 35
16
votes
3 answers

How to declare a function pointer that returns a function pointer

How do you declare a function pointer that points to a function that has the same parameters and also returns a pointer to a function with the same parameters. i.e. funcPtr points to func1(int a, int b), and func1 returns a pointer to another…
Simon Bagley
  • 320
  • 1
  • 13
16
votes
2 answers

Creating a function in R with variable number of arguments,

When creating a function in R, we usually specify the number of argument like function(x,y){ } That means it takes only two arguments. But when the numbers of arguments are not specified (For one case I have to use two arguments but another case I…
Lzz0
  • 331
  • 1
  • 2
  • 10
16
votes
1 answer

Bash trap on exit from function

Is there possible in bash to call some command when function exits. I mean something like: function foo { # something like this maybe? trap "echo \"exit function foo\"" EXIT # do something } foo And i want exit function foo to be…
bercik
  • 574
  • 5
  • 18
16
votes
2 answers

Vector of std::function<>

Say want to store the following: typedef std::function MyFunctionDecl; ..in a collection: typedef std::vector FunctionVector; FunctionVector v; This is possible, but if I want to find something using…
Moo-Juice
  • 36,340
  • 10
  • 69
  • 121
16
votes
6 answers

Execute dplyr operation only if column exists

Drawing on the discussion on conditional dplyr evaluation I would like conditionally execute a step in pipeline depending on whether the reference column exists in the passed data frame. Example The results generated by 1) and 2) should be…
Konrad
  • 14,406
  • 15
  • 86
  • 141
16
votes
4 answers

Aliasing a function in Perl

In Perl, if I want foo() to do exactly what bar() does, I can do this: sub foo {return bar(@_);} Is there a better way? Something closer to Ruby's "alias" operator?
user354134
1 2 3
99
100