Lazarus
Programming => General => Topic started by: gabrieljim on June 14, 2018, 12:11:00 am
-
Hello everyone, I've been having problems implementing this function on my code (I'm pretty sure the actual code isn't needed, if it is let me know and I'll post it), but I'm having some trouble and it's probably due to my lack of understanding of variable types, I always have trouble of these sorts because I'm giving a variable type that isn't the one that its specified.
In this particular case, could you help me out a bit? what should i change?
Thanks a lot in advance
-
Oh sorry, here's the pic.
-
Variable identifier expected is because you put 'var' in the parameters of the function, and you're passing it a constant.
Or you remove the var part, or pass it variables to power function.
Español:
Los parametros 'var' sirven para modificarlos adentro de la función.
Aprendiste ya parámetros por copia y por referencia?
Por copia es sin var
function algo(a, b: integer)
Lo que vos le pases en a y b son constantes: por ejemplo una formula, se evalúa y se pasa su valor resultado, si le pasas una variable se copia el valor y se pasa.
Por referencia es con var, es pasar el puntero
function algo(var a, b: integer)
Si modificas dentro de la función a o b ese valor cambia la variable original que vos le pasaste.
Por ejemplo
var
a: integer;
...
numero := 10;
algo(numero)
function algo(var a: integer): integer;
begin
a := 12; // ahora 'numero' vale 12 adentro y afuera de la función
end;
en cambio si sacas var, al cambiar adentro de la función, solo cambia ahí adentro, no se modifica la variable que le pasaste.
-
also note that your POWER function is looking for a LongInt when it should be looking for a Double..
-
Variable identifier expected is because you put 'var' in the parameters of the function, and you're passing it a constant.
Or you remove the var part, or pass it variables to power function.
Español:
Los parametros 'var' sirven para modificarlos adentro de la función.
Aprendiste ya parámetros por copia y por referencia?
Por copia es sin var
function algo(a, b: integer)
Lo que vos le pases en a y b son constantes: por ejemplo una formula, se evalúa y se pasa su valor resultado, si le pasas una variable se copia el valor y se pasa.
Por referencia es con var, es pasar el puntero
function algo(var a, b: integer)
Si modificas dentro de la función a o b ese valor cambia la variable original que vos le pasaste.
Por ejemplo
var
a: integer;
...
numero := 10;
algo(numero)
function algo(var a: integer): integer;
begin
a := 12; // ahora 'numero' vale 12 adentro y afuera de la función
end;
en cambio si sacas var, al cambiar adentro de la función, solo cambia ahí adentro, no se modifica la variable que le pasaste.
I see! It's different with numbers then, thanks for the detailed answer.
also note that your POWER function is looking for a LongInt when it should be looking for a Double..
Eh, I'm sorry but, I don't understand this bit
-
Lo que dijo Jamie es que el parámetro que pide es double o float, ya que estas haciendo operaciones matemáticas que devuelven generalmente numeros con decimales y no las redondeas.
-
Lo que dijo Jamie es que el parámetro que pide es double o float, ya que estas haciendo operaciones matemáticas que devuelven generalmente numeros con decimales y no las redondeas.
Mmmmm I see, so what do I do if I want one parameter to be a variable and the other a number?
-
you only need a VAR for two reasons:
1. the Item you are referencing is very large or an unknown size.
2. You need to modify the item or use that item as a reference point to store data.
In your case emit the "VAR" and it will put the values on the stack for that function to use.
VAR = Pass variable by reference, which means only a pointer to that item is given to your function..
VALUE = Pass Variable by placing a copy of it on the stack of memory the function can see and modify without
changing the original source of the value.
If the item is large and you have no intention of changing the original source of it, use CONST, the compiler
will pass it via "VAR" but not allow you to write to it.
-
I see, thanks a lot for the info!
-
you only need a VAR for two reasons:
1. the Item you are referencing is very large or an unknown size.
2. You need to modify the item or use that item as a reference point to store data.
...
If the item is large and you have no intention of changing the original source of it, use CONST, the compiler will pass it via "VAR" but not allow you to write to it.
With this in mind, reason 1 can be removed. As far as I know, the FPC always optimizes the constant parameter transmission without putting large values on the stack. If you don't trust this, you can use constref.