Archive: Function with variable


Function with variable
I would like to call a function and give a certain variable like:

Call MyFunction "variable string"

Function "MyFunction" $1
MessageBox MB_OK "$1"
FunctionEnd

Is this possible?


Push $1
Call MyFunction
...
Function MyFunction
Exch $1
MessageBox MB_OK "$1"
Exch $1
FunctionEnd

Or use macros:

!insertmacro MyMacro $1
...
!macro MyMacro Param1
MessageBox MB_OK "${Param1}"
!macroend
Stu

I can't understand clearly how stack and variables are managed.


Can you explain ?

On one hand, there is a stack. On the other hand, many variables... $0..$R0 among them.

What should be passed to the function ? the stack ?
So let's push $1 on top of the stack

StrCpy $1 "value"
push $1


And now run the func

call MyFunction


At this point, I misunderstand what is available to the function and what is not. $1 is a global var, can be seen from anywhere. So MyFunction can read and feed it with some value ... no ? Why dealing with stack ?

Exch $1

Originally Posted by doc [..]When a parameter is specified and is a user variable, exchanges the top element of the stack with the parameter[...]
What that means ? Previously, your stack had only one value: "value". You want to exchange "value" at the top of the stack with ... with "value" again ? (yes, global $1 contains "value"). What for ?

wonderful usage of the $1 var... let's suppose $1 becomes "value2"


Then,

Exch $1


again, what for ? saving the new value "value2" of $1 in stack ? (to make the parameter $1 as in and out ?)
Sometimes, functions run "pop $1".... what that does ? remove the value at stack top and feed $1 with it ?

Many thanks to help me to understand a bit, I read again and again the doc but fail to understand.

Gal'

You use the stack so that you can call the function without having to use the same variable to pass a value.
http://nsis.sourceforge.net/Pop,_Pus...h..._The_Stack

Stu


Okay thank you very much. The link you give me describes the in and out of a stack behaviour, but you point out the key: no need to use global var thanks to this stack-of-parameters.

Many thanks ;)

Gal'