TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Function Examples

Use a Function member inside a Module to receive 0 or more arguments, and return 1 value.
Function. A Function returns a value. It uses a special syntax form in the VB.NET language. The Function optionally accepts one or more parameters—these are called formal parameters.Keywords
A Function is part of a Module, Class or Structure. A Function is called from other Functions, Subs or Properties. It can be reused throughout a program.Module
First example. This program shows the Function keyword. We provide an Area Function: this Function receives a radius. It returns a Double that is the area of a circle with that radius.

Formal parameter: Area receives one formal parameter of type Double. Specifying the type of the parameter is optional but advised.

ByVal, ByRef

Returns: The Function returns a value of type Double. After the formal parameter list, the keywords "As Double" indicate the return type.

VB.NET program that demonstrates Function Module Module1 ''' <summary> ''' Get area of a circle with specified radius. ''' </summary> Function Area(ByVal radius As Double) As Double Return Math.PI * Math.Pow(radius, 2) End Function Sub Main() Dim a As Double = Area(4.0) Console.WriteLine(a) End Sub End Module Output 50.2654824574367
No argument. A Function must return a value—otherwise we must use a Subroutine. But a Function does not need to receive a value—it can have an empty parameter list.
VB.NET program that uses Function, no argument Module Module1 Function GetID() As Integer ' This function receives no arguments. Return 100 End Function Sub Main() ' Use GetID function. Console.WriteLine(GetID()) End Sub End Module Output 100
Functions, Properties. What is the difference between a Function and a Property? A Property is a type of Function. The Get part of a Property can be implemented as a Function.

Info: Properties are meant to replace getters and setters. If you have a Sub that sets a value, it can be changed to be a Property.

Tip: At the level of the implementation, Properties are similar to Functions and Subs.

Property

And: If you want to, you can change all Properties on your types to Functions and Subs. You won't get in trouble for doing this.

But: On existing types, such as those in the .NET Framework, you must use the Property syntax if the member is a Property.

VB.NET program that compares Property to Function Module Module1 ReadOnly Property ID As Integer Get Return 100 End Get End Property Function GetID() As Integer Return 100 End Function Sub Main() ' Use property and function. Console.WriteLine("PROPERTY: {0}", ID) Console.WriteLine("FUNCTION: {0}", GetID()) End Sub End Module Output PROPERTY: 100 FUNCTION: 100
Return values. A Function can only return one value. But if this value is a class or Structure, it can return many values in this step. ByRef can be used to set output parameters.Multiple Return Values
A summary. Unlike a Sub, a Function returns a value. We must return this value—if we have no return value, we need to use a Sub instead. We can assign to the result of a Function.Sub
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf