Questions tagged [nested-function]

Nested functions are functions whose definition is lexically enclosed inside another function definition. Nested functions are not necessarily available in every language having the concept of function, and the exact meaning of the term varies among languages.

340 questions
261
votes
8 answers

Why aren't python nested functions called closures?

I have seen and used nested functions in Python, and they match the definition of a closure. So why are they called nested functions instead of closures? Are nested functions not closures because they are not used by the external world? UPDATE: I…
Srikar Appalaraju
  • 66,073
  • 51
  • 206
  • 260
157
votes
12 answers

Is nested function a good approach when required by only one function?

Let's say that a function A is required only by function B, should A be defined inside B? Simple example. Two methods, one called from another: def method_a(arg): some_data = method_b(arg) def method_b(arg): return some_data In Python we…
nukl
  • 8,243
  • 15
  • 40
  • 54
108
votes
4 answers

Local variables in nested functions

Okay, bear with me on this, I know it's going to look horribly convoluted, but please help me understand what's happening. from functools import partial class Cage(object): def __init__(self, animal): self.animal = animal def…
noio
  • 5,435
  • 7
  • 37
  • 61
100
votes
7 answers

JavaScript Nested function

I got a piece of code for javascript which I just do not understand: function dmy(d) { function pad2(n) { return (n < 10) ? '0' + n : n; } return pad2(d.getUTCDate()) + '/' + pad2(d.getUTCMonth() + 1) + '/' + …
Thomas
  • 31,089
  • 118
  • 335
  • 589
89
votes
6 answers

Nested Function in Python

What benefit or implications could we get with Python code like this: class some_class(parent_class): def doOp(self, x, y): def add(x, y): return x + y return add(x, y) I found this in an open-source project, doing…
Hosam Aly
  • 38,883
  • 35
  • 132
  • 179
81
votes
12 answers

What are PHP nested functions for?

In JavaScript nested functions are very useful: closures, private methods and what have you.. What are nested PHP functions for? Does anyone use them and what for? Here's a small investigation I did
meouw
  • 40,162
  • 10
  • 48
  • 67
59
votes
9 answers

How do nested functions work in Python?

def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 Why does the nested function remember the first value 2 even though…
eozzy
  • 58,300
  • 96
  • 249
  • 396
42
votes
11 answers

Javascript call nested function

I have the following piece of code: function initValidation() { // irrelevant code here function validate(_block){ // code here } } Is there any way I can call the validate() function outside the initValidation() function? I've…
Eduard Luca
  • 6,036
  • 14
  • 73
  • 127
30
votes
3 answers

Is self captured within a nested function

With closures I usually append [weak self] onto my capture list and then do a null check on self: func myInstanceMethod() { let myClosure = { [weak self] (result : Bool) in if let this = self { …
29
votes
2 answers

Function inside function - every time?

Let we have this code: def big_function(): def little_function(): ....... ......... The Python documentation says about def statement: A function definition is an executable statement. Its execution binds the function…
dondublon
  • 671
  • 1
  • 6
  • 12
28
votes
1 answer

Implementation of nested functions

I recently found out that gcc allows the definition of nested function. In my opinion, this is a cool feature, but I wonder how to implement it. While it is certainly not difficult to implement direct calls of nested functions by passing a context…
fuz
  • 76,641
  • 24
  • 165
  • 316
28
votes
8 answers

Simulating nested functions in C++

In C the following code works in gcc. int foo( int foo_var ) { /*code*/ int bar( int bar_var ) { /*code*/ return bar_var; } return bar(foo_var); } How can I achieve the same functionality of nested functions in C++ with the gcc…
Paul McCutcheon
  • 283
  • 1
  • 3
  • 4
21
votes
4 answers

What is the practical use of nested functions in swift?

What is the practical use of nested functions? It only makes the code harder to read and doesn't make a particular case easy. func chooseStepFunction(backwards: Bool) -> (Int) -> Int { func stepForward(input: Int) -> Int { return input + 1 } …
Esqarrouth
  • 35,175
  • 17
  • 147
  • 154
20
votes
11 answers

Are nested functions a bad thing in gcc ?

I know that nested functions are not part of the standard C, but since they're present in gcc (and the fact that gcc is the only compiler i care about), i tend to use them quite often. Is this a bad thing ? If so, could you show me some nasty…
LB40
  • 11,121
  • 16
  • 66
  • 104
20
votes
5 answers

How can I combine multiple nested Substitute functions in Excel?

I am trying to set up a function to reformat a string that will later be concatenated. An example string would look like this: Standard_H2_W1_Launch_123x456_S_40K_AB Though sometimes the "S" doesn't exist, and sometimes the "40K" is "60K" or not…
1
2 3
22 23