Presentation is loading. Please wait.

Presentation is loading. Please wait.

VB.Net Programming Console Application

Similar presentations


Presentation on theme: "VB.Net Programming Console Application"— Presentation transcript:

1 VB.Net Programming Console Application
Walkthrough Example : Function

2 Narrative Memory Screen Code
The first line of the program indicates the main( ) procedure. Note the separate Function SquareFunc Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen

3 Narrative Memory Screen Code
These lines declare in memory two locations called In_x Sq_x Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen In_X Sq_X

4 Narrative Memory Screen Code This line clears the console screen. In_X
Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen In_X Sq_X

5 Narrative Memory Screen Code
This line writes Demo of Simple Function-Square on the console screen. Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen In_X Sq_X Demo of Simple Function -Square

6 Narrative Memory Screen Code
Writes a blank line onto the console screen Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen In_X Sq_X Demo of Simple Function -Square

7 Narrative Memory Screen Code
Prompts the user to Enter a value to be squared Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen In_X Sq_X Demo of Simple Function –Square Enter value to be squared…:

8 Narrative Memory 4 Screen Code
The user enters a value of 4 and this value is placed in location in_x Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4

9 Narrative Memory 4 Screen Code This line has two parts.
The SquareFunc code is called sending in_x as its parameter. Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4

10 Narrative Memory 4 Screen Code Memory-SquareFunc X SquareFunc
The main program is suspended while the SquareFunc function is run. SquareFunc is given Memory of its own and places the value received in its parameter into its own location X. Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4 Memory-SquareFunc X SquareFunc

11 Narrative Memory 4 16 Screen Code Memory-SquareFunc X SquareFunc
The calculation is performed x * x and the result 16 is placed in to SquareFunc. Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4 Memory-SquareFunc X 16 SquareFunc

12 Narrative Memory 4 16 Screen Code Memory-SquareFunc X SquareFunc
Now the function ends and the contents of SquareFunc is passed back to the main program and into sq_x. The main program now continues. Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 16 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4 Memory-SquareFunc X SquareFunc

13 Narrative Memory 4 16 Screen Code
The memory given to the SquareFunc is no longer needed. Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.Write("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 16 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4

14 Narrative Memory 4 16 Screen Code
This line prints out the two memory locations within the message: The square of 4 is 16 Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.WriteLine("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 16 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4 The square of 4 is 16

15 Narrative Memory 4 16 Screen Code
The final line waits for a key press before ending the program. Sub Main( ) Dim in_x As Integer Dim sq_x As Integer Console.Clear() Console.WriteLine("Demo of simple Function- Square") Console.WriteLine() Console.Write("Enter value to be squared...:") in_x = Console.ReadLine( ) sq_x = SquareFunc(in_x)   Console.WriteLine("The square of {0} is {1}", in_x, sq_x) Console.ReadKey( ) End Sub Function SquareFunc(ByVal x As Integer) As Integer SquareFunc = x * x End Function Code Narrative Memory Screen 4 16 In_X Sq_X Demo of Simple Function –Square Enter value to be squared…: 4 The square of 4 is 16


Download ppt "VB.Net Programming Console Application"

Similar presentations


Ads by Google