0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ada言語 2日目 変数(Integer型)

Last updated at Posted at 2018-12-10

#はじめに
前回は入門としてHello Worldしましたが、今回から本格的に始めていこうと思います。
開発環境について学習するのはあとにして、先に基本的な文法をマスターしていきたいと思います。

#今回やったこと
それで今回は小さな一歩ですが、整数型を使って変数について学習しました。
どうやら変数宣言をする場所はアセンブリのdataセグメントみたいに決まっているらしいのです。
実際にソースコード及び実行結果で見てみることにします。

#ソースと実行結果

main.adb
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure main is

	Integer_value1 : Integer := 1;

begin

	Ada.Integer_Text_IO.Put(Integer_value1, 1);

	Integer_value1 := 2;

	Ada.Integer_Text_IO.Put(Integer_value1, 1);

end main;

こちらがソースコードになります。
コンパイル、リンク、実行の手順は1日目と同様、
gcc -c main.adb
gnatbind main
gnatlink main
./main
です。

まず変数宣言ですが、
変数名 : 型;
と宣言するようです。

上のソースでは
変数名 : 型 := 初期値;
としています。

ちなみに初期値を代入しないまま参照するとゴミが表示されます。
さらにwarning: "Integer_value1" may be referenced before it has a valueといったようなワーニングも表示されます。

ここで注意しなければいけないのが変数宣言する場所です。
下のような書き方をすると

main.adb
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure main is

-- この間で変数宣言すること!
-- ちなみに左のはコメントを表す。

begin

	Integer_value1 : Integer := 1;

	Ada.Integer_Text_IO.Put(Integer_value1, 1);

	Integer_value1 := 2;

	Ada.Integer_Text_IO.Put(Integer_value1, 1);

end main;

declarations must come before "begin"
と、エラーがでます。

実行結果ですが、12となります。

今回使用したライブラリと(おそらく)関数については詳しくは学習していませんのでまたいつか。

#今後の予定
当分データ型や変数について学習したあと、条件分岐やループに入っていきたいと思います。

#日記一覧へ
Ada言語を習得する日記一覧

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?