Recent

Author Topic: Error When Using imported Procedures  (Read 738 times)

bobby6478

  • New member
  • *
  • Posts: 8
Error When Using imported Procedures
« on: October 18, 2019, 05:24:09 pm »
This kind of blows my mind.

So, I have a procedure

Code: Pascal  [Select]
  1. procedure swapIntegers(var i :integer; var j:integer); // can I just write var i,j here? I know you can when not passing by reference
  2. var tmp : integer;
  3. begin
  4.     tmp := i;
  5.     i := j;
  6.     j := tmp;
  7. end;
  8.  

it works great inside of one of my units. I then decided that this really can be used in many different cases, so I moved it to a "useful functions" unit, but then I get the error after writing the procedure in the interface section of the "useful functions" unit and importing it. The error is

Error: Call by var for arg no. 2 has to match exactly: Got "LongInt" expected "SmallInt"

but here's the thing: this error doesn't happen if I define the function inside my unit. So the operation of importing the function inherently changes the accepted arguments somehow?

Please note that, maybe there is a better procedure for this thats's already defined, but I get the same error for procedures with a call by reference which have only one argument, and my only solution was to rewrite those procedures in the unit that I was using. So I need a solution to the error not a workaround for this specific case.

Thanks!   

Edit: Switched j:=i to j:=tmp

SOLUTION: The error here was that the imported files were compiling in fpc mode by default while my code was in objfpc. objfpc has integers as longints while fpc has integers as smallint.

{$MODE OBJFPC}{H+}

before the interface switches it to objfpc.

https://www.freepascal.org/docs-html/prog/progap4.html

will show you the differences between the modes.
« Last Edit: October 25, 2019, 03:55:09 pm by bobby6478 »

avk

  • Full Member
  • ***
  • Posts: 143
    • my self-education project
Re: Error When Using imported Procedures
« Reply #1 on: October 18, 2019, 05:36:07 pm »

440bx

  • Hero Member
  • *****
  • Posts: 1189
Re: Error When Using imported Procedures
« Reply #2 on: October 18, 2019, 05:41:54 pm »
Code: Pascal  [Select]
  1. procedure swapIntegers(var i :integer; var j:integer); // can I just write var i,j here? I know you can when not passing by reference
  2. var tmp : integer;
  3. begin
  4.     tmp := i;
  5.     i := j;
  6.     j := i;
  7. end;
  8.  
I thought I'd point out that the statement "j := i;" should be "j := tmp;"

You probably want to correct that ;)

HTH.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

bobby6478

  • New member
  • *
  • Posts: 8
Re: Error When Using imported Procedures
« Reply #3 on: October 18, 2019, 05:50:26 pm »
I thought I'd point out that the statement "j := i;" should be "j := tmp;"

You probably want to correct that ;)

HTH.

Thanks for the help! I actually rewrote the function here because I have pascal running on a VM so I couldn't just copy and paste it. The actual code has your correction. I've now also updated the post to include that correction. 

avk

  • Full Member
  • ***
  • Posts: 143
    • my self-education project
Re: Error When Using imported Procedures
« Reply #4 on: October 18, 2019, 05:55:35 pm »
I thought I'd point out that the statement "j := i;" should be "j := tmp;"
Wow, but I didn’t notice. There is an offer as an apology. Procedure declarations can be made shorter:
Code: Pascal  [Select]
  1. procedure swapIntegers(var i, j:integer);
  2. var tmp : integer;
  3. begin
  4.     tmp := i;
  5.     i := j;
  6.     j := tmp;
  7. end;
  8.  
:)
« Last Edit: October 18, 2019, 05:57:16 pm by avk »

Thaddy

  • Hero Member
  • *****
  • Posts: 9142
Re: Error When Using imported Procedures
« Reply #5 on: October 18, 2019, 09:01:34 pm »
You can do it without the temp:
Code: Pascal  [Select]
  1. {$mode objfpc}
  2. procedure swapIntegers(var i, j:integer);inline;
  3. begin
  4.     i := i xor j;
  5.     j := j xor i;
  6.     i := i xor j;
  7. end;
  8. var
  9.   a:integer =100;
  10.   b:integer =-50;
  11. begin
  12.   SwapIntegers(a,b);
  13.   writeln(a:5, b:5);  
  14. end.

Bittwiddling hacks.... It saves memory space, but is not necessarily faster.
Anyway, I thought everybody knew that one.... ;D

(platform Intel:e.g.:  asm xchg eax, edx end; - lock is implicit!, but a micro lock that does not affect pipelines, so do not add lock prefix!!)

[edit this is just based on 32 bit]
« Last Edit: October 22, 2019, 03:24:29 pm by Thaddy »
also related to equus asinus.

coradi

  • Jr. Member
  • **
  • Posts: 90
Re: Error When Using imported Procedures
« Reply #6 on: October 22, 2019, 09:03:12 am »
Maybe as newbie you want write some into the Wiki?
And write an better example
https://wiki.lazarus.freepascal.org/Procedure/de
Amstrad Schneider CPC 6128
Wind XP / 7 / 10

Thaddy

  • Hero Member
  • *****
  • Posts: 9142
Re: Error When Using imported Procedures
« Reply #7 on: October 22, 2019, 11:55:51 am »
Maybe as newbie you want write some into the Wiki?
And write an better example
https://wiki.lazarus.freepascal.org/Procedure/de
Aber dieses link gibt keiner implemention detail für austauschen! That link does not give any implementation detail for swap.
Basically, just add the code we provided to that wiki  :)  (I hope my German is still somewhat correct)
« Last Edit: October 22, 2019, 11:59:46 am by Thaddy »
also related to equus asinus.

PascalDragon

  • Hero Member
  • *****
  • Posts: 664
  • Compiler Developer
Re: Error When Using imported Procedures
« Reply #8 on: October 23, 2019, 09:20:35 am »
Maybe as newbie you want write some into the Wiki?
And write an better example
https://wiki.lazarus.freepascal.org/Procedure/de
Aber dieses link gibt keiner implemention detail für austauschen! That link does not give any implementation detail for swap.
Basically, just add the code we provided to that wiki  :)  (I hope my German is still somewhat correct)
A bit butchered. Correct would be: "Aber unter diesem Link gibt es keine Implementierungsdetails zum Austauschen zweier Variablen."  ;D

Thaddy

  • Hero Member
  • *****
  • Posts: 9142
Re: Error When Using imported Procedures
« Reply #9 on: October 23, 2019, 11:11:52 am »
Not at all is not at all. 8-)
also related to equus asinus.