Results 1 to 15 of 15

Thread: Subs and Functions

  1. #1

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274

    Post

    Can anyone tell me the difference between subs and functions, sub and functions which can receive values and all. Any kinda help will be greatly appreciated.

  2. #2
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    A function can return a value (actually, it does return a value), a sub can't (doesn't).
    Hope this helps

    Crazy D

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Basicly a Sub is just a piece of code that runs when you call it. for Example a PrintCentral Sub could be as follows

    Code:
    Public Sub PrintCentral(str As String)
    
    Form1.CurrentX = (Form1.ScaleWidth - Form1.TextWidth) / 2
    Form1.CurrentY = (Form1.ScaleHeight - Form1.TextHeight(str)) / 2
    
    Form1.Print str
    
    End Sub

    Which Prints Text into the Centre of your Form (Try it, not very usefull but Still)

    But You Can't Get any information Back From The Sub, it just runs and does something.

    If You Want to Get Information Back You Can Use a Function For example You Could Have a Function That Takes 2 Numbers and Gives You a Times table Type String, so 3 and 4 would give you "3 Times 4 is 12" Like This

    Code:
    Public Function TimesTables(FirstNumber As Integer, SecondNumber As Integer) As String
    
    TimesTables = CStr(FirstNumber) & " Times " & _
                  CStr(SecondNumber & " Is " & _
                  CStr(FirstNumber * SecondNumber) & "."
    
    End Function

    Now All You Have to Do is this

    PrintCenral TimesTables(3,4)

    and your TimesTable Apears in the Centre of the Form.

    Hope This Helps

  4. #4
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Originally posted by Crazy D
    A function can return a value (actually, it does return a value), a sub can't (doesn't).
    Kinda depends on how you define "return". I've got subroutines that take a string, modify it and return the modified string to the calling routine.

  5. #5
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Return is defined in programming as that thing functions do but subs don't (probably not the dictionary definition but it's what it means)

    modifying byRef variables is Technicly a side efect of a function.

  6. #6
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    That may be your definition but it's not mine.

  7. #7
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089

    well it's what it means, I can't remember exactly how it's defined, but the value the function returs is the value of mVar after the line

    mVar = MyFunc(params..)

    It doesn't matter much as long as you can distinguish between the 2, but don't go talking about a return value when you mean an out parameter (which is what it was, I've just remembered, a side effect is something similar but not useful) it can confuse people.

  8. #8
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    Yes of course a sub can return a value too by passing the params ByRef. But, a function can *always* return a function, no matter if the params are byval or byref.
    Hope this helps

    Crazy D

  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Talking My 2.1 cents

    Subs are faster (by about a factor of ten) and Module subs are fastest!

    But considering the time periods involved this is really a very small issue.

    A sub is a the same a C/C++ Void function. ie it returns NO value.

    Passing a referernce to a sub and having it modified is not "Returning" a value. Only one value can be returned by a function: a variable, array, UDT or object pointer

    A sub "Returns" in the sense that it finishes and operation is passed back to the sub or function that called it but that's it.

    References can be passed to functions too and can be modified but these values are not known as the return value.

    CrazyD... Why does your first and last post argue opposite points? What DO you believe? or does it depend on luna cycles?
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  10. #10
    Lively Member benski's Avatar
    Join Date
    Sep 1999
    Location
    UK
    Posts
    126
    Poor wasiq-

    Presumably your question is more than sufficiently answered!!

    I think vb-world needs a "Pendantic Answers" forum...
    (Please don't flame-- it *was* meant in good humour!!)


  11. #11
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Talking

    hahhahaha

    I'd be there all day!

    A competition to see who can answer the same questions the most times but with different wording!

    I think benski that what you'll find is that for those addicted, a question is an excuse to start a debate about the question topic, the winner being the person whose code get's incorporated !!!



    hahahaha

    Kedaman and Meg should be on this thread

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  12. #12
    Lively Member benski's Avatar
    Join Date
    Sep 1999
    Location
    UK
    Posts
    126
    Well put!

  13. #13
    New Member
    Join Date
    Jun 2000
    Location
    Canada
    Posts
    8

    On The Bandwagon

    I'd just like to jump on the bandwagon and give you my two cents (Holy Mixed Metaphors Batman!!) I agree with Sam Finch in that functions RETURN a variable, subs do not. A sub can be PASSED a vlue, by reference, which changes the value of the variable during the execution of the sub. At the end of the sub NOTHING is really returned to where the sub was called. Functions return values, subs don't!
    Jonathan (Co President - Big Berdie International)
    Karl Moore RULEZ!
    "Pipers Do It With Amazing Grace!"

  14. #14
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Seeing as we are all offering opinions, here's mine.


    Becuse a fucntion can return a value, it can be used in a Logic or Arithmetic operation. A sub cannot.
    Iain, thats with an i by the way!

  15. #15

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274

    Talking

    thanks for all ur help, i think its enough for my little question. I can fully differentiate between a sub and a function (hopefully, . thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width