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
19
votes
6 answers

"cannot be used as a function error"

I am writing a simple program that uses functions found in different .cpp files. All of my prototypes are contained in a header file. I pass some of the functions into other functions and am not sure if I am doing it correctly. The error I get is…
darko
  • 2,438
  • 8
  • 42
  • 54
19
votes
2 answers

How to determine that a JavaScript function is native (without testing '[native code]')

I would like to know if there is a way to differentiate a JavaScript script function (function(){}) from a JavaScript native function (like Math.cos). I already know the func.toString().indexOf('[native code]') != -1 trick but I was wondering if…
Franck Freiburger
  • 26,310
  • 20
  • 70
  • 95
19
votes
15 answers

JavaScript button onclick not working

This is my code. But it's not working. When I click on the button nothing happens.
Martin W
  • 4,548
  • 3
  • 16
  • 27
19
votes
4 answers

the functions (procedures) in MIPS

I'm new in MIPS language and I don't understand how the functions (procedures) in the MIPS assembly language work. Here are but I will specify my problem : What does: jal jr $ra mean in mips language and the important thing How can we use them…
Snymeh
  • 255
  • 1
  • 3
  • 9
19
votes
4 answers

python 3: how to check if an object is a function?

Am I correct assuming that all functions (built-in or user-defined) belong to the same class, but that class doesn't seem to be bound to any variable by default? How can I check that an object is a function? I can do this I guess: def…
max
  • 49,282
  • 56
  • 208
  • 355
19
votes
2 answers

Why can I not use `new` with an arrow function in JavaScript/ES6?

As far as I know, the arrow function is similar to a normal function. There aren’t any problem when I use it like this: let X = () => {}; let Y = function() {}; X(); Y(); However, the error occurred when I used them with new: let X = () =>…
Ricky
  • 569
  • 4
  • 16
19
votes
2 answers

Call functions with special prefix/suffix

I have a package named "seeder": package seeder import "fmt" func MyFunc1() { fmt.Println("I am Masood") } func MyFunc2() { fmt.Println("I am a programmer") } func MyFunc3() { fmt.Println("I want to buy a car") } Now I want to call…
Ario
  • 1,822
  • 1
  • 21
  • 31
19
votes
7 answers

Swift - Take Nil as Argument in Generic Function with Optional Argument

I am trying to create a generic function that can take an optional argument. Here's what I have so far: func somethingGeneric(input: T?) { if (input != nil) { print(input!); } } somethingGeneric("Hello, World!") // Hello,…
Coder-256
  • 5,212
  • 2
  • 23
  • 51
19
votes
3 answers

php - find if an array contains an element

I have an array with just a list of ids, like so: $my_array = array( 12, 17, 99, 23 ); Now I know I could probably do something like: function in_array($haystack = array(), $needle = NULL) { foreach($haystack as $id) { if ($id == $needle) …
Matthew
  • 15,282
  • 27
  • 88
  • 123
19
votes
1 answer

What does colon do in a javascript function parameter

I saw in a javascript code written by a young guy this function function foo(e:MouseEvent){ ... } I want to know what does e:MouseEvent do?
Mariksel Azemaj
  • 530
  • 7
  • 18
19
votes
4 answers

Stop Javascript Function execution from another Function

Is there any way to stop the execution of a called function from another function? I have following code:- function MainFunction() { //a long code that runs for few time }; MainFuntion();
19
votes
2 answers

What happens when non-static function declaration follows static function declaration?

The following compiles: static int foo() { return 1; } int foo(); But, will it always compile? Is the behavior in this case well defined? And what does it means when a non-static prototype follows a static declaration?
user4209701
19
votes
4 answers

JavaScript - Passing a reference to this current anonymous function

window.addEventListener('unload', function(e) { MyClass.shutdown(); window.removeEventListener('unload', /* how to refer to this function? */); }, false);
Pablo
  • 28,133
  • 34
  • 125
  • 215
19
votes
4 answers

why is there no std::make_function()?

std::function<> is a useful wrapper around almost any callable thing, including free functions, lambdas, functors, member functions, results from std::bind. However, when creating a std::function<>, one must explicitly specify the function signature…
Walter
  • 44,150
  • 20
  • 113
  • 196
19
votes
2 answers

Julia: show body of function (to find lost code)

In the R-language I am able to declare a function and to see the body of the function like so: > megafoobar = function(x){ return(x + 10000 )} > body(megafoobar) { return(x + 10000) } Is something like this also possible in Julia? I wrote a…
cantdutchthis
  • 31,949
  • 17
  • 74
  • 114
1 2 3
99
100