Pass one function's output as parameter in another function - autohotkey

I want to pass the output of one function as a parameter of another function without relying on temp-variables. The ways it tried it, it either did not recognize it as a function, and thus outputed 1, or was just empty. Expected output for the example below should be a1.
I tried it with msgbox % functionb(% functiona(),1); msgbox % functionb(functiona.call(),1) and msgbox % functionb(func("functiona"),1).
Is there a way to do it?
msgbox % functionb(functiona.call(),1)
functiona() {
return a
}
functionb(Var1, Var2) {
output := Var1 Var2
return output
}

I think the proper way would be something like this:
msgbox % functionb(functiona(),1)
functiona() {
return "a"
}
functionb(Var1, Var2) {
return, Var1 Var2
}

amsgbox % functionb(% functiona(),1)
would be trying to force an expression, while already in expression syntax. So that's the wrong part there.
msgbox % functionb(functiona.call(),1)
would be trying to call a function object, but "functiona" is not a function object, it's just the name of a function.
msgbox % functionb(func("functiona"),1)
would be creating a function object, not right either.
Second problem is return a.
You're trying to return the value of a variable named a, as opposed to returning character "a".
The correct way would be:
MsgBox, % functionb(functiona, 1)
functiona()
{
return "a"
}
functionb(Var1, Var2)
{
output := Var1 Var2
return output
}

Related

Autohotkey / AHK - Param %1% not accessible within a function

I'm having a problem with accessing the %1% ( Startup Param that has been passed to the Script by the console ) in Autohotkey.
When I use the following code (outside of a function):
Msgbox %1%
I get the output of the Param that has been passed to the Script. But as soon as I use the following Code:
HelloWorld() {
Msgbox %1%
}
HelloWorld()
The output is empty.
I also tried to assign %1% to a global variable, or to pass it to the Function as a parameter but it didn't work for me neither.
Thank you
I believe the command line parameter variables are considered global variables, so in order to use them in a non-expression context inside a function you have to declare them as global:
HelloWorld() {
global 1
Msgbox %1%
}
HelloWorld()
It gets even more confusing once you want to use them in expressions (such as using % in the text argument for MsgBox), since they will be treated as numbers so you have to indirectly access them through variables:
HelloWorld() {
;global 1
; Neither of these two expressions access the variable named "1"
;Msgbox % 1
;Msgbox % %1%
; You have to do this instead:
p := 1
MsgBox % %p% ; p is translated to 1 and then "1" is used as a variable name
}
HelloWorld()
Note that doing this doesn't require global 1.
If you're using the newest version of AHK, you instead probably want to use the newly introduced built-in variable A_Args, which is an array that holds the command line parameters. Being built-in, it doesn't have to be declared global, and it ultimately makes the code clearer:
HelloWorld() {
MsgBox % "Number of command line args received: " A_Args.Length() "`n"
. "First argument: " A_Args[1]
}
HelloWorld()
Just declare your cli variables as Global - outside the function - to make them globally available to any and all internal functions. For me, this is how I do it with my version of AHK (Version 1.1.25.01):
Global 1, 2, 3
HelloWorld() {
MsgBox Hello`t1:`t%1%`n`t2:`t%2%`n`t3:`t%3%
}
HelloWorld()
Note, these are different command lines:
"Scripts\myScript.ahk" one two three
"Scripts\myScript.ahk" "one two" three
"Scripts\myScript.ahk" "one" "two three"
"Scripts\myScript.ahk" "one two three"
The first is three separate parameters, the second and third, only two and the last is only one param (2 and 3 exist, but are empty).
Hth,

Output of Matlab function is an empty array

I have a Matlab function which returns an array with probability alpha and nothing (i.e. an empty array) with probability 1-alpha:
function [binary_array_e1 , binary_array_e2 ] = croiser(binary_array_p1,binary_array_p2,alpha )
binary_array_e1=[];
binary_array_e2=[];
compt=1;
if (rand <= alpha)
% some stuff that will put sth in binary_array_e1 and binary_array_e2
end
My question is: how should I manage the fact that the function could return empty arrays at the call of the function? Is something like:
[binary_array_e1 , binary_array_e2]=croiser(binary_array_p1,binary_array_p2,alpha);
be sufficient?
If you want to return an empty variable from a function call, you are totally allowed to do it and you are on the right path. Initialize your variables as empty at the beginning of the function...
function [binary_array_e1 , binary_array_e2 ] = croiser(binary_array_p1,binary_array_p2,alpha )
binary_array_e1=[];
binary_array_e2=[];
% ...
end
and then just check the outcome whenever you need to do it, for example:
[binary_array_e1,binary_array_e2] = croiser(binary_array_p1,binary_array_p2,alpha);
if (isempty(binary_array_e1))
% do something 1...
elseif (isempty(binary_array_e2))
% do something 2...
else
% do something 3...
end
Of course, in order to check the outcome, both variables must be returned and evaluated. But if you are returning two empty arrays in one case and two non-empty arrays in the other case, you could also check only one array:
if (isempty(binary_array_e1))
% do something...
else
% do something else...
end
Anyway... there are so many ways to obtain the same result. For example, you could also return a 'logical' variable that tells you immediately if your arrays have been filled with something or not, or you could return a 'struct' filled with your data in order to make everything more compact (I can elaborate on these solutions if you want). It's up to you but I see nothing wrong in your approach!
Yes, that should be fine. You can later check if the output is an empty array using the function isempty

