Imports System Math Module 1 Sub Main Dim
Imports System. Math פונקציות מוכרות Module 1 Sub Main() Dim x As Decimal = Math. Abs(-10. 4) Dim y As Decimal Console. Write. Line("I will find the square root of a number") y = Console. Read. Line() Console. Write. Line("The absolute value is " & x) Console. Write. Line("The value of PI is " & Math. PI) Console. Write. Line("The square root of " & y & " is " & Math. Sqrt(y)) Console. Write. Line("2 to the 4. 5 power is " & Math. Pow(2, 4. 5)) Console. Read. Key() End Sub End Module
פונקציות במחרוזות Module 1 Sub Main() Dim word As String = "Hello world!" Dim find As String Console. Write. Line("The word is " & word) Console. Write. Line("The length is " & word. Length()) Console. Write. Line("Part of the word " & word. Substring(3, 5)) find = Console. Read. Line() Console. Write. Line("Was it there? " & word. Contains(find)) word = word. Insert(3, find) Console. Write. Line("The word is now " & word) Console. Read. Key() End Sub End Module
Subroutine דוגמא של Module 1 Sub Test. Sub() Console. Write. Line("This is the function") Console. Write. Line("This is another line of text") End Sub Main() Dim i As Integer For i = 1 To 10 Test. Sub() Next Console. Read. Key() End Sub End Module
Module 1 עוד דוגמא Sub Add. Numbers() Dim first As Integer Dim second As Integer Dim answer As Integer Console. Write. Line("Please type a number") first = Console. Read. Line() Console. Write. Line("Please type another number") second = Console. Read. Line() answer = first + second Console. Write. Line("The total is " & answer) End Sub Main() Add. Numbers() Console. Read. Key() End Sub End Module
Module 1 עם פרמטרים Sub Add. Numbers(By. Val first As Integer, By. Val second As Integer) Dim answer As Integer answer = first + second Console. Write. Line("The total is " & answer) End Sub Main() Add. Numbers(4, 5) Console. Read. Key() End Sub End Module
SWAP! Module 1 Sub Swap(By. Val first As Integer, By. Val second As Integer) Dim temp As Integer temp = first = second = temp Console. Write. Line("The first variable is now " & first) Console. Write. Line("The second variable is now " & second) End Sub Main() Dim x As Integer = 4 Dim y As Integer = 5 Swap(x, y) Console. Write. Line("X is now " & x) Console. Write. Line("y is now " & y) Console. Read. Key() End Sub End Module
Module 1 SWAP! – The Fix (#1) Sub Swap(By. Ref first As Integer, By. Ref second As Integer) Dim temp As Integer temp = first = second = temp Console. Write. Line("The first variable is now " & first) Console. Write. Line("The second variable is now " & second) End Sub Main() Dim x As Integer = 4 Dim y As Integer = 5 Swap(x, y) Console. Write. Line("X is now " & x) Console. Write. Line("y is now " & y) Console. Read. Key() End Sub End Module
SWAP! – The Fix (#2) Module 1 Dim x As Integer = 4 Dim y As Integer = 5 Sub Swap() Dim temp As Integer temp = x x=y y = temp Console. Write. Line("The first variable is now " & x) Console. Write. Line("The second variable is now " & y) End Sub Main() Swap() Console. Write. Line("X is now " & x) Console. Write. Line("y is now " & y) Console. Read. Key() End Sub End Module
Return Home! Module 1 Function One() As Integer Dim x As Integer x = Console. Read. Line() Return x + 2 End Function Sub Main() Dim x As Integer x = One() Console. Write. Line("X is now " & x) Console. Read. Key() End Sub End Module
! עם פרמטרים Module 1 Function One(By. Val first As Integer) As Integer Return first + 2 End Function Sub Main() Dim x As Integer x = One(5) Console. Write. Line("X is now " & x) Console. Read. Key() End Sub End Module
!!! איזה כף Module 1 Function One(By. Val first As Integer, By. Val second As Integer, By. Val third As Integer) As Integer Return first * second + third * first End Function Sub Main() Dim x, y, z As Integer z=3 x=4 y=5 Console. Write. Line("What? ? ? " & One(z, x, y)) Console. Write. Line("What? ? ? " & One(x, y, z)) Console. Read. Key() End Sub End Module
בניית פונקציות Module 1 Function Atzeret(By. Val first As Integer) As Integer Dim product As Integer = 1 Dim i As Integer For i = 2 To first product *= i Next Return product End Function Sub Main() Console. Write. Line("What? ? ? " & Atzeret(6)) Console. Read. Key() End Sub End Module
עם שני פרמטרים Module 1 Function Chezkat(By. Val first As Integer, By. Val second As Integer) As Integer Dim product As Integer = 1 Dim i As Integer For i = 1 To second product *= first Next Return product End Function Sub Main() Console. Write. Line("What? ? ? " & Chezkat(2, 3)) Console. Write. Line("What? ? ? " & Chezkat(4, 2)) Console. Write. Line("What? ? ? " & Chezkat(10, 3)) Console. Read. Key() End Sub End Module
MAIN שימו לב לתיאום בין הפונקציה וה Module 1 Function Grade(By. Val first As Decimal) As Char Dim answer As Char = "F" If (first < 100 And first > 90) Then Return "A" End If If (first < 90 And first >= 80) Then Return "B" End If Return answer End Function Sub Main() Dim x As Char x = Grade(5) Console. Write. Line("X is now " & x) Console. Read. Key() End Sub End Module
התחלה לתרגיל האחרון Module 1 Sub Mishva() Console. Write. Line("Answer here!") End Sub Main() Mishva() Console. Read. Key() End Sub End Module
- Slides: 18