Download presentation
Presentation is loading. Please wait.
1
Programming paradigms
Section 3.5 Programming paradigms Part 2.
2
Standard Programming Techniques
In modern programming, problems are broken into modules that can be programmed easily. Each module is a solution to an individual problem and each module has to interface with other modules. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
3
Parameters, & local and global variables
A local variable: This is a variable declared within a procedure or a function of a program and which cannot be accessed by code outside that procedure or function. A Local variable can only be accessed by code within the procedure or the function where it is declared. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
4
Parameters, & local and global variables
Cont~d A global variable This is declared within the main program or within a public module of a program. It can be accessed from anywhere within the source code of the program. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
5
Parameters, & local and global variables
Cont~d A function uses PARAMETERS to pass values to the calling module. Consider a VB6 program to calculate the perimeter of a rectangle. Public Function PerimeterOfRectangle (X As Integer, Y As Integer) As Integer X = 2 * X Y = 2 * Y PerimeterOfRectangle = X + Y End Function Formal parameters VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
6
Parameters, & local and global variables
Cont~d When this function is called, it is given actual parameters. Values must be stated. A=4 B=5 Perimeter = PerimeterOfRectangle(A, B) Alternatively you can call the function by giving the values directly e.g. Perimeter = PerimeterOfRectangle(4, 5) Formal parameters VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
7
Passing parameters We have two different ways of passing parameters.
Passing by value Passing by reference When a parameter is passed by value, Only the value of the parameter is passed to the procedure called. Value of the parameter can be manipulated by the procedure called. When the procedure called is terminated the new value is discarded and the value of the parameter in the calling procedure returns to the original value VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
8
Passing parameters When parameters are passed by reference,
The parameter is stored in the original location and only a pointer (a reference to the parameter’s memory address) is passed to the procedure called. Any changes made to the reference passed to the procedure called, will remove the value of the parameter at the original location. When the procedure called is terminated the new value is available to the calling procedure VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
9
Examples: Passing by value Passing by reference
Procedure1 Declare a a = 5 Call Procedure2 (a) Display a EndProcedure1 Procedure2 (ByVal b) b = b + 2 EndProcedure2 The output of the parameter will be the display of value 5 Passing by reference Procedure1 Declare a a = 5 Call Procedure2 (a) Display a EndProcedure1 Procedure2 (ByRef b) b = b + 2 EndProcedure2 The output of the parameter will be the display of value 7 Both of these examples are coded in Visual basic VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
10
Examples: Global variable The output will be the display of value 7
Main Declare a a = 5 Call Procedure2 Display a EndProcedure1 Procedure2 ( ) a = 7 EndProcedure2 The output will be the display of value 7 Local variable Main Declare a a = 5 Call Procedure2 Display a EndProcedure1 Procedure2 ( ) a = 7 EndProcedure2 The output will be the display of value 5 VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi Also coded in Visual basic
11
Stacks and Procedures When a procedure or function is called, the computer needs to know where to return to when the function or procedure is completed. i.e. , the return address must be known. Furthermore, functions and procedures may call other functions and procedures which means that several return addresses be stored and they must be retrieved in the right order. This can be achieved by using a stack. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
12
How a stack can be used to handle procedure calling & parameter passing
The return address is placed on stack along with the values of parameters when a procedure is called. If that procedure in turn calls another procedure the returning addresses are placed on the stack in the order so that most recent return address is placed on the top of the stack. As the procedure(s) execute the parameters are read off the stack The return values will be passed to the statements by taking the addresses out of those statements out of the stack. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
13
Example: Consider the following Module 1 Module 2 End End End
Main Prog Module 1 CALL MODULE 1 100 Module 2 CALL MODULE 2 130 End End End In this example, the numbers represent memory addresses. The addresses will be stored in the stack each time a function is called and will be removed from the stack each time a return instruction is executed. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
14
Example Contd STACK (Last in First Out) 130 100
When module 1 is called by the main program, the address 100 is pushed onto the stack Then when module 1 calls module 2, the address 130 is also pushed When module 2 returns a value to module1, the address is popped off the stack. (The pointer is now back at 100) When the module1 returns the value to the main program, then the location 100 is popped and the pointer is put at NULL STACK (Last in First Out) 130 100 VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
15
Object Oriented Programming
Object oriented languages (like JAVA, Eiffel, Smalltalk, etc) have classes and derived classes, and use concepts of encapsulation, inheritance and polymorphism. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
16
Classes, sub classes and super classes
In the real world, you'll often find many individual objects all of the same kind. E.g. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. Therefore A class is the blueprint from which individual objects are created. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
17
Classes, sub classes and super classes
Classes can be derived from other classes. The derived class is called a subclass. The class from which it's derived is called the superclass. The following figure illustrates these two types of classes: A subclass inherits its state and behavior from all of its ancestors. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
18
Encapsulation (data hiding)
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition. Only the object's own methods can directly manipulate its fields. Object method Object DATA method method An object cannot manipulate the data directly. Object VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
19
Take an example of a class called Rectangle
To calculate the area, we CAN’T do it like this area : = myRectangle.width * myRectangle.length; where myRectangle is an object of type Rectangle Class Rectangle must provide a suitable method in order to calculate the area of myRectangle. It can do this when an instance of the class is instantiated. The class must be provided with the methods to calculate the length and width, which can then be used to calculate the area. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
20
Example Contd. You can use the following methods to calculate the length and width integer getWidth( ) { getWidth := width; }//end of getWidth method. integer getLength( ) { getLength := length; }//end of getLength method. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
21
Example Contd. myRectangle can now use these methods to get at the width and length. However, it cannot change their values. To find the area we can write myWidth := myRectangle.getWidth( ); myLength := myRectangle.getLength( ); area := myWidth * myLength; To find the perimeter we can write myPerimeter := 2 * (myWidth + myLength); VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
22
Inheritance Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. It allows the re-use of code and the facility to extend the data and methods without affecting the original code. Inheritance is the ability of a class to use the variables and methods of a class from which the new class is derived. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
23
Example of inheritance
Consider the class Person that has data about a person's name and address and the methods outputData( ) that outputs the name and address, getName( ) that returns the name and getAddress( ) that returns the address. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
24
Example of inheritance Contd.
If we now want a class called Employee that requires the same data and methods as Person but also needs to store and output an employee's National Insurance Number (NIN). We don’t need to rewrite the contents of Person class. We simply create a subclass called Employee which inherits the data and methods of class Person. Person Employee Name Address NINumber outputData()getName() outputData()getNINumber() VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
25
Polymorphism The fact that when two or more classes that are inherited from a parent class, can implement an inherited method differently is called polymorphism. From the previous example, both the Person class and the Employee class have the method called outputData(). If myPerson is an instance of Person class, we can use myperson.outputData() To use the outputData() method of Employee class, we can say myEmp.outputData(). Where myEmp is an instance of the employee subclass. VCN CIE COMPUTING 9691/3 ::: Compiled by Benjamin Muganzi
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.