Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

User Defined Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS0004: Introduction to Programming Repetition – Do Loops.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Chapter 6: User-Defined Functions I
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 7: User-Defined Methods
Guide To UNIX Using Linux Third Edition
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Introduction to Methods
Python quick start guide
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
Object Oriented Programming Criteria: P2 Date: 07/10/15 Name: Thomas Jazwinski.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
CSC111 Quick Revision.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Object Oriented Systems Lecture 03 Method
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
User Defined Functions
6 Chapter Functions.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Chapter 6: User-Defined Functions I
Week 4 Lecture-2 Chapter 6 (Methods).
In C Programming Language
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Methods/Functions.
CPS125.
Presentation transcript:

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

Functions The 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

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

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. P2

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. P2

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

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

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

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

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

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

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

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

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

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

Example of Procedural Programming This is an example of a procedural program I have written in C: P2

Example of Procedural Programming This is an example of a procedural program I have written in Java: P2