Function: function procedures and sub procedures share the same characteristics, with

Similar documents
IS 320 Spring 96 Page 1 Exam 1. Please use your own paper to answer the following questions. Point values are shown in parentheses.

Relational Operators. > greater than < less than >= greater than or equal to <= less than or equal to <> not equal to = equal to

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Before We Begin. Introduction to Computer Use II. Overview (1): Winter 2006 (Section M) CSE 1530 Winter Bill Kapralos.

Excel VBA Variables, Data Types & Constant

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

CS313D: ADVANCED PROGRAMMING LANGUAGE

IS 320 A/B Winter 1998 Page 1 Exam 1

(Subroutines in Visual Basic)

Language Fundamentals

CMPT 110 MIDTERM OCTOBER 18, 2001

Please answer questions in the space provided. Question point values are shown in parentheses.

VBA Handout. References, tutorials, books. Code basics. Conditional statements. Dim myvar As <Type >

The name of this type library is LabelManager2 with the TK Labeling Interface reference.

Microsoft Visual Basic 2005: Reloaded

Programming with Visual Studio Higher (v. 2013)

Sub Programs. To Solve a Problem, First Make It Simpler

Procedural programs are ones in which instructions are executed in the order defined by the programmer.

CS111: PROGRAMMING LANGUAGE II

NOTES: Procedures (module 15)

Level 3 Computing Year 2 Lecturer: Phil Smith

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

DroidBasic Syntax Contents

IS 320 A-C Page 1 Spring 99 Exam 2

Year 12 : Visual Basic Tutorial.

d2vbaref.doc Page 1 of 22 05/11/02 14:21

CSc Introduction to Computing

Angel International School - Manipay 1 st Term Examination November, 2015

HELP - VB TIPS. ANIMATE AN IMAGE BOX Insert a module. In this module, create a global variable: Global x

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

Welcome To VTL Course

Language Reference Manual

Functions. Lecture 6 COP 3014 Spring February 11, 2018

PROGRAM 1: SIMPLE CALCULATOR

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Variable A variable is a value that can change during the execution of a program.

Lecture Using ListBox and ComboBox Controls In Visual Basic 6: list box


Your first C++ program

1 Lexical Considerations

Lexical Considerations

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Flow Control. CSC215 Lecture

RTL Reference 1. JVM. 2. Lexical Conventions

Lab Sheet 4.doc. Visual Basic. Lab Sheet 4: Non Object-Oriented Programming Practice

Procedures in Visual Basic

Revision for Final Examination (Second Semester) Grade 9

Start Visual Basic. Session 1. The User Interface Form (I/II) The Visual Basic Programming Environment. The Tool Box (I/II)

EnableBasic. The Enable Basic language. Modified by Admin on Sep 13, Parent page: Scripting Languages

Fundamentals of Programming Session 23

Random numbers program

2Practicals Visual Basic 6.0


Text box. Command button. 1. Click the tool for the control you choose to draw in this case, the text box.

The Control Properties

Part 4 - Procedures and Functions

CS111: PROGRAMMING LANGUAGE II

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

Procedures (Subroutines) and Functions

Input And Output of C++

IDENTIFYING UNIQUE VALUES IN AN ARRAY OR RANGE (VBA)

Exercise: Using Numbers

Angel International School - Manipay 1 st Term Examination November, 2015

Visual Basic. The Integrated Development Environment. Menu Bar

Statements and Operators

Good Variable Names: dimensionone, dimension1 Bad Variable Names: dimension One, 1dimension

C Programming for Engineers Functions

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Lexical Considerations

How to Design Programs

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Lecture 2 Tao Wang 1

The SudokuPuzzle Class

MIS 216 SPRING 2018 PROJECT 4

CS260 Intro to Java & Android 03.Java Language Basics

Motivation was to facilitate development of systems software, especially OS development.

An InputBox( ) function will display an input Box window where the user can enter a value or a text. The format is

PROGRAMMAZIONE I A.A. 2017/2018

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

20. VB Programming Fundamentals Variables and Procedures

1. Create your First VB.Net Program Hello World

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Syntax. Table of Contents

XQ: An XML Query Language Language Reference Manual

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Variables. Data Types.

Creating a Dynamo with VBA Scripts

Programming with visual Basic:

Computational Expression

Java is an objet-oriented programming language providing features that support

