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
17
votes
1 answer

Why this "Implicit declaration of function 'X'"?

I wrote a simple program to find the Sum, average, biggest and smallest number of 3 numbers. It lets the user to input three (integer) numbers and return the sum, average, max and min. It has no errors but a warning. Here is my source…
0xEDD1E
  • 1,142
  • 1
  • 12
  • 27
17
votes
2 answers

Root mean square function in R

A very brief question. After much searching I couldn't find a function to calculate the RMS of a set of integers. Does such a function exist in R?
Jojo
  • 3,923
  • 7
  • 19
  • 27
17
votes
9 answers

How to Find All Callers of a Function in C++?

I'm refactoring some code in C++, and I want to deprecate some old methods. My current method for finding all of the methods looks like this: Comment out the original method in the source file in which I'm working. Try to compile the code. If a…
James Thompson
  • 43,044
  • 17
  • 61
  • 80
17
votes
4 answers

Python Language Question: attributes of object() vs Function

In python, it is illegal to create new attribute for an object instance like this >>> a = object() >>> a.hhh = 1 throws Traceback (most recent call last): File "", line 1, in AttributeError: 'object' object has no attribute…
Anthony Kong
  • 29,857
  • 33
  • 139
  • 244
17
votes
11 answers

C# Creating and using Functions

I need help with C# programming; I am new to it and I come from C background. I have a Console Application like this: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Add_Function { class Program …
CaTx
  • 1,121
  • 4
  • 17
  • 36
16
votes
2 answers

Working with function types in Go

I wanted to create a function of a certain type. I've found one way to do it, but there must be other, cleaner and nicer ways that do not include using var. What are the alternative ways to declare the function english of type Greeting? package…
ANisus
  • 60,676
  • 28
  • 141
  • 147
16
votes
4 answers

JavaScript Space After Function

I know that white space is irrelevant in JavaScript, but I am curious about style. When defining a function like: function Foo(a, b, c) {} I do not put a space after the function name. But, if I am creating a function as an expression: Bar(function…
Travis Parks
  • 7,747
  • 9
  • 44
  • 83
16
votes
1 answer

How to get the number of arguments of `std::function`?

Is that possible to get the number of arguments of std::function ? Something like NumOfArgument<...>::value. For example, NumOfArgument >::value should be 2.
Yun Huang
  • 3,736
  • 6
  • 25
  • 33
16
votes
5 answers

How can I create a function dynamically?

I have a variable like $string = "blah"; How can I create a function that has the variable value as name? Is this possible in PHP? Like function $string($args){ ... } or something, and be able to call it like: blah($args);
Alex
  • 60,472
  • 154
  • 401
  • 592
16
votes
2 answers

Nested function application

It's quite easy to define an operator like (@) :: [x -> y] -> [x] -> [y] which takes a list of functions and a list of inputs and returns a list of outputs. There are two obvious ways to implement this: Apply the first function to the first input,…
MathematicalOrchid
  • 58,942
  • 16
  • 110
  • 206
16
votes
2 answers

Why is PostgreSQL calling my STABLE/IMMUTABLE function multiple times?

I'm trying to optimise a complex query in PostgreSQL 9.1.2, which calls some functions. These functions are marked STABLE or IMMUTABLE and are called several times with the same arguments in the query. I assumed PostgreSQL would be smart enough to…
EMP
  • 51,372
  • 47
  • 157
  • 214
16
votes
1 answer

Does anyone know the word that built-in function ord() abbreviate for?

python has a built-in function ord(), I think it must be a anbbreviation. Just like another built-in function chr(), which is an abbreviation of the word "character". Does anyone know the word that built-in function ord() abbreviate for?
arnkore
  • 425
  • 4
  • 14
16
votes
3 answers

Smalltalk-style Messages vs. C-style Functions

When should I use messages versus C-style functions?
Jake
  • 14,329
  • 20
  • 64
  • 85
16
votes
7 answers

php: int() function equivalent for bigint type? (int() cuts strings to 2147483647)

php: What's the equivalent int() function for bigint type? (int() cuts big numbers to 2147483647)? Example: $bigint1="12312342306A_C243"; $bigint1=(int)$bigint1;//2147483647 but I want it to be 12312342306.
Haradzieniec
  • 8,150
  • 26
  • 100
  • 199
16
votes
6 answers

Windows .bat/.cmd function library in own file?

there is a nice way to build functions in DOS .bat/.cmd script. To modularize some installation scripts, it would be nice to include a file with a library of functions into an .bat/.cmd script. what I tried was: mainscript.bat call…
andreas
  • 1,266
  • 1
  • 13
  • 33
1 2 3
99
100