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

Create a function declaring a predefined text array

I need to create a function in Postgres and one of the variables I declare is a predefined text array, but I don't know the syntax to set its values. This is what I have so far: CREATE OR REPLACE FUNCTION testFunction() RETURNS text AS $$ DECLARE …
Edson Horacio Junior
  • 3,033
  • 2
  • 29
  • 50
20
votes
2 answers

Default value for boost::function argument?

I've got a function that I want to take an optional boost::function argument as a callback for reporting an error condition. Is there some special value I can use a the default value to make it optional? For example, with a regular function pointer…
gct
  • 14,100
  • 15
  • 68
  • 107
20
votes
4 answers

How is possible to deduce function argument type in C++?

Having the function definition: void f(int) { } I want to define: int a; but if the function definition changes to: void f(double) { } the variable definition must become: double a; that is, the type of "a" must be the same of the first argument…
user3459257
  • 320
  • 2
  • 7
20
votes
2 answers

innerHtml prepend text instead of appending

I have a function that emulates typing with few lines of basic JavaScript code, however it appends text, but I want to prepend text to the element before any other already existing text/elements without changing html structure. How this can be…
vlad
  • 1,511
  • 5
  • 17
  • 26
20
votes
14 answers

How do I reverse a sublist in a list in place?

I'm supposed to create a function, which input is a list and two numbers, the function reverses the sublist which its place is indicated by the two numbers. for example this is what it's supposed to do: >>> lst = [1, 2, 3, 4, 5] >>> reverse_sublist…
Tam211
  • 723
  • 5
  • 10
  • 27
20
votes
3 answers

When to use missing versus NULL values for passing undefined function arguments in R, and why?

To date when writing R functions I've passed undefined arguments as NULL values and then tested whether they are NULL i.e. f1 <- function (x = NULL) { if(is.null(x)) ... } However I recently discovered the possibility of passing undefined…
joethorley
  • 468
  • 3
  • 7
20
votes
2 answers

Golang function pointer as a part of a struct

I have the following code: type FWriter struct { WriteF func(p []byte) (n int,err error) } func (self *FWriter) Write(p []byte) (n int, err error) { return self.WriteF(p) } func MyWriteFunction(p []byte) (n int, err error) { // this…
user340495
  • 321
  • 1
  • 4
  • 11
20
votes
3 answers

All valid values of an argument of a function in R

Suppose we have an R function whose arguments must be selected out of a finite set of elements. Like qplot(..., geom=""). And geom can take only some values, like bar or point. How can I find out all the valid values the argument of a given function…
Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91
20
votes
7 answers

MATLAB error: Undefined function or method X for input arguments of type 'double'

I'm a new user of Matlab, can you please help: I have the following code in an .M file: function f = divrat(w, C) S=sqrt(diag(diag(C))); s=diag(S); f=sqrt(w'*C*w)/(w'*s); I have stored this file (divrat.M) in the normal Matlab path, and therefore…
Per
20
votes
4 answers

Pass all the values from an array into a function as parameters

I have an array of values: ['a', 'b', 'c', 'd'] and I need to pass these as parameters to a function: window.myFunction('a', 'b', 'c', 'd'); This would be easier if I could just pass the array/object into the function, but the functions are…
Randy Hall
  • 7,716
  • 16
  • 73
  • 151
20
votes
3 answers

Pass function as a parameter in vb.net?

I have class that performs many similar yet different read/write operations to an excel file. All of these operations are defined in separate functions. They all have to be contained within the same long block of code (code that checks and opens a…
20
votes
7 answers

Pass by reference in C

I'm trying to use pass by reference in C so that the function can modify the values of the parameters passed to it. This is the function signature: int locate(char *name, int &s, int &i) However when I try to compile it I get this error that…
neuromancer
  • 53,769
  • 78
  • 166
  • 223
20
votes
3 answers

What's the difference between CRUD and CRUDL?

I don't understand the difference between read and list. According to Wikipedia: The acronym may be extended to CRUDL to cover listing of large data sets which bring additional complexity such as pagination when the data sets are too large to…
BiGXERO
  • 7,104
  • 8
  • 23
  • 25
20
votes
4 answers

what is the meaning of _AX = 1000 in the following C program?

I am a beginner in C programming language, recently I have started learning functions, I have studied that functions use keyword return to return a value in the caller function. For example the following program. int getVal(){ return 1000; } int…
Mayank Tiwari
  • 2,974
  • 5
  • 30
  • 52
20
votes
8 answers

How Non-Member Functions Improve Encapsulation

I read Scott Meyers' article on the subject and quite confused about what he is talking about. I have 3 questions here. Question 1 To explain in detail, assume I am writing a simple vector class with methods like push_back, insert and operator…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184