Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT]How to write your own function[/TUT]
#1
A function is something you can write and put in your code. You can use it as many times later on in the code without having to write it again and again. The difference between a Sub and a function is that a function returns something.

It's being used in crypters for the encrypting/decrypting alot. Oke lets start
Add 2 numbers
Code:
Function Add(ByVal intNumber1 As Integer, ByVal intNumber2 As Integer) As Integer
        Dim intReturn As Integer
        intReturn = intNumber1+ intNumber2
        Return intReturn
    End Function
Lets break it down. I named the function "Add" as you can see. And i give the function 2 Integers "intNumber1 and intNumber2" then i say that the return value is gonna be an integer "As Integer".
How we use this:
Code:
MessageBox.Show(Add(TextBox1.Text, TextBox2.Text))
Its better to put variables but now i used "TextBox1.Text" because that's easier to follow. Next in our code we calculate the sum "intReturn = intNumber1+ intNumber2". Fine we now have the sum, we have the return the sum "intReturn". If we dont do that, are function is worthless

Even and Odd numbers
Code:
Function EvenOrOdd(ByVal intNumber As Integer) As Boolean
        If intNumber Mod 2 = 0 Then
            Return True
        Else
            Return False
        End If
    End Function
Now the name of our function is EvenOrOdd, we give 1 integer to it "intNumber" and the output will be "boolean" true or false. If the rest is 0 then we have a even number so we return True "Return True" else it's an odd number and we return false "Return False". So it's not necessary to have only one return option, we can return different things. In this case true or false.
I hope this was a good little tut, if there is interest i will make another to check if a number is a prime number, perfect number etc.. And i will cover a Sub in the next tut and more advanced functions
Reply
#2
Very well made tutorial. Formatting isn't so bad but very understandable. I read through the whole thing and it helped me out a bit. Totally bookmarked. Thanks a lot.
[Image: t5BWm.png]
Reply
#3
This is a really nice and thorow tutorial. Great job man!
Reply
#4
(03-27-2011, 08:56 AM)L3g1tWa5te Wrote: Very well made tutorial. Formatting isn't so bad but very understandable. I read through the whole thing and it helped me out a bit. Totally bookmarked. Thanks a lot.
Thank you, it's my first programming tutorial, more to come!
Reply
#5
You should explain byref,byval and optional ?
Reply
#6
(03-28-2011, 02:38 PM)h4yr0 Wrote: You should explain byref,byval and optional ?
Oke i will do it, but first i am gonna think how to explain it, its not easy to explain ;p in text.

Reply
#7
(03-28-2011, 02:38 PM)h4yr0 Wrote: You should explain byref,byval and optional ?
And overloading too, if you want to make a complete tutorial on functions.
My software company: Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber
Reply
#8
yes man , I forget that one Smile)
Reply
#9
Great tutorial for beginners. This should encourage them to make their own functions instead of looking for premade source codes around the internet.
Reply
#10
Great tut i love it keep it up.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to hook the keyboard. [VB.NET] [TuT] Vorfin 92 60,894 09-23-2019, 11:55 AM
Last Post: Jamimartin
  [TUT]Creating Advanced Web Browser with Awesome Theme Imaking31 0 1,320 05-25-2013, 03:12 AM
Last Post: Imaking31
  VB.NET Port Scanner [TUT] Fragma 30 14,435 11-27-2012, 11:26 PM
Last Post: ƃu∀ ıʞƃu∀
  [TUT] MD5 Encrypter & Finder [VB.NET] Fragma 12 7,845 11-25-2012, 10:36 PM
Last Post: ƃu∀ ıʞƃu∀
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 61,859 10-07-2012, 06:56 AM
Last Post: a99

Forum Jump:


Users browsing this thread: 2 Guest(s)