Download presentation
Presentation is loading. Please wait.
Published byToby Simmons Modified over 9 years ago
1
Procedures and Functions Computing Module 1
2
What is modular programming? Most programs written for companies will have thousands of lines of code. 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. 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 separate module is written to solve each of these problems. A module should be less than 100 lines of code. 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. Modules can be used in several different parts of a program, or even in several different programs. Easier to understand and debug the code. Easier to understand and debug the code. Program maintenance is easier. Program maintenance is easier. Several programmers can work on the same program, each is given different modules to work on. 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. 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. 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. 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 Procedure Function Function
6
Function A function is a subroutine that returns a value. A function is a subroutine that returns a value. A function must form part of an expression. A function must form part of an expression. Pascal has some built-in functions, though you can also create your own functions. 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 is a built-in function that returns the square root of a number sqrt: real real sqrt: real real Examples of sqrt being used: Examples of sqrt being used:answer:=sqrt(9);final:=sqrt(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. 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. These are examples of expressions
8
More built-in functions There are lots of other built-in functions. There are lots of other built-in functions. The table shows a few examples. 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. As well as using the built-in functions, you can create your own functions. In this example a factorial function has been written. In this example a factorial function has been written. Remember: Factorial of 5 is 5*4*3*2*1 = 120. Factorial of n is n * (n-1).. * 1. Factorial of 1 is 1. Remember: Factorial of 5 is 5*4*3*2*1 = 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; beginproduct:=1; for count := 1 to n for count := 1 to n do product := product * count; result := product end; The name of the function is factorial The value passed to the function The function returns an integer The value to be returned by a function is always stored in a variable called result.
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);answer:=factorial(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. 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. 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. 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. So given the number 5, 543210 will be printed.
14
Countdown procedure procedure countdown (n: integer); var count : integer; begin count = n; repeatwrite(count);count=count-1; until count = 0 end; The name of the procedure is countdown The value passed to the procedure
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(start+5);
16
Variable scope The scope of a variable refers to where a variable can be used in a program. 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 variable can be either global or local. A global variable can be used anywhere in a program. 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. 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. 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. 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. 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. 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. Whenever possible local variables are used. Programs that use local variables instead of global variables tend to have fewer errors. 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. 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. 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. 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. 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. 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). 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. The procedure Countdown takes one parameter – an integer. When it is called it is passed one integer value. 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, start+5 is passed as a parameter. In the examples on the right start is passed as a parameter to the countdown procedure. The second time countdown is called, start+5 is passed as a parameter.
23
Passing Parameters There are two methods of passing parameters: Passing by value Passing by value Passing by reference (also called passing by variable) 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. 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. In this example the actual value of start is passed to the memory location n.
25
Passing by Reference When a parameter is passed the value of the parameter can change in the procedure/function. 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. 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. calchours(noofhours); procedure calchours(var h : integer); Notice that the word var is used in the formal parameter
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.