CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2009
    Posts
    28

    functino that returns nothing

    hi, is there anyway to make a proper function that does not return anything. when i try to create i get this kind of error:

    A null reference exception could occur at run time when the result is used.

    when i put as integer behind it gets rid of the error, but i was wondering if there was anyway that i could do to get rid of this thing

  2. #2
    Join Date
    Apr 2009
    Posts
    394

    Re: functino that returns nothing

    By creating a sub...



    Good Luck

  3. #3
    Join Date
    Oct 2009
    Posts
    28

    Re: functino that returns nothing

    oo, ok sry now it looks real dumb, but vb is really a different dialect, i've yet to come across such a specific language lol

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: functino that returns nothing

    Post some of your code, and we will try to see what you are trying to do.
    Code:
    ' Use CODE TAGS
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: functino that returns nothing

    A proper function, as you put it, should return something.

    A proper Sub, returns nothing...

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: functino that returns nothing

    The way VB treats that is quite versatile in fact.

    You will write a Sub if you want to use it like a command.
    (In Pascal it's called a procedure, so it is not so specific at all).

    You write a function if you want to use it within an expression. There it will result in a value anyway when the expression is evaluated.

    If your function code does not provide a return value explicitly, zero, or null, or Nothing or Empty is returned depending on the function's data type.
    Nevertheless, having a return value or not, any function can be used and treated syntactically like a sub when the return value is not needed.

  7. #7
    Join Date
    Apr 2009
    Posts
    394

    Re: functino that returns nothing

    Quote Originally Posted by HanneSThEGreaT View Post
    A proper function, as you put it, should return something.

    A proper Sub, returns nothing...
    au contraire Monseigneur Great...

    ByRef...

    If an API call or user created sub changes one or more values that are passed in as arguements then does not that sub return something? And a function, API or otherwise, treated like a sub, does that not return nothing even though it is setup to? (symantics I know...)

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: functino that returns nothing

    Quote Originally Posted by vb5prgrmr View Post
    au contraire Monseigneur Great...
    Touche

    Quote Originally Posted by vb5prgrmr
    ByRef...
    Passing variables By Reference means that you are essentailly creating a pointer to that variable, and then change that. Passing object By Value, you are making a "copy" of the adressed variable.

    Quote Originally Posted by vb5prgrmr
    If an API call or user created sub changes one or more values that are passed in as arguements then does not that sub return something?
    No. A sub is just a sequence of statements, to do whatever, it returns nothing. If obviously you have created a sub, and another sub makes use of something changed / created in the previous sub, that is still not a function, although it works like it.

    A Function needs some return value as as pointed out by WoF, even if it Null, Empty or Nothing

    Quote Originally Posted by vb5prgrmr
    And a function, API or otherwise, treated like a sub, does that not return nothing even though it is setup to? (symantics I know...)
    Have a look at this very very interesting thread, dealing with API functions and API subs...

    http://www.codeguru.com/forum/showth...owindow&page=2

  9. #9
    Join Date
    Apr 2009
    Posts
    394

    Re: function that returns nothing

    Quote Originally Posted by HanneSThEGreaT View Post
    No. A sub is just a sequence of statements, to do whatever, it returns nothing. If obviously you have created a sub, and another sub makes use of something changed / created in the previous sub, that is still not a function, although it works like it.
    Exactly my point!

    Quote Originally Posted by HanneSThEGreaT View Post
    A Function needs some return value as as pointed out by WoF, even if it Null, Empty or Nothing
    But if you treat it like a sub when you call it, it then does not return anything and yes I do understand that a function must be declared as returning something but the point is treating it like a sub, it does not return anything...(at least that is what it seems like on the calling end.)


    Overall my point was/is to say that it can be confusing for some newbies




    Good Luck

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: function that returns nothing

    Quote Originally Posted by vb5prgrmr View Post
    Overall my point was/is to say that it can be confusing for some newbies
    That is why it is good that you braught this issue up So that the newbies could understand better

    Good work!

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: functino that returns nothing

    I think our OP has fled the playground. Hope he understood.

    Quote Originally Posted by vb5prgrmr View Post
    Exactly my point!
    ... treating it like a sub, it does not return anything...(at least that is what it seems like on the calling end.)
    To be even more pedantic you'd have to say, when called like a sub it still creates a return value, but the calling part does simply not use it.

  12. #12
    Join Date
    Apr 2009
    Posts
    394

    Re: functino that returns nothing

    ...or accept it...

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: functino that returns nothing

    Quote Originally Posted by WoF View Post
    I think our OP has fled the playground. Hope he understood.
    In that case, he / she is missing a lot of useful information....

  14. #14
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: functino that returns nothing

    Yes. Have we some more elaborations to add to this topic?
    Maybe we should hint on the miracle of the Call statement.
    Why do we have it?
    We can write FunctionName Param, why would we write Call FunctionName(Param)?
    Because we love the brackets?
    FunctionName(Param) cannot stand alone. Why?
    Because now it produces a value which MUST be used somehow, or so it seems.

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

Featured