Learning programming languages

  • Thread starter Stephanus
  • Start date
  • #26
35,237
7,057
Aside from Borland text editor which allow cursor to go up/down vertically :smile:, what I like about Pascal is its nested procedure.
It's very convenient to access local variable globally.
Code:
procedure Level1;
var
  A: Integer;
  procedure Level2;
    procedure Level3;
    begin
     A:=10; { don't have to put A in global scope }
    end;
  begin
  end;
begin
end;
Right, neither C nor C++ allows for functions to be nested inside other functions, although some current versions of C++ such as Visual Studio 2010 and later, allow the programmer to insert lambda expressions into a function or method. Lambda expressions are anonymous functions that are defined inline.

Python allows functions to be defined inside a function, though.
In your example above, you're not defining A globally -- it is local to Level1. Level2 and Level3 have access to variables defined at outer levels, which A is. If you had defined A inside Level3, it would not be visible to Level2 or Level1, so you wouldn't be able to access it "globally".

Stephanus said:
And it's array definition which has a unique characteristic.
Pascal: var A: Integer[7..10];
Basic: dim A(10)
C: int a[10];
Pascal can set the element of the array from 7 not from zero, not a good programming technique tough. Has to leave this concept, because 99% of Delphi objects starts from 0
TStrings.Strings[Index] starts from zero
TList.Items[Index] starts from zero
TMemo.Lines[Index], TDataSet.Fields[Index], and almost many more.
IMO, it's only mildly convenient to be able to index arrays starting at other than zero. Many languages have this capability, among them Basic and Fortran.
Stephanus said:
But of course C++ has multiple inheritance which Pascal don't have.
And operator overloading is the most impressive feature in C++.
But nested procedure is the one that I need most in Pascal.
Pascal OOP has a unique characteristic than C++.
In C++ the inherited constructors) is called automatically. Is it good? Bad? I don't know.
But in Pascal we can delay calling inherited constructor.
Code:
constructor Square.InitSquare;
begin
  A:-10;
  B:=20;
  { some codes here }
  InitRectangle; { call the inherited constructor after some lines processed }
end;
If you write your own constructor in C++, rather than rely on a default constructor, you can have it do what you want.
Stephanus said:
But I know a kid, (not mine unfortunately :frown:), 13 years old, start doing Phyton.
I'm starting to learn Python, myself. I'm finding it to be a very interesting language with lots of unusual capabilities.
 
  • #27
1,316
104
In your example above, you're not defining A globally -- it is local to Level1. Level2 and Level3 have access to variables defined at outer levels, which A is. If you had defined A inside Level3, it would not be visible to Level2 or Level1, so you wouldn't be able to access it "globally".
This is what we have to do in C. Move a to global.
Code:
int a; // accessed by level3
void level1()
{
  int a;
  a=50;
  level3();
  cout << a; / will output 50 instead of 20. C can't do that.
}
void level2()
{
}
void level3()
{
  a=20;
}
 
  • #28
35,237
7,057
This is what we have to do in C. Move a to global.
Code:
int a; // accessed by level3
void level1()
{
  int a;
  a=50;
  level3();
  cout << a; // will output 50 instead of 20. C can't do that.
}
void level2()
{
}
void level3()
{
  a=20;
}
You don't have to do this, and it's usually not recommended. It's usually better to pass variables in the parameter list
C:
void level1()
{
  int a;
  a=50;
  level3(a);
  cout << a; // will output 50 instead of 20. C can't do that.
}

void level3(int b)
{
  // level3 has read access to level1's a.
  printf("b is %d", b);   // prints "b is 50"
}
If level3 also needs write access, level3 can be rewritten so that the parameter is a pointer variable.
 
  • #29
2,167
499
And operator overloading is the most impressive feature in C++.

This is also supported by recent versions of Delphi.
 
  • #30
1,316
104
This is also supported by recent versions of Delphi.
Wow, I'm still using Delphi 3, 1996 version. I have a qr barcode library, can't find for the newer version. So I am stuck with it. I just realized, it's been 19 years. The longest programming language that I code.
 

Related Threads on Learning programming languages

Replies
14
Views
1K
Replies
3
Views
2K
  • Last Post
Replies
19
Views
7K
  • Last Post
Replies
1
Views
812
  • Last Post
Replies
16
Views
3K
  • Last Post
Replies
2
Views
585
  • Last Post
Replies
4
Views
1K
Replies
9
Views
974
Top