×

VB.Net Programs

VB.Net program to create a user-defined function to add two integer numbers

Here, we are going to learn how to create a user-defined function to add two integer numbers in VB.Net?
Submitted by Nidhi, on December 13, 2020 [Last updated : March 07, 2023]

Creating a function to add two numbers

Here, we will create a user-defined function that will accept two integer numbers as an argument and return the addition of arguments to the calling function or sub-routine.

Program/Source Code:

The source code to create a user-defined function to add two integer numbers is given below. The given program is compiled and executed successfully.

VB.Net code to create a function to add two numbers

'VB.Net program to create a user-define function 
'to add two numbers.

Module Module1
    Function AddNum(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
        Dim num3 As Integer = 0

        num3 = num1 + num2
        AddNum = num3
    End Function

    Sub Main()
        Dim num1 As Integer = 0
        Dim num2 As Integer = 0
        Dim num3 As Integer = 0

        Console.WriteLine("Enter number1: ")
        num1 = Integer.Parse(Console.ReadLine())

        Console.WriteLine("Enter number2: ")
        num2 = Integer.Parse(Console.ReadLine())

        num3 = AddNum(num1, num2)
        Console.WriteLine("Addition is: {0}", num3)
    End Sub
    
End Module

Output

Enter number1:
20
Enter number2:
30
Addition is: 50
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a sub-routine Main() and a user define function AddNum().

The Main() subroutine is the entry point for the program. Here, we read two integer numbers from the user and pass them into AddNum() function.

The AddNum() is the user-defined function that accepts two integer arguments and returns the addition of arguments to the Main() subroutine, then the output will be printed on the console screen.

VB.Net User-defined Functions Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.