230 likes | 423 Views
Pascal Programming. Functions, Procedures and Parameters Carl Smith National Certificate Year 2 – Unit 4. Overview. Procedures and functions are referred to collectively as “routines” or “modules”
E N D
Pascal Programming Functions, Procedures and Parameters Carl Smith National Certificate Year 2 – Unit 4
Overview • Procedures and functions are referred to collectively as “routines” or “modules” • They are self-contained statement blocks that can be called from different locations in a program. • A function is a routine that returns a value when it executes. • A procedure is a routine that can change values in the main program (or not…!).
Parameters • Parameters are variables or values passed to a function/proc from the calling program, in Pascal there are two types:- • Value parameters are formal parameters that pass data into a procedure or function, but not out of a procedure or function. • Variable parameters are formal parameters that pass data into a procedure and out of a procedure. Variable parameters can be used to return the results of a procedure. • E.G. x := sqrt (integer_var); {built in function} MYPROC (var1, var2, var3); {user defined}
Four differences between functions and procedures: • Functions return only one value…Procedures can return or change any number of values including none. • A function call must be part of an expression, a procedural call is an individual statement. • Functions return their results via the name of the function, the function_identifier. Procedures only return its results via its parameters - never the procedure_identifier. • Functions can use only value parameters. Procedures can use value and variable parameters.
Exiting procedures • When a procedure or function completes it will naturally return back to the point from which the routine was called. • You can return from a procedure by using “Exit;” within the body of any procedure or function (It is normally used within an IF statement). • Exit halts execution of the routine where it occurs and immediately passes program control back to the point from which the routine was called.
Why use functions and procedures? • The use of functions and procedures enables you to write modular programs which are more structured and easier to debug and modify • Enables the use of re-useable code • Provides “encapsulation” which hides the details of the subprogram. We don’t have to worry how a subprogram works, just when it should be called.This allows teams to work on large systems more effectively.
When to use a function • Questions to ask when creating a function : • What will the function do, remember that the function should only do one task. • What parameters will be required ? • What will the type of the parameters be ? • What will be the type of the function result ? • What local variables may be declared ?
Procedure Declaration • procedure procedureName(parameterList); localDeclarations;begin statementsend; • where procedureName is any valid identifier, statements is a sequence of statements that execute when the procedure is called, and (parameterList), and localDeclarations; are optional.
Example Procedure procedure PerformComputations (PizzaCost : real; PizzaSize : Integer; VAR PricePerSquareInch : real); {DATA Received – Cost and Size of Pizza INFO Returned – Price per square inch of pizza LOGIC – Given the diameter, find the radius then Compute the area using Area = Pi * sqr(Radius) } VAR Radius, Area : real; {local variables} BEGIN Radius:= PizzaSize / 2 ; Area := Pi * sqr(Radius) ; PricePerSquareInch := PizzaCost / Area END; {of Procedure PerformComputations}
Function Declarations • function functionName(parameterList): returnType; localDeclarations;begin statementsend; • where functionName is any valid identifier, returnType is any data type, statements is a sequence of statements that execute when the function is called, and (parameterList), and localDeclarations; are optional.
Example Function Function PricePerSquareInch (Cost, Size : real) : real; {DATA Received – Cost and Size of Pizza INFO Returned – Price per square inch of pizza LOGIC – Given the diameter, find the radius then Compute the area using Area = Pi * sqr(Radius) } VAR Radius, Area : real; {local variables} BEGIN Radius:= PizzaSize / 2 ; Area := Pi * sqr(Radius) ; PricePerSquareInch := PizzaCost / Area END; {of FUNCTION PricePerSquareInch }
User Defined“Cube” Function Function Cube (X : Integer) : Integer; {DATA Received – number to be “cubed” INFO Returned – number cubed LOGIC – received integer is multiplied by itself 3 times } BEGIN Cube := X * X * X; END;
Using functions in programs • Using assignment statementsA:= 5;B := Cube(A); • Using arithmentic expressionsA :=5;B := 3 * Cube(A) + 2 ; • Using output statementsA := 5;Writeln (Cube(A):17);
Demo of Variable andValue Parameters • DEMO PROGRAM…
How modules help us to analyse and solve problems • Using procedures relates to modular programming techniques • Each module can be tested separately to see if it is producing the desired result using pseudo code • Modules can then be transferred to Pascal (or other language)
Pseudo code – 1st Hit Program FtoC {Fahrenheit to Centigrade} Declare variables and modules Begin main program IntroScreen GetUserInput DoCalculation OutputResult End Main program
Module Specification IntroScreen {DATA Received – None INFO Returned – None LOGIC – Clears screen then displays user instructions } GetUserInput {DATA Received – None INFO Returned – Temperature in F LOGIC – Get user input (integer or real) } • By examining the module specification we can see what parameters are needed (or not…!)
Pseudo code – (2nd hit) • IntroScreen 1.1 Clear the screen 1.2 Draw user input message • GetUserInput 1.1 Read input from user 1.2 Validate input as necessary
Summary • What defines a procedure • What defines a function • The use of procedures in modular programming • Value and Variable parameter passing • We looked at example programs