What Meaning Of This Function - octave

function [TC]=Translate(T0,Base)
end
I know that Translate is a function and T0 and Base his parameter but what is [TC]?

Octave (and matlab) have a rather unique way of returning variables from functions. Instead of defining explicitly what to return from the function using a return keyword, they define from the outset which variables will be returned when the function exits, and octave simply looks for those variables by name at the time the function exits, and returns their values, whatever they may be by that point.
Your function may return nothing:
function returnsNothing();
disp('hello, I return nothing');
end
or it may return one output:
function Out = returnsOne(x)
Out = x+5
disp('This function will return the value of Out');
end
or it may return more than one outputs:
function [Out1, Out2] = returnsTwo(x)
Out1 = x+5;
Out2 = x+10;
end
You would call the last function from the octave terminal (or script) like this:
[a,b] = returnsTwo(5); % this will make a = 10 and b = 15

Related

funtion return [ret] for ret to be 2d

I found this octave function, that returns a tuple in octave:
function [ret] = g(x)
ret(1, 1) = cos(x)
ret(1, 2) = sin(x)
end
I don't get the brackets why for [ret], as we are returning one variable, can you explain please? Because ret is a vector, and why vector inside a vector.
The square brackets there don’t form a vector, they collect the output variables. It is part of the function signature.
function [ret] = … is exactly the same as function ret = …. That is, the brackets are optional when there is a single return variable.
The same is true when there are no return variables, function [] = name(…) is the same as function name(…).

Lua function needs to be assigned to variable

I have been trying to learn some lua recently, and I came across something I didn't understand with functions today, the code below didn't work
function iter()
local i=0
return function() print(i); i=i+1; end
end
iter()
iter()
I had to assign a variable to my function iter() and then call the variable before it would run:
function iter()
local i=0
return function() print(i); i=i+1; end
end
it=iter()
it()
it()
can anyone clarify why that is?
First of all, functions are just values. Your outer function is assigned to the variable iter. That function returns an anonymous function. ("Anonymous" just means you did not give the function a name before returning it.)
Secondly, an argument list in parentheses is basically an operator that calls a function (unless it's in a function declaration). When you use that operator, the function runs and the expression results in the return value.
In the statement iter(), you call a function and ignore its return value, so you never get to see the inner function run.
In the statement it = iter(), you end up with a named function called it. Every time you call it, it increments the i variable from inside the iter call that created it.
As a side note, it would be legal to say iter()() to immediately call the returned function. This wouldn't actually be useful in your case, because each call to iter returns a fresh closure with i starting at zero.

How to call a function within a function in Lua?

I have this code:
function test()
function awesome()
print("im awesome!")
end
function notawesome()
print("im not awesome.")
end
function notevenawesome()
print("im not even awesome...")
end
end
test().notawesome()
When I run this, the console prints
15: attempt to index a nil value
What I'm trying to do is call the function notawesome() within the function test(), how would I do that?
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()
#Axnyff explains what you might be trying to do. I'll explain what you did.
If you are familiar with other languages, please note that Lua does not have function declarations; It has function definitions, which are expressions that produce a function value when evaluated. Function definition statements, which you have used, are just shortcuts that implicitly include an assignment. See the manual.
When you run your code, a function definition is evaluated and the resulting function value is assigned to the variable test. Then the variable test is evaluated and its value is called as a function (and it is one).
When that function executes, three function definitions are evaluated and assigned to the variables awesome, notawesome, and notevenawesome, repsectively. It doesn't return anything.
So, when the result of calling test (nil) is indexed by the string "awesome", you get the error.
If you wanted to call the function value referred by the variable awesome, just do awesome().
If you want to achieve that instead to use the main function you can use an object:
test = {
awesome = (function()
return 'im awesome!'
end),
notawesome = (function()
return 'im not awesome.'
end),
notevenawesome = (function()
return 'im not even awesome...'
end)
}
To call your functions use this:
print(test.notawesome()) --> im not awesome.

How to get the variable a function is assigned to?

I need to get the variable a function's return value is assigned to.
function example()
end
variable = 3 --just for example
variable = example() --I need the value of variable(in this case 3) passed as an argument to example()
Please read the Lua reference on how to call and define functions.
https://www.lua.org/manual/5.3/manual.html#3.4.10
https://www.lua.org/manual/5.3/manual.html#3.4.11
You can simply do this:
function fancyFunction(val)
return val
end
local val = 3
val = fancyFunction(val)
of course this minimum example does not make much sense. You should of course have another return value than your argument. Otherwise you don't have to call the function in the first place.
(Quite hard to understand what you're trying to do.) Could you possibly be referring to object oriented programming? If so, then you need something like this:
--setup
t = {}
function t:set(v) self.value = v end --assign value to 'variable'
function t:get() return self.value end
--use
t:set(3)
print(t:get())

Lua - find out calling function

In Lua, is it possible to know which function has called the current function.
For instance
function a()
get_calling_function() --Should print function b
end
function b()
a()
end
Is something like this possible?
Does the debug library have such functionality?
You could use debug.traceback():
function a()
print(debug.traceback())
end
function b()
a()
end
b()
which would print:
stack traceback:
./test.lua:45: in function 'a'
./test.lua:50: in function 'b'
./test.lua:53: in main chunk
[C]: in ?
you can use debug.sethook() to set up a hook that gets called each time certain special events happen in lua. it can be useful for things like this.
local debugInfo = { caller = nil, callee = nil }
function hook()
local info = debug.getinfo(2)
if info == nil then
debugInfo.callee = nil
return
end
-- we only want to watch lua function calls (not C functions)
if info.what ~= "Lua" then
debugInfo.callee = "C function"
return
end
debugInfo.caller = debugInfo.callee
debugInfo.callee = info.name
end
debug.sethook(hook, "c")
function caller1()
if debugInfo.caller ~= nil and debugInfo.callee ~= nil then
msg = debugInfo.callee.. " was called by ".. debugInfo.caller.. "!"
print(msg)
end
end
function caller2()
caller1()
end
caller2()
this prints 'caller1 was called from caller2!'
debug.sethook can handle 3 different characters in the second parameter so you can let it know when to notify you. 'c' means call your hook function any time a function is called in lua, 'r' means call your hook function every time a function returns in lua, and 'l' means call your hook function whenever lua processes a new line of code.
you could set this up to build your own custom stack trace if you really wanted to, and you could also use debug.getlocal() within your hook to even try to work out what arguments were passed to your called function.
edit for lhf. this is actually a much simpler way of doing what you're asking, if you don't need to track this and just need to know the context of how the function was called.
function caller1()
local current_func = debug.getinfo(1)
local calling_func = debug.getinfo(2)
print(current_func.name.. " was called by ".. calling_func.name.. "!")
end
function caller2()
caller1()
end