Recent

Author Topic: Having problems with variable types  (Read 2923 times)

gabrieljim

  • New Member
  • *
  • Posts: 13
Having problems with variable types
« 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

gabrieljim

  • New Member
  • *
  • Posts: 13
Re: Having problems with variable types
« Reply #1 on: June 14, 2018, 12:16:20 am »
Oh sorry, here's the pic.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Having problems with variable types
« Reply #2 on: June 14, 2018, 01:00:41 am »
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.
« Last Edit: June 14, 2018, 01:15:12 am by lainz »

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Having problems with variable types
« Reply #3 on: June 14, 2018, 01:07:25 am »
also note that your POWER function is looking for a LongInt when it should be looking for a Double..
The only true wisdom is knowing you know nothing

gabrieljim

  • New Member
  • *
  • Posts: 13
Re: Having problems with variable types
« Reply #4 on: June 14, 2018, 01:25:53 am »
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

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Having problems with variable types
« Reply #5 on: June 14, 2018, 01:31:41 am »
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.

gabrieljim

  • New Member
  • *
  • Posts: 13
Re: Having problems with variable types
« Reply #6 on: June 14, 2018, 11:06:04 pm »
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?

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Having problems with variable types
« Reply #7 on: June 15, 2018, 12:31:44 am »
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.


The only true wisdom is knowing you know nothing

gabrieljim

  • New Member
  • *
  • Posts: 13
Re: Having problems with variable types
« Reply #8 on: June 22, 2018, 03:33:14 pm »
I see, thanks a lot for the info!

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Having problems with variable types
« Reply #9 on: June 22, 2018, 03:47:42 pm »
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.

 

TinyPortal © 2005-2018