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
20
votes
5 answers

Is there an opposite function of preventDefault() in JavaScript?

I am counting words in a text field and after a certain amount of words, I use prevent default. In the else, I would like to re-enable the default commands. Does preventDefault() have an opposite function? Here is some sample code:
Wilkins
  • 800
  • 2
  • 6
  • 12
20
votes
2 answers

Receiving error "Select statements included within a function cannot return data to a client"

Receiving an error when attempting to use a Select statement in a function. The error states: Msg 444, Level 16, State 2, Procedure JDE_GetWhereClause_test, Line 26 Select statements included within a function cannot return data to a client. Any…
Ankur
  • 201
  • 1
  • 2
  • 3
20
votes
11 answers

What happens in assembly language when you call a method/function?

If I have a program in C++/C that (language doesn't matter much, just needed to illustrate a concept): #include void foo() { printf("in foo"); } int main() { foo(); return 0; } What happens in the assembly? I'm not…
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
20
votes
2 answers

How to create a function in bashrc to accept arguments?

I would like to have an alias in my bashrc file to append the argument passed to it from terminal. For example: $ lh300 calls: alias lh3000='open http://localhost:3000' However, if I type: $ lh8080 or lh followed by any number: $ lh#### I would…
CommonCents
  • 315
  • 1
  • 3
  • 7
20
votes
3 answers

Calling a function upon button press

I am trying to call function funcion when I click btn_brow_3. How could I accomplish this? When running the following code, I receive this error message: TypeError: connect() slot argument should be a callable or a signal not "nonetype" import…
user2109621
  • 341
  • 1
  • 3
  • 6
20
votes
4 answers

Turn array into independent function arguments - howto?

I want to use values in an array as independent arguments in a function call. Example: // Values "a" and "b" $arr = array("alpha", "beta"); // ... are to be inserted as $a and $b. my_func($a, $b) function my_func($a,$b=NULL) { echo "{$a} - {$b}";…
Kristoffer Bohmann
  • 3,986
  • 3
  • 28
  • 35
20
votes
4 answers

What object javascript function is bound to (what is its "this")?

I know that inside the function it is this. var func = function { return this.f === arguments.callee; // => true, if bound to some object // => false, if is bound to null, because this.f === undefined } var f = func; // not bound to…
esp
  • 7,314
  • 6
  • 49
  • 79
20
votes
4 answers

How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop

I want to define helper methods on the Array.prototype and Object.prototype. My current plan is to do something like: Array.prototype.find = function(testFun) { // code to find element in array }; So that I can do this: var arr = [1, 2, 3]; var…
wbrady
  • 318
  • 1
  • 2
  • 6
19
votes
3 answers

How to declare function pointer in header and c-file?

I'm a little confused over how to declare a function pointer in a header file. I want to use it in main and a file called menus.c and declare it in menus.h I assume. We want to initialize to point to a certain function. it looks like this: void…
user1106072
  • 331
  • 2
  • 4
  • 8
19
votes
5 answers

Best alternative to a typedef for a function template?

What I'd like to do is something like this: template DataType myFunc(DataType in) { ... } typedef myFunc myFunc_i; myFunc_i(37); ...however, typedefs cannot be used for functions like this in C++. What I was wondering…
Paul Molodowitch
  • 1,366
  • 3
  • 12
  • 29
19
votes
4 answers

Ordering Wordpress Stylesheets?

I have a wordpress theme with a stylesheet that needs to be loaded last, since plugin css are interfering with my theme. I was wondering if there was some type of function I could use to make the main stylesheet load last in my theme.
Jamie
  • 1,909
  • 3
  • 22
  • 47
19
votes
2 answers

What method attributes are used in Django?

Under the documentation for ModelAdmin.list_display it describes a few ways to configure a method/function for usage and display in an admin's list view: admin_order_field (describes which field in the model to use for ordering by the…
Jordan Reiter
  • 20,467
  • 11
  • 95
  • 161
19
votes
7 answers

How can global function exist in C#?

How can global function exist in C# when everything is defined inside a class? I was reading the documentation of OpCodes.Call at MSDN, and was surprised to see the following wordings, The metadata token carries sufficient information to determine…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
19
votes
3 answers

Passing arrays to functions in Perl

I think I have misunderstood some aspects of argument passing to functions in Perl. What's the difference between func(\@array) and func(@array)? AFAIK, in both functions, arguments are passed by reference and in both functions we can change the…
aminfar
  • 2,297
  • 3
  • 27
  • 37
19
votes
2 answers

PHP: Can I declare an abstract function with a variable number of arguments?

I want to be able to declare an abstract function in an parent class, with an unknown number of arguments: abstract function doStuff(...); and then define an implementation with a set of hinted arguments: /** * @param int $userID * @param int…
user668660