3. User Defined Functions

Slides:



Advertisements
Similar presentations
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
Advertisements

Object-Oriented Programming and Classes. OOP / Slide 2 Basic, built-in, pre-defined types : char, int, double, … Variables + operations on them int a,
Mark Dixon Page 1 12 – Functions and Modules. Mark Dixon Page 2 Questions: Parameters Name a parameter in the following code: Sub Move(ByRef obj, ByVal.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Homework Assignment #3 J. H. Wang Apr. 19, 2007.
Mark Dixon 1 14 – Functions and Modules. Mark Dixon 2 Questions: Parameters Consider the following code: Sub Move(ByRef obj, ByVal dist) obj.style.posLeft.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 03 – Information Processing. Mark Dixon 2 Questions: Events Consider the following code: a) How many unique events does it contain? b) Name.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CMSC 2021 Software Development. CMSC 2022 Software Development Life Cycle Five phases: –Analysis –Design –Implementation –Testing –Maintenance.
Mark Dixon 1 Soft051 Examination Sample Questions.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
CS0004: Introduction to Programming
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Operator Overloading.
Functions + Overloading + Scope
Subprograms Functions Procedures.
Topic: Classes and Objects
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Chapter 5 Functions for All Subtasks 1
Python Programming Module 3 Functions Python Programming, 2/e.
04 – Information Processing: Expressions, Operators & Functions
Translating From One Unit to Another
The CONST definition CONST Pi = , City = ‘New York’;
Building Java Programs
CO1401 Programming Design and Implementation
TEMPERATURE CONVERSION
Review Session.
Temperature Activity.
Chapter#8 General Procedures
Functions Chapter 20.
Chapter 4 - Visual Basic Schneider
Building Java Programs
CSC1201: Programming Language 2
Introduction to Data Structures
Functions, Procedures, and Abstraction
Java's Math class Method name Description Math.abs(value)
Subprograms and Programmer Defined Data Type
A Mathematical Programming Language
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter#8 General Procedures
Data Structures and Algorithms for Information Processing
Function Procedures.
JavaScript MCS/BCS.
Computer Science
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Programming Control Structures
Multiple Files Revisited
11 – Functions and Modules
More Functions Chapter 20.
Loops and Simple Functions
Computer Science
1.7 – Dimensional Analysis
Lab6 PROGRAMMING 1 metheds chapter5.
Introduction to Functions
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Functions, Procedures, and Abstraction
Lecture 2 – Abstract Data Type (ADT)
Lecture 2 - Names & Functions
Presentation transcript:

3. User Defined Functions

Procedures and Functions Both Procedures and Functions Group of statements Identified by unique name mirror real life activities Procedures – just do something Functions – return a value

Built in Functions: SQR Sqr – gives square a number function Sqr(X: Extended): Extended; Examples Sqr(4) 16 Sqr(3) 9 Sqr(2) 4 X: Extended Sqr Extended

Built in Functions: Random Random – generates random numbers function Random(): Extended; function Random(Limit: Integer): Integer; Random Extended Limit: integer Random integer

User Defined Functions Syntax very similar to procedure definition: function <name>(<parameters>): <type>; begin ... Result := <value> end; Where <name> represents function’s name you choose <parameters> represent information needed <type> represents the return type <value> represent the return value

Exercise: code to diagram Draw function diagram for the following code: function Thing(): double; function Miles(km: real): real; function Double(num: integer): integer;

Exercise: diagram to code Generate the code for the following diagrams: Mins: integer Minutes integer Hours: integer Pounds: integer Euros integer

Example: Functions Demo (form)

Example: Functions Demo (code) implementation ... const MinutesPerHour = 60; const KMperMile = 1.6; function Minutes(Mins: integer; Hours: integer): integer; begin Result := Mins + (Hours * MinutesPerHour); end; function Miles(km: real): real; Result := km / KMperMile; procedure TfrmFuncts.cmdMinutesClick(Sender: TObject); var tmpMins: integer; var tmpHours: integer; var resMins: integer; tmpMins := StrToInt(txtMins.Text); tmpHours := StrToInt(txtHours.Text); resMins := Minutes(tmpMins, tmpHours); lblMinutes.Caption := IntToStr(resMins); procedure TfrmFuncts.cmdMilesClick(Sender: TObject); var tmpKM: real; var resMiles: real; tmpKM := StrToFloat(txtKM.Text); resMiles := Miles(tmpKM); lblMiles.Caption := FloatToStr(resMiles); end.

Meet George Common Boa Constrictor (Boa Constrictor Constrictor).

George (cont.) Problem: Solution Difficult to keep Require temperature and humidity controlled environment Much of the literature is from the US Temperature in Fahrenheit Solution Need a program to convert from Celsius to Fahrenheit

George (cont.) To convert from Fahrenheit to Celsius: To convert from Celsius to Fahrenheit:

George (cont.) The code: function FtoC(F: double): double; begin end;