19
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Embed Size (px)

Citation preview

Page 1: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Procedural Programming

Criteria: P2

Task: 1.2

Thomas Jazwinski

Page 2: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Functions• A function is a method in procedural programming that returns a

value.

• Functions are separate to the main method and can be called from any other method.

• They can also have parameters passed to them to use in the body of the function.

• Once a function has been made it can be called as many times as you need it.

• The syntax for a function is:

Modifier ReturnType FunctionName (Parameters)

{

Body of function

}P2

Page 3: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

FunctionsThe return type of this function in java is double, the name of the function is areaOfCircle and the parameter in the function is called radius.

This function is in C and calculates the average of 3 numbers. The return type for this function was also double and the parameters were num1, num2 and num3.

P2

Page 4: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Procedures• A procedure is a method in procedural programming which does not

return a value back to the main method.

• A procedure is also called from the main method and parameters can be passed to them.

• Like a function these parameters can also be used in the body of the method.

• Procedures can also be used as many times as you need them

• The syntax for a procedure is:

Modifier Void ProcedureName (Parameters)

{

body of procedure

}P2

Page 5: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Procedures• This is an example of a procedure

in Java. The return type for this, and all procedures is ‘void’ because it does not return a value. Calling a procedure in java looks like this:

• This is an example of a procedure in C which outputs the sum of 5 numbers. Any statements can be used in a procedure including iteration which you can see in this method.P

2

Page 6: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Programming Libraries

• Each procedural programming language has their on programming library which stores all of the pre-defined functions available to the user.

• The C library has different packages with different functions within that.

• Whereas Java is more complicated as it has packages which have different classes within them which have the pre-defined methods inside it.

• If the user of the procedural programming language has the correct packages then they can use the functions as many times as they want in any of the programs they create.P

2

Page 7: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Programming Libraries

• An example of a pre-defined function I have used in Java is the ‘pow’ function from the math class which is in the java.lang package. This calculates the total of one number to the power of another number. I also used the ‘min’ function from the same math class which calculate the smallest number out of a set of numbers.

• An example of a pre-defined function which I have used in C is the print function and the scan function. These pre-defined functions are in the stdio.h of the C library. The print function is used to output onto the screen and the scan function is to take in a user input.

P2

Page 8: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Global Variables• A global variable is a variable that is declared outside of a method

but inside the class of a program.

• This means that the variable can be used in any method that is also within the class where the variable is declared, this is called a global scope.

• This is very useful for modularity as it allows a variable to be used in any of the methods

• This also means that the value stored in the variable can be changed at any time.

• Also, global variables need to be initialised with a value in order for them to work.

P2

Page 9: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Global Variables

• As you can see on the right I have used a global variable in Java called ‘c’ and I have then used it in different methods in this program. A problem with using a global variable like this is that when the value of c has changed it will then be different for the time it is used.

P2

Page 10: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Local Variables

• A local variable is a variable that is declared within a method.

• This means that it can only be used inside the method it is declared it because it has a local scope.

• This is useful for when a variable is only going to be used once, or is only being used for one purpose.

• Local variables do not need to be initialised but are only given the default value for the data type in that programming language.

P2

Page 11: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Local Variable

• On the left you can see an example of a local variable being used in C. There are 3 local variables in this code called ‘a’, ‘b’ and ‘c’.

P2

Page 12: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Static Variables

• A static variable is a variable that can keep the same data value throughout the extent of the program.

• These variables are more commonly used in object oriented programming.

• This is an example of a static variable in Java:

P2

Page 13: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Parameters• Parameters are variables that are used in procedures and functions.

• The value of the parameter is decided in another method and passed to the function or procedure.

• This is called parameter passing.

• This is an example of where I have used parameter passing. As you can see the parameter in the method is called radius and the value that is being passed to the parameter is 10 which is then used inside the method.

P2

Page 14: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Parameters

• In functions there is also a value that is returned back to the method which called it.

• This is done using the ‘return’ statement.

• In this code, the value of ‘area’ is calculated using the parameters and then the value is passed back to the main method.

P2

Page 15: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Debugging

• Debugging is the when errors are identified and fixed within a program.

• Possible errors could be syntax errors that stop the program running or run-time errors that are mistakes which do not stop the program from running.

• These errors can be identified through using break points which stops the program at certain points.

• You can then look at variables that are being used to see their value and watch how they change at different steps in the program.

P2

Page 16: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

• As you can see there is an example of where I have debugged my program by using break points and a variable watch to see

how the value of it changes (used on the ‘counter’ variable.

Debugging

P2

Page 17: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Example of Procedural Programming• This is an example of a procedural program I have written in

C:

P2

Page 18: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

Example of Procedural Programming• This is an example of a procedural program I have written in

Java:

P2

Page 19: Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski

P2