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
19
votes
6 answers

Matching an overloaded function to its polymorphic argument

Okay, the title is a mouthful and I think that's probably why it has been tough to find an answer through google or this site. It might just be that I don't know how to express the problem correctly but here goes: I have a series of methods in a…
Gyan aka Gary Buyn
  • 12,242
  • 2
  • 23
  • 26
19
votes
7 answers

Calling Member Functions within Main C++

#include using namespace std; class MyClass { public: void printInformation(); }; void MyClass::printInformation() { return; } int main() { MyClass::printInformation(); fgetc( stdin ); return(0); } How would…
user40120
  • 648
  • 5
  • 28
  • 58
19
votes
4 answers

Defining a function which returns a function pointer which also returns a function pointer without typedefs

I am trying to really understand function pointers without using typedef but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function. #include void odd() {…
Jas
  • 850
  • 7
  • 21
19
votes
2 answers

Iterating over a vector of functions in MATLAB

Is it possible to iterate over a list of functions in MATLAB? I'm trying to test different functions and this seems like the best way to do it.
Dean Barnes
  • 2,252
  • 4
  • 29
  • 53
19
votes
4 answers

How do I dynamically find metadata for a Clojure function?

Say I have the following code: (defn ^{:graph-title "Function 1"} func-1 [x] (do-something-with x)) (defn get-graph-title [func] (str ((meta func) :graph-title))) I expect this to return "Function 1", but it returns nil. I think…
funkymunky
  • 1,811
  • 3
  • 20
  • 22
19
votes
6 answers

How can I change image source on click with jQuery?

I 'm currently building a full background image layout and I want to change the image based on which page the user is visiting. To get to the point: I need to change a images attribute when the user clickes on a link. This is how far I…
Johannes Kärnstam
  • 193
  • 1
  • 1
  • 4
19
votes
6 answers

C++ Design Pattern for Passing a Large Number of Parameters

I have a reasonably-sized class that implements several logically-related algorithms (from graph theory). About 10-15 parameters are required as input to the algorithm. These are not modified by the algorithm, but are used to guide the operation of…
Alan Turing
  • 12,223
  • 16
  • 74
  • 116
19
votes
3 answers

Defining JavaScript functions inside vs. outside of a class

I'm trying to figure out if there's any different when defining functions inside or outside of a class in JavaScript. Why would I choose to do it one way over the other? (Notice my getName [inside class] and getName2 [outside of class]). class…
Birdman
  • 1,404
  • 5
  • 22
  • 49
19
votes
2 answers

C: Return value via stack/register question

I am new to C, and there is one thing I can not understand. When function returns something that is not bigger than register -- my compiler puts it in EAX. When I return big structure (not pointer but structure itself) -- it is returned via…
Ilya
  • 201
  • 1
  • 2
  • 4
19
votes
3 answers

Function to mangle/demangle functions

I have previously, here, been shown that C++ functions aren't easily represented in assembly. Now I am interested in reading them one way or another because Callgrind, part of Valgrind, show them demangled while in assembly they are shown…
Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
19
votes
2 answers

How To Declare Input-Output Parameters In SQL Server Stored Procedure/Function?

In Oracle, We can declare an input-output parameters (not just input or output) like the following: Create Or Replace Procedure USERTEST.SimpleInOutProcedure( p_InputInt Int, p_OutputInt out Int, p_InputOutputInt in out Int ) AS BEGIN …
Ian
  • 30,182
  • 19
  • 69
  • 107
19
votes
5 answers

PHP - override existing function

Can I redeclare a existing function, with the same name, but different code? Or somehow "disable" the old function? I want to redefince a core WordPress function, but since plugins and theme call this function a lot, I need to keep the same function…
Alex
  • 66,732
  • 177
  • 439
  • 641
19
votes
5 answers

Dictionary value as function to be called when key is accessed, without using "()"

I have a dictionary that has values sometimes as strings, and sometimes as a functions. For the values that are functions is there a way to execute the function without explicitly typing () when the key is accessed? Example: d = {1: "A", 2: "B", 3:…
tyleax
  • 1,556
  • 2
  • 17
  • 45
19
votes
1 answer

Rust scoping rules for struct-owned functions

I am trying to understand what exactly the scope is for functions defined within an impl block but which don't accept &self as a parameter. For example, why doesn't the following chunk of code compile? I get the error "cannot find function…
Scott L.
  • 265
  • 2
  • 9
19
votes
2 answers

The body of constexpr function not a return-statement

In the following program, I have added an explicit return statement in func(), but the compiler gives me the following error: m.cpp: In function ‘constexpr int func(int)’: m.cpp:11:1: error: body of constexpr function ‘constexpr int func(int)’ not a…
msc
  • 33,420
  • 29
  • 119
  • 214