09.10.2014 Views

download pascal tutorial (pdf - Tutorials Point

download pascal tutorial (pdf - Tutorials Point

download pascal tutorial (pdf - Tutorials Point

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Passing Arrays as Subprogram Arguments<br />

Pascal allows passing arrays as subprogram parameters. Following function will take an<br />

array as an argument and return average of the numbers passed through the array as<br />

follows:<br />

program arrayToFunction;<br />

const<br />

size = 5;<br />

type<br />

a = array [1..size] of integer;<br />

var<br />

balance: a = (1000, 2, 3, 17, 50);<br />

average: real;<br />

function avg( var arr: a) : real;<br />

var<br />

i :1..size;<br />

sum: integer;<br />

begin<br />

sum := 0;<br />

for i := 1 to size do<br />

sum := sum + arr[i];<br />

avg := sum / size;<br />

end;<br />

begin<br />

(* Passing the array to the function *)<br />

average := avg( balance ) ;<br />

(* output the returned value *)<br />

writeln( 'Average value is: ', average:7:2);<br />

end.<br />

When the above code is compiled and executed, it produces the following result:<br />

Average value is: 214.40<br />

TUTORIALS POINT<br />

Simply Easy Learning Page 89

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!