- Pascal Tutorial
- Pascal - Home
- Pascal - Overview
- Pascal - Environment Setup
- Pascal - Program Structure
- Pascal - Basic Syntax
- Pascal - Data Types
- Pascal - Variable Types
- Pascal - Constants
- Pascal - Operators
- Pascal - Decision Making
- Pascal - Loops
- Pascal - Functions
- Pascal - Procedures
- Pascal - Variable Scope
- Pascal - Strings
- Pascal - Booleans
- Pascal - Arrays
- Pascal - Pointers
- Pascal - Records
- Pascal - Variants
- Pascal - Sets
- Pascal - File Handling
- Pascal - Memory
- Pascal - Units
- Pascal - Date & Time
- Pascal - Objects
- Pascal - Classes
- Pascal Useful Resources
- Pascal - Quick Guide
- Pascal - Useful Resources
- Pascal - Discussion
Passing Arrays as Subprogram Arguments
Pascal allows passing arrays as subprogram parameters. Following function will take an array as an argument and return average of the numbers passed through the array as follows −
program arrayToFunction; const size = 5; type a = array [1..size] of integer; var balance: a = (1000, 2, 3, 17, 50); average: real; function avg( var arr: a) : real; var i :1..size; sum: integer; begin sum := 0; for i := 1 to size do sum := sum + arr[i]; avg := sum / size; end; begin (* Passing the array to the function *) average := avg( balance ) ; (* output the returned value *) writeln( 'Average value is: ', average:7:2); end.
When the above code is compiled and executed, it produces the following result −
Average value is: 214.40
pascal_arrays.htm
Advertisements
To Continue Learning Please Login
Login with Google