1 / 18

פונקציות

פונקציות. פונקציות מוכרות. 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()

kaili
Télécharger la présentation

פונקציות

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. פונקציות

  2. פונקציות מוכרות 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

  3. פונקציות במחרוזות 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

  4. דוגמא של 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

  5. עוד דוגמא 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

  6. פונקציות למה זה חשוב מודולאריות מודולאריות מודולאריות מבוא למדעי המחשב - מאיר קומר - סמסטר א'- תשס"ט - שיעור מספר 5

  7. עם פרמטרים 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

  8. 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

  9. 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

  10. פונקציות תחום הקיום של משתנה - scope משתנה מקומי Local variable משתנה גלובלי Global variable מבוא למדעי המחשב - מאיר קומר - סמסטר א'- תשס"ט - שיעור מספר 5

  11. 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

  12. 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

  13. עם פרמטרים! 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

  14. איזה כף!!! 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

  15. בניית פונקציות 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

  16. עם שני פרמטרים 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

  17. שימו לב לתיאום בין הפונקציה וה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

  18. התחלה לתרגיל האחרון Module Module1 Sub Mishva() Console.WriteLine("Answer here!") End Sub Sub Main() Mishva() Console.ReadKey() End Sub End Module

More Related