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

Wrapper to FOR loops with progress bar

I like to use a progress bar while running slow for loops. This could be done easily with several helpers, but I do like the tkProgressBar from tcltk package. A small example: pb <- tkProgressBar(title = "Working hard:", min = 0, max = length(urls),…
daroczig
  • 28,004
  • 7
  • 90
  • 124
20
votes
3 answers

Get the name of the calling procedure or function in Oracle PL/SQL

Does anyone know whether it's possible for a PL/SQL procedure (an error-logging one in this case) to get the name of the function/procedure which called it? Obviously I could pass the name in as a parameter, but it'd be nice to make a system call or…
Paul Yeoman
  • 344
  • 1
  • 3
  • 11
20
votes
1 answer

Use a variable to define a PHP function

I'd like to dynamically name a few functions using variables, like this: $thing = 'some_function'; function $thing() { echo 'hi!'; } I know I can call a function using a variable like this: $something = 'function_exists'; if(…
Mick
  • 203
  • 1
  • 2
  • 5
20
votes
5 answers

Emulating named function parameters in PHP, good or bad idea?

Named function parameters can be emulated in PHP if I write functions like this function pythonic(array $kwargs) { extract($kwargs); // .. rest of the function body } // if params are optional or default values are required function…
Imran
  • 87,203
  • 23
  • 98
  • 131
20
votes
4 answers

Returning tuple with a single item from a function

Just came across this little bit of weirdness in Python and thought I'd document it write it as a question here in case anyone else is trying to find an answer with the same fruitless search terms I was Looks like tuple unpacking makes it so you…
mcstrother
  • 6,867
  • 5
  • 22
  • 18
20
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
  • 29,461
  • 12
  • 59
  • 75
20
votes
4 answers

How to store methods as function pointers in a map container?

I want to be able to call functions based on the data I read from file. So for each item type, I want to call the desired reader method. I wrote this code, but it does not compile where I want to add function pointers to the map. What is wrong?…
Ring Zero.
  • 459
  • 3
  • 12
20
votes
6 answers

Is it possible to replace (monkeypatch) PHP functions?

You can do this in Python, but is it possible in PHP? >>> def a(): print 1 ... >>> def a(): print 2 ... >>> a() 2 e.g.: Fatal error: Cannot redeclare var_dump() in /tmp/- on line 1
gak
  • 32,061
  • 28
  • 119
  • 154
20
votes
6 answers

View logs of firebase functions in real time

I'm using the firebase functions and it's the first time I use node. My question is very simple, or so I think. I create a simple function, where, theoretically, if a new field is added to the database, this function must react. my code is the…
Sergio76
  • 3,835
  • 16
  • 61
  • 88
20
votes
3 answers

Two functions with the same name in JavaScript - how can this work?

As far as I know, function foo() { aaa(); } is just var foo = function(){ aaa() } in JavaScript. So adding function foo() { bbb(); } should either overwrite the foo variable, or ignore the second definition - that's not the point. The point is that…
tillda
  • 18,150
  • 16
  • 51
  • 70
20
votes
6 answers

How does overloading of const and non-const functions work?

The STL is full of definitions like this: iterator begin (); const_iterator begin () const; As return value does not participate in overloading resolution, the only difference here is the function being const. Is this part of the overloading…
davka
  • 13,974
  • 11
  • 61
  • 86
20
votes
6 answers

What is the difference between a macro and a function in C?

What is the difference between a macro and a function in C? Please tell me one application where I can use macros and functions?
user615929
  • 475
  • 3
  • 7
  • 9
20
votes
9 answers

Difference between ByVal and ByRef?

What is the difference? I always use ByVal, but, I don't really have a good idea of when should I and when not...
Saturn
  • 17,888
  • 49
  • 145
  • 271
20
votes
4 answers

Determine how long PHP executed so far

I need to determine how long a PHP function has been running so far. What are some options to find out how long some PHP code takes to run? I'm using zend framework.
jmint
  • 201
  • 1
  • 2
  • 3
20
votes
2 answers

How can I use modules in web2py?

I have some functions in func.py that I would like to access from my web2py controller called default.py. I tried to import and use func.py in default.py with "import func" and "calculatesomething = func.calculatesomething", but it doesn't work. The…
user569474
  • 451
  • 3
  • 7
  • 14