Use the values of a variable array of a procedure in the main algorithm routine

Asked

Viewed 434 times

0

program nome_jogadores_futebol_basquete_volei;

{$APPTYPE CONSOLE}

uses
  SysUtils;

procedure futebol;
  var
    i: integer;
    jogador: array[1..11] of string;
  begin
    for i := 1 to 11 do
      begin
        writeln('Digite o nome do jogador ', i);
        readln(jogador[i]);
      end;
  end;
procedure basquete;
  var
    i: integer;
    jogador: array[1..5] of string;
  begin
    for i := 1 to 5 do
      begin
        writeln('Digite o nome do jogador ', i);
        readln(jogador[i]);
      end;
  end;
procedure volei;
  var
    i: integer;
    jogador: array[1..6] of string;
  begin
    for i := 1 to 6 do
      begin
        writeln('Digite o nome do jogador ', i);
        readln(jogador[i]);
      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, ': ', jogador[i]);
      writeln('Deseja verificar outro jogador(s/n)');
      readln(verificar);
    end;
  readln;
end. 

I would like to use the variable values jogador which is in a procedure, in the main algorithm routine.

  • 1

    Unable to access this variable this way if you have to use procedure really, will have to put the variable jogador in the global scope. Or if you can change to function can return the list of names.

1 answer

3


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.
  • Thank you very well clarified my doubts, I wonder if there would be any problem using the player variable with the stipulated array of 1.. 11 so even if it was volleyball or basketball the variable covers the amount of players, this instead of using the Setlength function.

  • It will work. But you will be using more memory space without need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.