Recent

Author Topic: What is the code for checking whether a variable is defined?  (Read 1288 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
What is the code for checking whether a variable is defined?
« on: February 24, 2024, 11:22:00 pm »
I assume that it is a function which takes a string and checks if there is a variable for it?

These topics of whether is variable is Free, is Nil or is Assigned always catches me out in one way or the other. So lets start with whether it exists in the first place?
Lazarus 3.0/FPC 3.2.2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2090
  • Fifty shades of code.
    • Delphi & FreePascal
Re: What is the code for checking whether a variable is defined?
« Reply #1 on: February 24, 2024, 11:42:55 pm »
Your topics getting more and more confused so when a variable not exists but you mention it somewhere than FPC will not compile your project = variable is free :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

srvaldez

  • New Member
  • *
  • Posts: 36
Re: What is the code for checking whether a variable is defined?
« Reply #2 on: February 24, 2024, 11:53:59 pm »
maybe like this
Code: [Select]
{$ifndef some_constant}
const some_constant = 10;
{$endif}

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: What is the code for checking whether a variable is defined?
« Reply #3 on: February 25, 2024, 12:23:53 am »
hi

it must be me struggling with terminoligy or the question.

In pascal a variable has to be defined/declared before you use it, or it will not compile.
If you have declard a variable and not used it,you get a hint message variable not defined.
If you declare and assign a variable but do not use it you get hint message variable assigned but not used.

Always check the compiler hints,and fix as many as you can, quite often it will fix a problemyou havent thought of.

if you coming from basic, variable are defined on creation/use. most dialects would also set the default values.
ie
Code: Pascal  [Select][+][-]
  1. 10 for i%=1 to10:print i%:next i%
this defines i% as an integer on its use,in pascal you need to define it first
Code: Pascal  [Select][+][-]
  1. program tst;
  2. var i:integer;
  3. begin
  4.   for i:=1 to 10 do writeln(i);
  5. end.

basic to define a string you could use
Code: Pascal  [Select][+][-]
  1. let s$=""
pascal you need to difine it and then set its value
Code: Pascal  [Select][+][-]
  1. var s:string;
  2. begin
  3.   s:='';

in later dialects  this can be done in its definition
Code: Pascal  [Select][+][-]
  1. var s:string='';

Its the job of the programmer to set the initial state of variables before you use them.
most of my old code,initializes the variables in the same order as they are defined,it makes for simpler debugging, i have been burnt many times assuming a default value is assigned on declaration. So always initialize before use.

I have probably not got the question though, so please give specifics



The best way to get accurate information on the forum is to post something wrong and wait for corrections.

440bx

  • Hero Member
  • *****
  • Posts: 4066
Re: What is the code for checking whether a variable is defined?
« Reply #4 on: February 25, 2024, 12:27:37 am »
I assume that it is a function which takes a string and checks if there is a variable for it?
there is a compiler directive {$if declared(<variable_name>)} Pascal statements {$endif} but there is no FPC function that checks for the existence of a variable.
« Last Edit: February 25, 2024, 09:46:32 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: What is the code for checking whether a variable is defined?
« Reply #5 on: February 25, 2024, 12:35:08 am »
Your topics getting more and more confused so when a variable not exists but you mention it somewhere than FPC will not compile your project = variable is free :D

AI training ?  :D
The only true wisdom is knowing you know nothing

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Re: What is the code for checking whether a variable is defined?
« Reply #6 on: February 25, 2024, 02:49:50 am »
I've been doing some Emacs Lisp a lot recently, and perhaps PHP/Ruby/Smalltalk at some time in the past.

Perhaps it is symbols, or classes, or objects, or components I'm talking about, and I'm confusing them with variables in procedural or statically typed languages.

Some of the concepts there are leaking into my FreePascal thought processes.
Lazarus 3.0/FPC 3.2.2

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: What is the code for checking whether a variable is defined?
« Reply #7 on: February 25, 2024, 12:04:12 pm »
I've been doing some Emacs Lisp a lot recently, and perhaps PHP/Ruby/Smalltalk at some time in the past.

Perhaps it is symbols, or classes, or objects, or components I'm talking about, and I'm confusing them with variables in procedural or statically typed languages.

Some of the concepts there are leaking into my FreePascal thought processes.

Then make an example code which will show what you want to achieve. It will be easier for others to understand what you want and help you even if you use incorrect terminology.

TRon

  • Hero Member
  • *****
  • Posts: 2540
Re: What is the code for checking whether a variable is defined?
« Reply #8 on: February 25, 2024, 12:42:53 pm »
What is the code for checking whether a variable is defined?

That question is relatively simple to answer:
Code: Pascal  [Select][+][-]
  1. {$if declared(literal_name_of_variable)}
  2. writeln('do something when the variable is declared');
  3. {$else}
  4. writeln('do something when the variable is not declared');
  5. {$endif}
  6.  

I assume that it is a function which takes a string and checks if there is a variable for it?
Not exactly but hopefully the example above was self explanatory enough ?

Quote
These topics of whether is variable is Free, is Nil or is Assigned always catches me out in one way or the other. So lets start with whether it exists in the first place?
In context with the other question(s) this question makes be doubt if I understood the topic title at all, because what on earth does that have to do with checking whether or not if a variable is defined or not ? Did you perhaps meant: if a variable is assigned or not (contains a valid value) ?
« Last Edit: February 25, 2024, 12:50:48 pm by TRon »

VisualLab

  • Sr. Member
  • ****
  • Posts: 335
Re: What is the code for checking whether a variable is defined?
« Reply #9 on: February 25, 2024, 01:50:45 pm »
I assume that it is a function which takes a string and checks if there is a variable for it?

These topics of whether is variable is Free, is Nil or is Assigned always catches me out in one way or the other. So lets start with whether it exists in the first place?

Pascal (and ObjectPascal) is not PHP or JS.



EDIT:

In some respects, it is much easier to program in languages whose code is compiled to machine code than in interpreted languages. Arguably, the number of pitfalls, ambiguities, and quirks that exist in the interpreters of popular scripting languages far outweigh those that exist in compiled languages.

Difficulties can lurk with classes and generics. But these are completely different types of traps than those in scripting languages.
« Last Edit: February 25, 2024, 01:57:55 pm by VisualLab »

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: What is the code for checking whether a variable is defined?
« Reply #10 on: February 25, 2024, 09:43:49 pm »
Where's Thaddy when we need him.  :)

Personally English is not my first Language. So define and declare, what's the difference?

Thaddy

  • Hero Member
  • *****
  • Posts: 14390
  • Sensorship about opinions does not belong here.
Re: What is the code for checking whether a variable is defined?
« Reply #11 on: February 26, 2024, 08:28:13 am »
In the context of Pascal:
If something is specified vs it is actually used. The correct answer was already given.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018