180 likes | 348 Views
פונקציות. פונקציות מוכרות. Imports System.Math Module Module1 Sub Main() Dim x As Decimal = Math.Abs(-10.4) Dim y As Decimal Console.WriteLine("I will find the square root of a number") y = Console.ReadLine()
E N D
פונקציות מוכרות Imports System.Math Module Module1 Sub Main() Dim x As Decimal = Math.Abs(-10.4) Dim y As Decimal Console.WriteLine("I will find the square root of a number") y = Console.ReadLine() Console.WriteLine("The absolute value is " & x) Console.WriteLine("The value of PI is " & Math.PI) Console.WriteLine("The square root of " & y & " is " & Math.Sqrt(y)) Console.WriteLine("2 to the 4.5 power is " & Math.Pow(2, 4.5)) Console.ReadKey() End Sub End Module
פונקציות במחרוזות Module Module1 Sub Main() Dim word As String = "Hello world!" Dim find As String Console.WriteLine("The word is " & word) Console.WriteLine("The length is " & word.Length()) Console.WriteLine("Part of the word " & word.Substring(3, 5)) find = Console.ReadLine() Console.WriteLine("Was it there? " & word.Contains(find)) word = word.Insert(3, find) Console.WriteLine("The word is now " & word) Console.ReadKey() End Sub End Module
דוגמא של Subroutine Module Module1 Sub TestSub() Console.WriteLine("This is the function") Console.WriteLine("This is another line of text") End Sub Sub Main() Dim i As Integer For i = 1 To 10 TestSub() Next Console.ReadKey() End Sub End Module
עוד דוגמא Module Module1 Sub AddNumbers() Dim first As Integer Dim second As Integer Dim answer As Integer Console.WriteLine("Please type a number") first = Console.ReadLine() Console.WriteLine("Please type another number") second = Console.ReadLine() answer = first + second Console.WriteLine("The total is " & answer) End Sub Sub Main() AddNumbers() Console.ReadKey() End Sub End Module
פונקציות למה זה חשוב מודולאריות מודולאריות מודולאריות מבוא למדעי המחשב - מאיר קומר - סמסטר א'- תשס"ט - שיעור מספר 5
עם פרמטרים Module Module1 Sub AddNumbers(ByVal first As Integer, ByVal second As Integer) Dim answer As Integer answer = first + second Console.WriteLine("The total is " & answer) End Sub Sub Main() AddNumbers(4, 5) Console.ReadKey() End Sub End Module
SWAP! Module Module1 Sub Swap(ByVal first As Integer, ByVal second As Integer) Dim temp As Integer temp = first first = second second = temp Console.WriteLine("The first variable is now " & first) Console.WriteLine("The second variable is now " & second) End Sub Sub Main() Dim x As Integer = 4 Dim y As Integer = 5 Swap(x, y) Console.WriteLine("X is now " & x) Console.WriteLine("y is now " & y) Console.ReadKey() End Sub End Module
SWAP! – The Fix (#1) Module Module1 Sub Swap(ByRef first As Integer, ByRef second As Integer) Dim temp As Integer temp = first first = second second = temp Console.WriteLine("The first variable is now " & first) Console.WriteLine("The second variable is now " & second) End Sub Sub Main() Dim x As Integer = 4 Dim y As Integer = 5 Swap(x, y) Console.WriteLine("X is now " & x) Console.WriteLine("y is now " & y) Console.ReadKey() End Sub End Module
פונקציות תחום הקיום של משתנה - scope משתנה מקומי Local variable משתנה גלובלי Global variable מבוא למדעי המחשב - מאיר קומר - סמסטר א'- תשס"ט - שיעור מספר 5
SWAP! – The Fix (#2) Module Module1 Dim x As Integer = 4 Dim y As Integer = 5 Sub Swap() Dim temp As Integer temp = x x = y y = temp Console.WriteLine("The first variable is now " & x) Console.WriteLine("The second variable is now " & y) End Sub Sub Main() Swap() Console.WriteLine("X is now " & x) Console.WriteLine("y is now " & y) Console.ReadKey() End Sub End Module
Return Home! Module Module1 Function One() As Integer Dim x As Integer x = Console.ReadLine() Return x + 2 End Function Sub Main() Dim x As Integer x = One() Console.WriteLine("X is now " & x) Console.ReadKey() End Sub End Module
עם פרמטרים! Module Module1 Function One(ByVal first As Integer) As Integer Return first + 2 End Function Sub Main() Dim x As Integer x = One(5) Console.WriteLine("X is now " & x) Console.ReadKey() End Sub End Module
איזה כף!!! Module Module1 Function One(ByVal first As Integer, ByVal second As Integer, ByVal 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.WriteLine("What??? " & One(z, x, y)) Console.WriteLine("What??? " & One(x, y, z)) Console.ReadKey() End Sub End Module
בניית פונקציות Module Module1 Function Atzeret(ByVal 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.WriteLine("What??? " & Atzeret(6)) Console.ReadKey() End Sub End Module
עם שני פרמטרים Module Module1 Function Chezkat(ByVal first As Integer, ByVal 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.WriteLine("What??? " & Chezkat(2, 3)) Console.WriteLine("What??? " & Chezkat(4, 2)) Console.WriteLine("What??? " & Chezkat(10, 3)) Console.ReadKey() End Sub End Module
שימו לב לתיאום בין הפונקציה והMAIN Module Module1 Function Grade(ByVal 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.WriteLine("X is now " & x) Console.ReadKey() End Sub End Module
התחלה לתרגיל האחרון Module Module1 Sub Mishva() Console.WriteLine("Answer here!") End Sub Sub Main() Mishva() Console.ReadKey() End Sub End Module