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

Java 8 Functions - compose and andThen

I find my Java knowledge out-of-date with Java 8, and there are many new language features I am trying to learn. One of them is Functions, specifically the compose and andThen methods. I have written a trivial experiment to test the reversibility of…
NickJ
  • 9,380
  • 9
  • 51
  • 74
20
votes
4 answers

How do I check if a JavaScript function returns a Promise?

Say I have two functions: function f1() { return new Promise((resolve, reject) => { resolve(true); }); } function f2() { } How do I know if f1 will return Promise and f2 will not?
tranminhtam
  • 235
  • 1
  • 2
  • 5
20
votes
2 answers

why in matlab sin(pi) is not exact but sin(pi/2) is exact?

I have a problem in calculation with matlab. I know that "pi" is a floating number and is not exact. So, in matlab sin(pi) is not exactly zero. My question is if "pi" is not exact then why sin(pi/2) is exactly equal 1. sin(pi) --> is not exact…
20
votes
1 answer

How to call a function placed in another directory in Matlab?

I have a large project coded in MATLAB, with 15-18 scripts. It is becoming very challenging to understand the whole code. I was thinking that if I can put some scripts in another folder, it will become very straightforward to understand and maintain…
Atinesh
  • 1,790
  • 9
  • 36
  • 57
20
votes
2 answers

Visual Studio Code - Classes and method lists?

is there any function list or function outline available in "Visual Studio Code"? I need it for JavaScript and PHP. Thanks for any help!
mr.burns
  • 493
  • 2
  • 6
  • 14
20
votes
2 answers

Run function exactly once for each row in a Pandas dataframe

If I have a function def do_irreversible_thing(a, b): print a, b And a dataframe, say df = pd.DataFrame([(0, 1), (2, 3), (4, 5)], columns=['a', 'b']) What's the best way to run the function exactly once for each row in a pandas dataframe. As…
David Nehme
  • 21,379
  • 8
  • 78
  • 117
20
votes
10 answers

Changing array inside function in C

I am learning C and confused why a array created in the main wont change inside the function, i am assuming the array passed is a pointer, and changing the pointer should've change the array , right ? can someone explain what is happening in this…
victorfts
  • 396
  • 2
  • 4
  • 16
20
votes
8 answers

More efficient way to write this algorithm?

Currently working on a Library simulator assignment. Everything is working fine, but I would like to know something just for the sake of knowing it. In this program there are 3 classes: Book, Patron, and Library. The library class contains 3 private…
user5605077
20
votes
6 answers

Should small simple structs be passed by const reference?

I have always been taught that non-primitive types should be passed by const reference rather than by value where possible, ie: void foo(std::string str);//bad void foo(const std::string &str);//good But I was thinking today that maybe actually…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
20
votes
3 answers

jquery combine event functions

instead of example: $(".myfield").blur(function() { // validate }) $(".myfield").keyup(function() { // validate }) is there a way to combine these two?
FFish
  • 10,964
  • 34
  • 95
  • 136
20
votes
10 answers

Algorithm to locate local maxima

I have data that always looks something like this: alt text http://michaelfogleman.com/static/images/chart.png I need an algorithm to locate the three peaks. The x-axis is actually a camera position and the y-axis is a measure of image…
FogleBird
  • 74,300
  • 25
  • 125
  • 131
20
votes
6 answers

Exit batch script from inside a function

I have a problem with my batch file. It builds several programs automatically by doing something like this: set some compilation flags run 'gmake all' call the "check error level" function and if errorlevel 1, exit So it looks like this: set…
Gui13
  • 12,993
  • 17
  • 57
  • 104
20
votes
5 answers

golang return multiple values issue

I was wondering why this is valid go code: func FindUserInfo(id string) (Info, bool) { it, present := all[id] return it, present } but this isn't func FindUserInfo(id string) (Info, bool) { return all[id] } is there a way to avoid the…
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
20
votes
3 answers

Adding a column with consecutive numbers in R

I apologize if this question is abhorrently simple, but I'm looking for a way to just add a column of consecutive integers to a data frame (if my data frame has 200 observations, for example, starting with 1 for the first observation, and ending…
Emily
  • 201
  • 1
  • 2
  • 6
20
votes
2 answers

Why is the function curry called curry?

In many list processing languages (and other languages as well) they have a function called curry, which does some neat things. My question is why do they call it curry? Where does this name come from? My only guess would be the tasty curry dishes…
Teodorico Levoff
  • 1,641
  • 2
  • 26
  • 44