Motivation was to facilitate development of systems software, especially OS development.

VB FUNCTIONS AND OPERATORS

These are notes for the third lecture; if statements and loops.

Type Storage Range of Values

Objectives. Introduce static keyword examine syntax describe common uses

Transcription:

Function: function procedures and sub procedures share the same characteristics, with one important difference- function procedures return a value (e.g., give a value back) to the caller, whereas sub procedures do not. Function Procedures can be created with the add procedure dialog shown in figure by selecting function. Figure bellow shows a function procedure. Fact, created with the add procedure dialog. Fact implicitly returns variant. Fact could also have been created by typing the function procedure directly into the code window. The line Private Function Fact() is the function procedure header. The header contains the keyword function, the function name and parentheses. The declarations and statements that the programmer will insert between the header and form the function procedure body, Fact is invoked with the line. Result= Fact( ) When a function procedure name (such as Fact) is encountered at run time, the function procedure is called, causing its body statements to execute. Consider the complete definition for Fact Private Function Fact( N ) Fact=N^2 A function procedure return value is specified in the body by assigning a value to the function procedure name, as in Fact=N^2 Then returns (along with the value returned) to the calling statement Result=Fact (N) And the return value is assigned to variable result. Program execution then continues with the next statement after the call to Fact. All function procedure definitions contain parentheses, the parentheses may be empty (e.g. Fact) or may contain one parameter variable declarations. Consider the following function procedure: Private Function Area (s1 as single,s2 as single) Area=s1*s2

Which declare two parameter variables s1, and s2. Area s return type is variant. Area is called with the statement Square=area(8.5, 7.34) The value 8.5 is stored in s1 and the value 7.34 is stored in s2. Example: Write a code program to read three integer numbers. Using a define sub Function (Min) to determine the smallest of three integers. Display the smallest value in textbox. Dim Num1 As Single, Num2 As Single, Num3 As Single, Result As Single Num1 = Fix(Text1.Text) Num2 = Fix(Text2.Text) Num3 = Fix(Text3.Text) Result =MinNum1, Num2, Num3) Text4.Text = Str(Result) Private Function Min(Num1, Num2, Num3) Min = Num1 If Num2 < Min Then Min = Num2 If Num3 < Min Then Min = Num3 Example: Write a code program to input the value of N. Using a define sub function fact to determine(n!). Display the result into text box. Dim N As Single, Result As Double N = Val(Text1.Text) Result = Fact(N) Text2.Text = Str(Result) Private Function Fact(N) Dim I, F F = 1

For I = 1 To N F = F * I Next Fact = F Example: write function to find a character in the string begin the search from a determined position. Dim i As Integer Function findchr(stringvalue As String, po As Integer, char As String) As Boolean For i = po To Len(stringvalue) If Mid(stringvalue, i, 1) = char Then findchr = True Exit Function End If Next i findchr = False Dim bool As Boolean Dim st As String Dim start As Integer Dim ch As String st = Text1.Text ch = Text2.Text78u start = Val(Text3.Text) bool = findchr(st, start, ch) If bool Then MsgBox "The character is found and his position is" & i

Else MsgBox "not found" End If Private Sub Command2_Click() End ByRef and ByVal Parameters can be sent to a subroutine By Reference (ByRef) or By Value (ByVal). ByRef is the default, and means that changes to the variable in the subroutine will result in changes to the source variable outside of the subroutine. ByVal literally copies the values of the variables from the calling subroutine into the called subroutine. By doing this, the variables can be changed, but their values will not change outside of the called subroutine. ByVal can also be a lot slower with large variable types, however, since memory has to be copied from one location to another. If you don't have any reason to do so, there is no need to pass variables ByVal. You can explicitly state the way that a variable is passed to a subroutine by using these keywords before the variable name Example: Sub showdiff(byval a As Integer, ByRef b As Integer) a = a + 10 Form1.Print "print variable A inside procedure"; a b = b + 2 Form1.Print "print variable B inside procedure"; b Dim no1 As Integer Dim no2 As Integer

no1 = Val(Text1.Text) no2 = Val(Text2.Text) Print "first no. and second no. befor call procedure" Print no1, no2 Call showdiff(no1, no2) Print "first no. and second no. after call procedure" Print no1, no2 Private Sub Command2_Click() End