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
861
votes
28 answers

Is there a better way to do optional function parameters in JavaScript?

I've always handled optional parameters in JavaScript like this: function myFunc(requiredArg, optionalArg){ optionalArg = optionalArg || 'defaultValue'; // Do stuff } Is there a better way to do it? Are there any cases where using || like that…
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
854
votes
16 answers

Pass a JavaScript function as parameter

How do I pass a function as a parameter without the function executing in the "parent" function or using eval()? (Since I've read that it's insecure.) I have this: addContact(entityId, refreshContactList()); It works, but the problem is that…
imperium2335
  • 23,402
  • 38
  • 111
  • 190
823
votes
23 answers

What is the use of the JavaScript 'bind' method?

What is the use of bind() in JavaScript?
Sandeep Kumar
  • 13,799
  • 21
  • 74
  • 110
807
votes
10 answers

How do you pass a function as a parameter in C?

I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?
andrewrk
  • 30,272
  • 27
  • 92
  • 113
806
votes
8 answers

How to change a string into uppercase?

How can I convert a string into uppercase in Python? When I tried to research the problem, I found something about string.ascii_uppercase, but it couldn't solve the problem: >>> s = 'sdsd' >>> s.ascii_uppercase Traceback (most recent call last): …
gadss
  • 21,687
  • 41
  • 104
  • 154
787
votes
19 answers

How do I call a function from another .py file?

file.py contains a function named function. How do I import it? from file.py import function(a,b) The above gives an error: ImportError: No module named 'file.py'; file is not a package
user2977230
  • 8,897
  • 5
  • 14
  • 9
722
votes
1 answer

"Parameter" vs "Argument"

I got parameter and argument kind of mixed up and did not really pay attention to when to use one and when to use the other. Can you please tell me?
dummy
720
votes
27 answers

Determine function name from within that function (without using traceback)

In Python, without using the traceback module, is there a way to determine a function's name from within that function? Say I have a module foo with a function bar. When executing foo.bar(), is there a way for bar to know bar's name? Or better…
Rob
  • 7,420
  • 3
  • 17
  • 12
690
votes
23 answers

Decorators with parameters?

I have a problem with the transfer of the variable insurance_mode by the decorator. I would do it by the following decorator statement: @execute_complete_reservation(True) def test_booking_gta_object(self): self.test_select_gta_object() but…
falek.marcin
  • 9,679
  • 9
  • 28
  • 33
680
votes
13 answers

How can I view the source code for a function?

I want to look at the source code for a function to see how it works. I know I can print a function by typing its name at the prompt: > t function (x) UseMethod("t") In this case, what does…
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
662
votes
17 answers

Define a global variable in a JavaScript function

Is it possible to define a global variable in a JavaScript function? I want use the trailimage variable (declared in the makeObj function) in other functions. …
hamze
  • 7,061
  • 6
  • 34
  • 43
654
votes
11 answers

What is a "static" function in C?

The question was about plain c functions, not c++ static methods, as clarified in comments. I understand what a static variable is, but what is a static function? And why is it that if I declare a function, let's say void print_matrix, in let's say…
Slava V
  • 16,686
  • 14
  • 60
  • 63
641
votes
13 answers

JavaScript variable number of arguments to function

Is there a way to allow "unlimited" vars for a function in JavaScript? Example: load(var1, var2, var3, var4, var5, etc...) load(var1)
Timo
  • 7,195
  • 7
  • 24
  • 25
615
votes
27 answers

How to get the return value from a thread?

The function foo below returns a string 'foo'. How can I get the value 'foo' which is returned from the thread's target? from threading import Thread def foo(bar): print('hello {}'.format(bar)) return 'foo' thread = Thread(target=foo,…
wim
  • 338,267
  • 99
  • 616
  • 750
613
votes
15 answers

How can I get the source code of a Python function?

Suppose I have a Python function as defined below: def foo(arg1,arg2): #do something with args a = arg1 + arg2 return a I can get the name of the function using foo.func_name. How can I programmatically get its source code, as I typed…
david