E0001 Computers in Engineering Procedures: subprograms and functions.

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Sub and Function Procedures
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
An Introduction to Programming with C++ Fifth Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
CP Lecture 11 Functions and advanced procedures.
VBA Modules, Functions, Variables, and Constants
CP Week 10 Modularising programs using Procedures.
Example 2.
CS 201 Functions Debzani Deb.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
Functions.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Why to Create a Procedure
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
E0001 Computers in Engineering
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Chapter 6: Functions Starting Out with C++ Early Objects
Input, Output, and Processing
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Software Development Topic 3 High Level Language Constructs.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
1 E0001 Computers in Engineering Built in Functions.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Python Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
User defined functions
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
INTRODUCTION TO PROGRAMMING. Program Development Life Cycle The program development life cycle is a model that describes the stages involved in a program.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Topics Designing a Program Input, Processing, and Output
Functions and Procedures
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
CS285 Introduction - Visual Basic
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Variables in C Topics Naming Variables Declaring Variables
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

E0001 Computers in Engineering Procedures: subprograms and functions

Readings and tutorial n Schneider sections 4.1 and 4.2 –practice problems and exercises 4.1 –practice problems and exercises 4.2 n For Practice –Schneider practice problems 3.4; numbers 57,60,62,66,68 –additional problems provided by the tutor

This lecture: n subroutines (subprograms) n user defined functions n local and global variables

Exam 2% n Week 11 n 20 multiple choice questions on –IF THEN and SELECT CASE –INPUT, PRINT, READ AND DATA –SUBS AND FUNCTIONS –PRINT USING –GENERAL KNOWLEDGE

Procedure n group of self-contained statements (modules that perform a specific task) e.g. displaying a menu n suited to structured programming –break problem into small piece –each piece becomes a procedure –reuse procedures whenever needed

Kinds of procedures n two kinds of procedures –subprograms –functions user defined built in

Program ‘MAIN program to calculate volumes REM length, width,depth INPUT l,w,d [CALL] volume (l,w,d,v) PRINT v END SUB volume (l,w,d,v) v=l*w*d END SUB lwdlwd MAIN PROGRAM VARIABLES Sub program variables lwdv v0v0

Subroutine n [CALL] subprogram [(argumentlist)] –CALL average (a,b,c,avg) –average a,b,c,avg n SUB subprogram (parameterlist) –SUB average (x,y,z,avg) sub program name must match name used in CALL statement arguments and parameters must match in number, order and type of variables BUT NOT NECESSARILY NAME argument list of CALL statements can also contain data (numbers) and arithmetic expressions –

For example CALL draw (x,y$,z) will look for a sub program call “draw”. The name on the subprogram must match EXACTLY Valid subprogram statements: SUB draw (x,y$,z) SUB draw (a,b$,z) SUB draw (number, name$, age)

CALL add (2,3) SUB add (x,y) PRINT “The sum of”;x;”and”;y;”is”;x+y END SUB SUB add (x,y) PRINT “The sum of”;x;”and”;y;”is”;x+y END SUB MAIN PROGRAM CALL add (2,3) A = 4 b = 5 CALL add (A, b) CALL add (b+1, A*5) SUB add (x,y) The sum of 2 of 3 is 5 The sum of 4 and 5 is 9 CALL add (A,b) SUB add (x,y) PRINT “The sum of”;x;”and”;y;”is”;x+y END SUB The sum of 6 and 20 is 26 CALL add (b+1, A*5) SUB add (x,y)

Subprograms n executed with (implied) CALL statement n designed to perform a particular task n section of code that is executed from another section of code n can be called from any place in a program n returns control to statement that called it n returns 0 or more values n DO NOT USE GOSUB!!!

This is the main section of code entered in the usual way. It calls a subroutine called “trivial” To enter the subroutine use the Edit Pull Down menu - NEW Sub

Enter the sub name only, not variables

The SUB name and END SUB are written automatically. If necessary add the sub parameters now and type the body of the sub. The F2 key will show you other subs in the program and allow you to select which “page”you wish to view.

MAIN PROGRAM x=2 PRINT “mainx=“;x CALL trivial PRINT “mainx=“;x CALL trivial PRINT “mainx=“;x END SUB trivial PRINT “trivial x=“;x x = 3 PRINT “trivial x = “;x END SUB

Local Variables n Local variable - variables used in subroutines or functions which are not passed into the sub or function are said to be local. The values of local variables cannot be used by other subs or functions or by the main body of the program unless passed in the argument list

Global Variables n Global variable - may be used anywhere in the program n declared with a DIM SHARED statement –DIM SHARED argumentlist –DIM SHARED a,b,c,avg a,b,c,avg are available to all functions and subs without being passed in the argument list

