How to Call the 'Function' Function

How do I call the `function` function?

This is because function is a special primitive:

typeof(`function`)
#> [1] "special"

The arguments are not evaluated, so you have actually passed quote(formals(mean)) instead of the value of formals(mean). I don't think there's a way of calling function directly without evaluation tricks, except with an empty formals list which is just NULL.

How to call a function within a function from another function in Python?

You can't, at least not directly.

I'm not sure why you would want to do that. If you want to be able to call func2() from outside func1(), simply define func2() at an appropriate outer scope.

One way that you could do it is to pass a parameter to func1() indicating that it should invoke func2():

def func1(call_func2=False):
def func2():
print("Hello!")
if call_func2:
return func2()

def func3():
func1(True)

but since that requires modification to the existing code, you might as well move func2() to the same scope as func1().


I don't recommend that you do this, however, with some indirection you can reach into the func1() function object and access it's code object. Then using that code object access the code object for the inner function func2(). Finally call it with exec():

>>> exec(func1.__code__.co_consts[1])
Hello!

To generalise, if you had multiple nested functions in an arbitrary order and you wanted to call a specific one by name:

from types import CodeType
for obj in func1.__code__.co_consts:
if isinstance(obj, CodeType) and obj.co_name == 'func2':
exec(obj)

calling a function on a function is it possible?

A functions is not only a first class function, but also a first class object and can contain properties.

function foo() {}
foo.bar = function () { console.log('i am bar'); };
foo.bar();

How do I call a function inside of another function?

function function_one() {    function_two(); // considering the next alert, I figured you wanted to call function_two first    alert("The function called 'function_one' has been called.");}
function function_two() { alert("The function called 'function_two' has been called.");}
function_one();

Calling a Function defined inside another function in Javascript

The scoping is correct as you've noted. However, you are not calling the inner function anywhere.

You can do either:

function outer() { 

// when you define it this way, the inner function will be accessible only from
// inside the outer function

function inner() {
alert("hi");
}
inner(); // call it
}

Or

function outer() { 
this.inner = function() {
alert("hi");
}
}

<input type="button" onclick="(new outer()).inner();" value="ACTION">​

How to call a function in a function in Lua

Functions are values that are created when function definitions are executed. Any variable can reference a function value.

So, although a function definition might inside another function body, the function value it not inside the function. It is available via whatever expressions, table keys and fields, and variables that reference it.

Variables are global or local (including parameters). If a name of a variable is not previously declared as local, it is global. So, executing this code:

function Lib()
function foo(x, y) return x+y end
function goo(x, y) return x-y end
return self
end

Sets the global variable Lib to the function value created by evaluating the function definition. That's all up to that point.

Then, executing:

Lib()

Uses the value of the global variable Lib, assumes it's a function and calls it with no parameters. That executes the function, which evaluates a function definition to obtain a function value and sets it to the global variable foo; similarly for goo; and returns the value of the global variable self (presumably nil). The return value is discarded because the call doesn't do anything with it.

Then, executing:

print(foo(3, 2))

Uses the value of the global variable foo, assumes it's a function and calls it with two parameters. That executes the function, which returns 5. It then uses the value of the global variable print, assumes it's a function and calls it with the value 5. The return value is discarded because the call doesn't do anything with it.

Other answers are leading you to what you might want to do. You can evaluate what they are suggesting using these principles.

How to call a function within a function in Lua?

Your function is not returning anything (thus returning nil). Something like this should work:

function test()
function awesome()
print("im awesome!")
end

function notawesome()
print("im not awesome.")
end

function notevenawesome()
print("im not even awesome...")
end
result = {}
result["notawesome"] = notawesome
result["awesome"] = awesome
result["notevenawesome"] = notevenawesome
return result
end
test().notawesome()


Related Topics



Leave a reply



Submit