When you create a variable within a method/function/procedure, this variable can only be accessed within the scope itself, so you cannot access the function 1 variable by being in function 2 unless it is a return or parameter passed by reference.
Another way to do what you need is to use a global variable, I do not think it is the best way, but meets and is also easier to understand for those who are starting.
To create a global variable we also use the section var
, but just below the section uses
:
uses
SysUtils;
var
jogadores: ?????
But here comes a question: What should be the type of the variable
jogadores
? Since for football we will have 11, basketball 5 and volleyball 6.
The trick is: we don’t need to define the size of a array
in the statement, we can define it in the future. For this just define as a array
of string
:
uses
SysUtils;
var
jogadores: Array of String;
Now that we have the global variable to store players, let’s create the methods responsible for defining the size of the array
and request names to be stored.
But how do we define the size of array
during the execution of
program?
Simple, we will use the function Setlength. This function serves both to define the size of a string
as the size of a array
. In our case, we will define the size of a array
, the first parameter is the variable that will be resized and the second is the new variable size:
uses
SysUtils;
var
jogadores: Array of String;
procedure futebol;
var
i: integer;
begin
SetLength(jogadores, 11);
for i := 1 to 11 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure basquete;
var
i: integer;
begin
SetLength(jogadores, 5);
for i := 1 to 5 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure volei;
var
i: integer;
begin
SetLength(jogadores, 6);
for i := 1 to 6 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
Note that this way the first item of array
will always be the
position 0
, so when we’re assigning we need to subtract 1
of the number the user is seeing.
Now just implement the process you’ve already done, just changing to jogadores
the places that are like jogador
:
uses
SysUtils;
var
jogadores: Array of String;
procedure futebol;
var
i: integer;
begin
SetLength(jogadores, 11);
for i := 1 to 11 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure basquete;
var
i: integer;
begin
SetLength(jogadores, 5);
for i := 1 to 5 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure volei;
var
i: integer;
begin
SetLength(jogadores, 6);
for i := 1 to 6 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
var
esporte, verificar: char;
i: integer;
begin
writeln('Digite o esporte que voce joga([f - futebol][b - basquete][v - volei])');
readln(esporte);
if esporte = 'f' then
begin
futebol;
end
else if esporte = 'b' then
begin
basquete;
end
else if esporte = 'v' then
begin
volei;
end;
writeln('Deseja verificar o nome dos jogadores(s/n)');
readln(verificar);
while verificar = 's' do
begin
writeln('Digite o numero do jogador');
readln(i);
writeln('Nome do jogador ', i, ': ', jogadores[i - 1]);
writeln('Deseja verificar outro jogador(s/n)');
readln(verificar);
end;
readln;
end.
Unable to access this variable this way if you have to use
procedure
really, will have to put the variablejogador
in the global scope. Or if you can change tofunction
can return the list of names.– Roberto de Campos