1 / 19

Pascal Programming

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”

sera
Télécharger la présentation

Pascal Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pascal Programming Functions, Procedures and Parameters Carl Smith National Certificate Year 2 – Unit 4

  2. 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…!).

  3. 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}

  4. 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.

  5. 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.

  6. 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.

  7. 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 ?

  8. 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.

  9. 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}

  10. 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.

  11. 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 }

  12. 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;

  13. 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);

  14. Demo of Variable andValue Parameters • DEMO PROGRAM…

  15. 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)

  16. Pseudo code – 1st Hit Program FtoC {Fahrenheit to Centigrade} Declare variables and modules Begin main program IntroScreen GetUserInput DoCalculation OutputResult End Main program

  17. 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…!)

  18. 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

  19. 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

More Related