CP1020 - Lecture 11 Functions and advanced procedures.

Slides:



Advertisements
Similar presentations
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
Advertisements

VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
More about classes and objects Classes in Visual Basic.NET.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
CP Week 10 Modularising programs using Procedures.
Example 2.
Chapter 4 - Visual Basic Schneider
CP Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation.
CP1020 Week 5 Selection Continued. CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements.
Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
IE 212: Computational Methods for Industrial Engineering
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
E0001 Computers in Engineering Procedures: subprograms and functions.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Higher Grade Computing Studies 3. High Level Language Constructs Higher Computing Software Development S. McCrossan 1 Simple Data Types Integer: An integer.
Please log on The. AN INTRODUCTION TO ‘Python is a high-level, general purpose programming language’ Python is one of the many programming languages.
22/11/ Selection If selection construct.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Top-down approach / Stepwise Refinement & Procedures & Functions.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Lesson 1. Security At the menu bar at the top you will see the word Tools. Click your mouse on Tools scroll down to Macro. Move the Mouse over and down.
31/01/ Selection If selection construct.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Aim: How can the metric system be used to measure cubic volume? Do Now: Write the formula for area. What is the area of a square that has the following.
Mark Dixon 1 Soft051 Examination Sample Questions.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Visual Basic I Programming
Chapter 9: Value-Returning Functions
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Using local variable without initialization is an error.
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Chapter 4 - Visual Basic Schneider
CS005 Introduction to Programming
If selection construct
Sub Procedures and Functions
If selection construct
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Functions continued.
The structure of programming
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
10.2 Procedures Passing Parameters 30/08/2019.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Programming Techniques
Presentation transcript:

CP Lecture 11 Functions and advanced procedures

Functions Standard functions that we can use e.g fSquareRoot = SQR(iNumber) in this example the value in iNumber is passed into the SQR function and the answer is returned to the variable fSquareRoot. QBasic allows you to create your own functions in a similar way to writing procedures.

Creating a function From the Edit menu select the New Function… option, a dialogue box will appear requesting a name for the function. A new code window opens up and your lines of code are written between SUB Function name (parameter list) and END SUB

Function example zA function that will determine the area of a triangle and return the answer to a variable Base Height Area = Base X Height 2

Function example Main programme fBase = 3 fHeight = 6 fArea = TriangleArea(fBase, fHeight) PRINT “the area of the triangle is “, fArea FUNCTION TriangleArea(fBottom AS SINGLE, fDrop AS SINGLE) TriangleArea = fBottom * fDrop / 2 END FUNCTION Parameters to pick up data Result returned through function name Result from function returned to this variable Arguments passed into function

Some rules governing functions zFunctions are similar to procedures in the way that data is passed to them either by value or by reference - same rules govern their use and behaviour - however, you should never alter their original values in a function zAs you use the function name to return the result to the main program you cannot create a variable in the function with the same name as the function FUNCTION Add(iNum1 As Integer) DIM Add AS INTEGER Declaration with same name is forbidden

Some rules governing functions & procedures zAny variables declared within a function or procedure are local - that is the calling program or any other procedure or function cannot see or use the values stored in these variables i.e. Any declared in green procedure cannot be seen in the main program (orange) or blue procedure.

Returning more than 1 value zTo return more than one value to the main program you must use a procedure and pass values by reference

An example zA procedure is required to determine the cost and volume of concrete to fill a trench for a building’s foundation. zThe procedure calculates these values based on the cost of concrete per cubic meter (£120) and the volume of concrete needed. height width length Vol = ht x wd x len

The calling program DIM fHeight, fLen, fWid AS SINGLE Dim fVol, fCost AS SINGLE INPUT “The height, Length and Width” ; fHeight, fLen, fWid ‘at this point fVol and fCost = 0 CALL Concrete(fHeight, fLen, fWid, fVol, fCost ) ‘at this point fVol and fCost have a value determined ‘by the procedure PRINT “the volume of concrete used is ”; fVol ;” m 3 “ PRINT “ at a cost of £”; fCost

The procedure SUB Concrete(…. fVol AS SINGLE, fCost AS SINGLE) fVol = fHeight * fLen * fWid fCost = fVol * 120 END SUB New values returned to main program by reference

A second example zlets extend our exam grading program to determine the exam grade and overall mark for a student zhere we will use 2 marks to determine a students overall mark, and what grade they get.

A procedure that passes several values DIM iTest1, iTest2, iTotal AS INTEGER DIM sMsgAS STRING INPUT “Enter your TWO test results” ; iTest1, iTest2 CALL Grade(iTest1, iTest2, iTotal, sMsg) PRINT “Your overall mark was “; iTotal PRINT sMsg Main program

continued SUB Grade(……, iTotal AS INTEGER, sMsg AS STRING) iTotal = iTest1 + iTest2 SELECT CASE iTotal CASE IS < 40 sMsg = “Sorry try harder” CASE 40 TO 59 sMsg = “well done you have a pass” CASE 60 TO 100 sMsg = “excellent result- you have a merit” END SELECT END SUB As iTotal and sMsg are receiving the arguments from the main program by reference then they will pass back new values

Questions 1 What is the difference between a procedure and a function? 3 Write a function that will determine which of the two numbers passed to the function is the largest. 4. Write the procedure call and procedure to determine which is the largest of two numbers and the difference between the two.