OAS Lab 3 Ins samia alblwi Functions Ins
OAS Lab 3 Ins. samia alblwi
Functions Ins. samia alblwi
The Difference Between Subroutines and Functions There are two types of code procedures: subroutines and functions, and on casual inspection they both appear to look the same. However, they actually are different: 1 -A subroutine is a piece of code that performs a set of actions or calculations or a combination of the two. It can form a “building block” within a program and may sometimes need to be repeated. It can be called by several different routines. 2 -The programmer has to write a subroutine only once, and it can be called from anywhere within the program as many times as needed. However, it does not return a value; if it performs a calculation, there is no direct way of finding the result. Ins. samia alblwi
3 -A function is exactly like a subroutine except that it returns a value. 4 -Functions start with Function (instead of Sub) and end with End Function (instead of End Sub) Ins. samia alblwi
Example: Sub Try. Add() Msg. Box Add. Numbers(34, 21) End Sub Function Add. Numbers(N 1 As Integer, N 2 As Integer) Add. Numbers = N 1 + N 2 End Function Ins. samia alblwi
IF statement The simplest, one line, form of an If statement is: If condition Then statement 1 [Else statement 2] End IF If condition Then statement 1 Else. If condition 2 statement 2 Else statement 3 End If Ins. samia alblwi
Example: This macro looks at the value in A 1. If it is 100 it enters � the text Full marks in B 1. If the value in A 1 is not 100, the macro enters No in B 1. Ins. samia alblwi
solution Sub fullmarks() Sheets("Sheet 1"). Select Cells(1, 1). Select If Active. Cell = 100 Then Cells(1, 2). Value = "Full marks" Else Cells(1, 2). Value = "No" End If End Sub Ins. samia alblwi
Select Case is similar to If…Then…Else, except that you can choose from several condition values The general form is Select Case expression Case value 1 one or more VB statements for action 1 Case value 2 one or more VB statements for action 2 ……… Case valuem one or more VB statements for actionm [Case Else one or more statements for action otherwise] End Select Ins. samia alblwi
Select Dim X As Integer X = 20 Select Case X Case 20 Msg. Box(“The value 20”) Case Else Msg. Box(“The value not 20”) End Select Ins. samia alblwi
Example Select Case X Case Is >= 90 Result = "Excellent“ Case 80 To 89 Result = "Very Good“ Case 70 To 79 Result = "Good” Case 60 To 69 Result = "Accepted“ Case Else Result=”Failure” End Select Ins. samia alblwi
Example 1 -2 Public Function score(mark) Select Case mark Case Is < 60 score = "That equivalent F“ . . Case Is = "" score = "Please : : Enter Correctly *MARK* " End Select End Function Ins. samia alblwi
Example 2 -2 Public Sub type_mark() Dim mark = Input. Box(" Enter your mark ? ? ") Msg. Box score(mark) End Sub Ins. samia alblwi
- Slides: 13