Add the DIM SHARED statement. This means that x is now a GLOBAL variable available to all sub, functions and the main routine

This now changes the output significantly. The value for x is shared between the sub and the main routine

Parameter Passing n Items in the parentheses of CALL - arguments –arguments can be CONSTANTS VARIABLES EXPRESSIONS n Items in the parentheses of SUB - parameters –parameters MUST be variables n Names of arguments & parameters NEED NOT MATCH n Position, type and Number MUST match

DECLARE statements n DECLARE SUB name (variables) n comes at the beginning of the program n done automatically by qbasic on FIRST SAVE

DECLARE DONE AUTOMATICALLY ON FIRST SAVE

DIM SHARED and data types n DIM can still be used to define data types n DIM SHARED x AS SINGLE, Y AS DOUBLE, a AS STRING etc n any variable NOT dimensioned as SHARED will be LOCAL

What is the output? Call ConvertMoney (“Mark”, 5,.6165) End Sub ConvertMoney (currency$, dollars, Factor) money = dollars * Factor PRINT dollars; “dollars equal to”; money, currency$; “s” END SUB

What is the output? ‘main program M=5 n=8 CALL calc (M, n, total) PRINT M, n, total END ‘sub program SUB calc (sum, A, B) sum = A + B END ANS is 8 8 0

Remember “built in” Functions n Look at the statement which uses a built in functions x = SQR(y)total = LEN(“jack”) variable = Function(arguments) n returns ONE value into variable i.e. after the function is executed the variable has the “answer” or the returned value

Write a function to calculate the volume of a cone - use diameter and height as inputs REM main program INPUT diam, height volume = Cone (diam, height) PRINT volume END FUNCTION Cone (d,h) r = d/2 ….. Cone = 3.14 * r^2*h/3 END FUNCTION

Take a closer look at the main program REM main program INPUT diam, height volume = Cone (diam,height) PRINT volume END n diam and height carried as parameters into function Variable = function (arguments) expression is used to call function n print variable which has the returned value from the function

Take a closer look at the Function FUNCTION Cone (d,h) r = d/2 ….. Cone = 3.14 * r^2*h/3 END FUNCTION Function name matches the name in expression volume = Cone (diam, height) number and type of variables match I.e diam d and height h function name =expression calculated value of expression stored in function name and returned to main

Comparison of SUBs and FUNCTIONS INPUT d, h CALL cone(d,h,v,s) PRINT v,s END SUB cone (x,y,z,s) r = x/2 z = calc for volume s = calc for surface area END SUB INPUT diam, height volume = Cone (diam, height) PRINT volume END FUNCTION Cone (d,h) r = d/2 Cone = 3.14 * r^2*h/3 s = calc for surface ares PRINT S END FUNCTION d=>x; h=>y; v=>z; s=>s x=>d; y=>h; z=>v; s=>s diam=>d; height=>h d=>diam; height=>h; Cone=>volume

DIFFERENCES? n SUBs n can take any number of variables, constants and expressions to sub n returns one or more variables n use extra arguments to return more to MAIN n executed with a [CALL] statement n FUNCTIONS n can take any number of variables, constants and expressions to sub n returns ONE value n returns value in function name with an expression n executed (called) with an EXPRESSION

User-defined Functions n customised to suit user n call with an expression n returns a single value n names for functions follow the same rules as variables n used for repetitive mathematical functions n value returned with function name (expression)

To create a Function n In Qbasic - Edit, new function OR n on a new line in Qbasic type –FUNCTION name and then ENTER e.g. function volume (this will also work for subs) –a new screen will appear with FUNCTION and END FUNCTION –F2 will return to MAIN program

RULES n Can call a FUNCTION from within a SUB n Can call a SUB from within a SUB (but not recommended) n Can call a SUB from within a FUNCTION (but bad structure) n number, type and order of parameters and arguments MUST match but not necessarily the name

Here is a program to determine the area of a rectangle. Rewrite it using a function CLS READ length, width LET area = length *width PRINT “Area is”;area DATA 4,5 CLS READ length, width a = rect (length, width) PRINT “Area is”;a END FUNCTION rect (l,w) rect = l*w END FUNCTION

Write a program with a function that converts Celsius to Fahrenheit. F = 9/5C +32 INPUT celsius f = convert (celsius) PRINT f END FUNCTION convert (c ) convert = 9/5*c + 32 END FUNCTION INPUT celsius PRINT convert (celsius) END FUNCTION convert (c ) convert = 9/5*c + 32 END FUNCTION