Procedures and Functions - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Procedures and Functions

Description:

Procedures and Functions Computing Module 1 What is modular programming? Most programs written for companies will have thousands of lines of code. – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 26
Provided by: nba96
Category:

less

Transcript and Presenter's Notes

Title: Procedures and Functions


1
Procedures and Functions
  • Computing Module 1

2
What is modular programming?
  • Most programs written for companies will have
    thousands of lines of code.
  • Rather than writing one large program the problem
    is split into lots of smaller problems.
  • A separate module is written to solve each of
    these problems.
  • A module should be less than 100 lines of code.

3
Advantages of Modular Programming
  • Modules can be used in several different parts of
    a program, or even in several different programs.
  • Easier to understand and debug the code.
  • Program maintenance is easier.
  • Several programmers can work on the same program,
    each is given different modules to work on.

4
Advantages of Modular Programming continued
  • More experienced programmers can be given more
    complex modules to work on.
  • Modules can be tested individually, which means
    the time taken to get the whole program working
    is reduced.
  • It is easier to monitor and control the project.

5
Subroutine
  • A subroutine is written for each module. There
    are two types of subroutine
  • Procedure
  • Function

6
Function
  • A function is a subroutine that returns a value.
  • A function must form part of an expression.
  • Pascal has some built-in functions, though you
    can also create your own functions.

7
The sqrt function
  • sqrt is a built-in function that returns the
    square root of a number
  • sqrt real ? real
  • Examples of sqrt being used
  • answersqrt(9)
  • finalsqrt(total)
  • The first example would store the value 3 in the
    answer variable. The second would store the
    square root of the contents of the total variable
    in the variable called final.

8
More built-in functions
  • There are lots of other built-in functions.
  • The table shows a few examples.

sqr Takes a number returns the square of that number
round Takes a number and rounds it to nearest whole number (real ? integer).
length Takes a string and returns an integer value representing the number of characters in the string.
9
Creating a new function
  • As well as using the built-in functions, you can
    create your own functions.
  • In this example a factorial function has been
    written.
  • Remember Factorial of 5 is 54321 120.
    Factorial of n is n (n-1).. 1. Factorial of
    1 is 1.

10
Factorial Function
  • function factorial (n integer) integer
  • var count, product integer
  • begin
  • product1
  • for count 1 to n
  • do product product count
  • result product
  • end

11
Factorial Function
  • The factorial function can now be used in a
    program.
  • program project
  • var number, answer integer
  • begin
  • write(Enter a number)
  • readln(number)
  • answerfactorial(number)
  • writeln(The factorial of , number, is ,
    answer)

Note this is only part of a program
12
Procedure
  • A procedure is a subroutine that does not return
    a value.
  • Pascal has some built-in procedures, e.g. write,
    readln - though you can also create your own
    procedures.

13
Creating a new procedure
  • In this example a procedure countdown that takes
    a number and prints all the numbers between that
    number and zero has been written.
  • So given the number 5, 543210 will be printed.

14
Countdown procedure
  • procedure countdown (n integer)
  • var count integer
  • begin
  • count n
  • repeat
  • write(count)
  • countcount-1
  • until count 0
  • end

15
Countdown procedure
  • The countdown procedure can now be used in a
    program.
  • program project
  • var start integer
  • begin
  • write(Enter a number)
  • readln(start)
  • countdown(start)
  • countdown(start5)

16
Variable scope
  • The scope of a variable refers to where a
    variable can be used in a program.
  • A variable can be either global or local.
  • A global variable can be used anywhere in a
    program.
  • A local variable can only be used in the
    subroutine that it was declared in.

17
Local Variables
  • In the function Factorial count and product are
    local variables.
  • The scope of count and product is the function
    Factorial. They can only be seen inside this
    function and cannot be seen by anything outside
    the function.

18
Global Variables
  • In program Project number and answer are global
    variables.
  • They can be seen anywhere in the program. They
    can also be seen from inside the Factorial
    function.

19
Local vs Global
  • Whenever possible local variables are used.
  • Programs that use local variables instead of
    global variables tend to have fewer errors.
  • This is because each module is more
    self-contained what happens in one module does
    not effect any other module.

20
Advantages of Local Variables
  • Modules that use local variables are easier to
    reuse in other programs.
  • It is easier to find errors in a self-contained
    module as each module can be tested separately.

21
Parameters
  • If a program is using local variables then there
    needs to be a way to allow the modules to
    communicate (to share data) with each other.
  • Values required by subroutines are passed to the
    routine by using parameters. A subroutine can
    have any number of parameters.
  • The number of parameters passed to the
    procedure/function (actual parameters) must match
    the number of parameters stated in the
    procedure/function declaration (formal
    parameters).

22
Parameter example
  • The procedure Countdown takes one parameter an
    integer.
  • When it is called it is passed one integer value.
  • In the examples on the right start is passed as a
    parameter to the countdown procedure. The second
    time countdown is called, start5 is passed as a
    parameter.

23
Passing Parameters
  • There are two methods of passing parameters
  • Passing by value
  • Passing by reference (also called passing by
    variable)

24
Passing by Value
  • When a parameter is passed the value of the
    parameter does not change in the
    procedure/function.
  • In this example the actual value of start is
    passed to the memory location n.

25
Passing by Reference
  • calchours(noofhours)
  • procedure calchours(var h integer)
  • When a parameter is passed the value of the
    parameter can change in the procedure/function.
  • In this example the memory address of noofhours,
    not the value, is passed to the procedure
    calchours. This means that if the value of h is
    changed in calchours then the value of the
    noofhours will also change.

Notice that the word var is used in the formal
parameter
Write a Comment
User Comments (0)
About PowerShow.com