Will returned array be copied by value or returned as reference in MATLAB?

I wanted to ask how values in MATLAB are returned? Are they copied or passed by reference?
take a look at this example with matrix A:
function main
A = foo(10);
return;
end
function [resultMatrix] = foo(count)
resultMatrix = zeros(count, count);
return;
end
Does the copy operation take place when function returns matrix and assigns it to variable A ?
MATLAB uses a system known as copy-on-write in which a copy of the data is only made when it is necessary (i.e. when the data is modified). When returning a variable from a function, it is not modified between when it was created inside of the function and when it was stored in a different variable by the calling function. So in your case, you can think of the variable as being passed by reference. Once the data is modified, however, a copy will be made
You can check this behavior using format debug which will actually tell us the memory location of the data (detailed more in this post)
So if we modify your code slightly so that we print the memory location of each variable we can track when a copy is made
function main()
A = foo(10);
% Print the address of the variable A
fprintf('Address of A in calling function: %s\n', address(A));
% Modify A
B = A + 1;
% Print the address of the variable B
fprintf('Address of B in calling function: %s\n', address(B));
end
function result = foo(count)
result = zeros(count);
% Print the address of the variable inside of the function
fprintf('Address of result in foo: %s\n', address(result));
end
function loc = address(x)
% Store the current display format
fmt = get(0, 'format');
% Turn on debugging display and parse it
format debug
loc = regexp(evalc('disp(x)'), '(?<=pr\s*=\s*)[a-z0-9]*', 'match', 'once');
% Revert the display format to what it was
format(fmt);
end
And this yields the following (or similar) output
Address of result in foo: 7f96d9d591c0
Address of A in calling function: 7f96d9d591c0
Address of B in calling function: 7f96d9c74400
As a side-note, you don't need to explicitly use return in your case since the function will naturally return when it encounters the end. return is only necessary when you need to use it to alter the flow of your program and exit a function pre-maturely.

Series of Variables by name

In AHK script:
Code for finding a value between great numbers of variables above, for one variable:
If (Variable1 = "sin (90°)")
MsgBox Value is reached
How searching by this method between series of variables with different value of number in their names? From Variable5 to Variable15, Variable51 to Variable105, etc.
How modify this code if number from 5 to 15, 51 to 105, or 74 to 117 etc?
number = 5
If (Variable%number% = "sin (90°)")
.............
Is %Variable%number%% acceptable and will works surely?
And here may also be useful Associative Arrays. What is it by simple examples?
Best practice here would probably be to use an array in the first place.
myArray := []
myArray[1] := "bla"
myArray.Push("bla2") ;by using this you don't need to worry about the index number
myArray[3] := "sin (90°)"
myArray[4] := 63456
Loop % myArray.MaxIndex()
{
If (myArray[A_Index] = "sin (90°)")
{
MsgBox Value is reached
}
}
... another example
anotherArray := []
Loop, read, C:\Files\prog.txt
{
If (A_LoopReadLine = "FileRead, OutputVar, C:\Files\prog1.txt")
{
anotherArray.Push(A_LoopReadLine)
MsgBox, An interesting code line was found. And was added to the array.
}
Else If (A_LoopReadLine = "blablabla")
{
anotherArray.Push(A_LoopReadLine)
MsgBox, An interesting code line was found. And was added to the array.
}
Else If (A_LoopReadLine = "some other text line")
{
anotherArray.Push(A_LoopReadLine)
MsgBox, An interesting code line was found. And was added to the array.
}
;Else
;{
; MsgBox, Nothing important was found.
;}
}
Loop % anotherArray.MaxIndex()
{
currentArrayEntry := anotherArray[A_Index]
MsgBox, %currentArrayEntry%
}
In an expression you can use variable expansion to modify the name of the variable to use:
number = 5
If (Variable%number% = "sin (90°)")
.............
But consider using arrays instead.
Is %Variable%number%% acceptable and will works surely?
Not. More right way
% Variable%number%
but it may have a problem.
Possible way is use Var := expression
Variable:= number
may be.

Why am I getting 'output argument not assigned during call to' error in Matlab

I have a function that Finds the Critical Points of a function.
function [ cr ] = CritPt(f, var1, var2)
f = sym(f);
fx = diff(f,var1);
fy = diff(f,var2);
[xcr,ycr] = solve(fx,fy);
crpt = [xcr,ycr]
I am supposed to use the function CritPt in the Command Window to define a variable called cp which contains the critical points of f(x,y)=x^2*y+(1-y)^2
When I attempt this I get:
>> cp=CritPt('x^2*y+(1-y)^2','x','y')
crpt =
[ 0, 1]
[ 2^(1/2), 0]
[ -2^(1/2), 0]
Error in CritPt (line 2)
f = sym(f);
Output argument "cr" (and maybe others) not assigned
during call to
"C:\Users\GTAV\Documents\MATLAB\CritPt.m>CritPt".
I have tried many alternatives like syms cp= [cp] = etc etc but there is clearly something I am not understanding. Any help would be greatly appreciated
You're using the function properly in the command window.
The problem is in the function CritPt itself: You need to assign a value to the variable cr. When the function completes, it attempts to return the value of whatever variable you have listed after function, but if that variable is not present you get an error.
If you want to return the value of the variable on the last line, then change your last line to
cr = [xcr,ycr]
Alternatively, you can leave the last line as it is but change the first line so you return crpt:
function [ crpt ] = CritPt(f, var1, var2)