Introduction: During this assignment i have been asked to discuss the key features of procedural programming and how and why it is used.
A: pre-defined functions, Predefined functions are methods which are already defined or in other word are built in the library of the program your using. For e.g. java have many pre-defined functions. I will give a few examples….
String sName;
SName = sName.toUpperCase ();
This is a pre-defined function within java as it is automatically tells the program to put the text into uppercase letters. This phase is already built into java therefore it is a pre-defined function. Alternatively another pre-defined function is….
SName = sName.toLowerCase ();
This is the exact opposite to the first phase as this time it is telling the program automatically to turn the letters into lower case letters. Both examples are pre-defined functions as they are already built into the program. There are many predefined functions.
B; Local variables. A local variable is a variable that is declared in the body of a method that you make. They you can use that variable only in the method you made. If you have other methods in the class they will not even be aware that the variable even exists. Here is an example of a program using a local variable…..
Public class HelloWorld
{
String sMyName;
sMyName="Husnain";
System.out.println(sMyName);
This is an example of a local variable. You don’t specify the static on declaration for a local variable, but if somehow you do the compiler will read the program and in return send you an error message and will refuse to compile your program.
C; Global variables;
int g_nX; // global variable
int main()
{
int nY; // local variable nY
// global vars can be seen everywhere in program
// so we can change their values here
g_nX = 5;
} // nY is destroyed here
This is an example of a Global variable. Variables that are declared outside a block are known as Global Variables. Global variables have a program scope, in other words this means they can be accessed anywhere/everywhere within the program. The only way they can be destroyed is when the program ends as they are global. Unlike local variables Global variables can be accessed anywhere throughout the program. A global variable maybe used when you want to declare a variable that needs to be accessed by all parts of your program.
D; Parameter passing; A parameter is a type of variable that you can pass into a function. Variables are the listed parts of each methods declaration. Each parameter must have a unique name and have a defined data type. For e.g. when you first open up java you get this code..
Public static void main (String[]args)
This this a type of function that takes one argument, an array of strings that’s called args. Furthermore there is a tye of way you write stuff to get input back.. System.out.println(“HelloWorld”). To sum up you have a function that takes one parameter. The value of the parameter is the string “HelloWorld”.
E: modularity;
Modularity is a general concept which applies to the development of software. It allows individual modules to be developed, in many cases this is done with a standardised interface to allow modules to work and communicate properly. Each module works independent to each other. Large software systems are really hard and complex to build this is where modularity comes in to break down a large system into separate physical entities that in a nutshell make it easier and more straightforward to understand.
F: Procedure; Procedures are just small programs. They are sometimes known as sub-programs. The purpose of a procedure is to help a programmer avoid repition. A procedure start off with a begin and end up with end;. The procedure can also have its own variables which cannot be used the main program. In a nutshell a procedure is a section of a program that performs a specific task or in other words an ordered set of tasks for performing some sort of action.
G: Programming Libraries; There are many Libraries in programming. They each contain some unique functions and subroutines for a specific program. For e.g. when I was programming to make graphical sub boxes to appear I had to write a specific code before inputting my codes.
import,.javax.swing.*;
This code allowed me to have graphical boxes to pop up. When I typed this code it gave me access to a specific library. So I could use its functions and subroutines.
H: Programming paradigms; there are three standard programming paradigms that can be used to implement algorithms. The three paradigms are….
Procedural programming – this is a high level programming paradigm. This has a basic rhythm/sequence of statements. This is straightforward and simple way of programming as it does not get complicated.
Object orientated programming- this is more of a problem modelling. And has a specific way of structuring a program. This method contains data and methods.
Flow programming- flow programming involves chaining a sequence of smaller processes together and allowing a flow of information through the sequence.
There are many programming languages however some vary in the way they are written. Two examples of good programming languages are Java and C++.Here are two examples of the languages.
Public class HelloWorld
{
String sMyName;
sMyName="Husnain";
System.out.println(sMyName);
This is a example of java, as you can see this is a small and straightforward program. There are many advantages of using a program like java for instance..
I)
Advantages of Java....
Java is fast and easy to learn.
Java is object-orientated meaning you can create modular programs and re-usable codes.
Java is secure
And lastly java is platform independent……
Here are some advantages of c++
Great program for people who are interested in making games.
It is a good language to learn enabling you to limitless benefits
It is relatively clear and mature language
C++ is a great language however it may take more time to learn that a program like java as there is a lot more to learn
J) There are many samples of codes, here I will explain a few.
Integer: Known as an int int the java program it is used to declare an whole number this is good for various things from age to birthyear.
Boolean: Boolean is used to declare a true or false statement, in other words Boolean is a data type used to hold true or false data this cannot be used with math calculations.
Char: This is used to declare a single character it cannot hold more than a character, this is good to declare sex (M or F) and many other things.
Data types:
byte: this can hold numbers in the range off -128 to +127
short: this can hold numbers ranging from -32,786 to + 32,767
long: this can hold number in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
IF) IF statements are used alot in java, this is a method of asking the program a question. for e.g. if you have a requested a user to use numbers instead of characters then you may use an IF to detect any characters and to tell users to use numbers, An example of an if statement....
if(sAge.contains("A")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
This is telling the program to see if the letter A is found then to send an error message to0 the user telling them to write in numbers not letters.
Else and Else if: Instead of using IF twice you can use IF then Else this make your program look better and more efficient. Else follows after a IF and is executed when a Boolean is expression is proven false.
Here is an example....
if(sAge.contains("A")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
}else if(sAge.contains("B")){
sAge = JOptionPane.showInputDialog(null,"please input your name in numbers " + sName,"Enter your Age please...",JOptionPane.WARNING_MESSAGE);
As you can see this is an Else statement this is just a shorter way for the program to read through and find an expression which proves the Boolean false. If the first condition does not work then the program will check the second one.
FOR: The for statement provides a good and compact way of repeating a range of values, this is often referred to as a for loop by programmers as it repeatedly loops until a certain condition is satisfied.
for (initialization; termination;
increment) {
statement(s)
}
This is an example of a For or for loop.
WHILE LOOP: The while loop is used to control a structure and it allows you too repeat a task a number of times. The while loop keeps repeating until the condition is satisfied, here is an example...
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
This is basically telling the program to keep looping until the value of x is near 20, this means it will only loop till the number 19.