Results 1 to 17 of 17

Thread: Functions and Subroutines within a module

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    4

    Thumbs up Functions and Subroutines within a module

    Okay, wow.. I truly have no idea what to do... maybe someone can help.

    In class we talked about functions, subroutines and encapsulation but my teacher went through the material so quick that most of us were unable to comprehend what he did.

    If anyone is kind enough to help me with this HW assignment, then you would be highly appreciated!

    PROBLEM

    "The Focus of this assignment is Functions and Subroutines I want you to create one of each of the following:

    On each of these Functions and subroutines you NEED to practice Encapsulation. What I mean be this is that the only way you can communicate between the Functions/Subroutines and the rest of the application is through the passed parameters and returned values. No global values. No access to the form.

    The Functions and Subroutines should be in your Module.

    1) Create a Function that takes a decimal number and returns a hexadecimal value. Note that you will want to return a string.
    2) Create an alternate function to convert a decimal number to a hexadecimal value. However, this function returns True if the input is a valid number, and False if it is not a valid number. The function should take two variables (one being the input number to convert, and the second should be the result if the user submitted a valid number). Thus this is working somewhat like TryParse, which returns either True/False, and passes the result in a parameter.
    3) Create a Subroutine that will append a comment to a log file. You need to pass everything into the subroutine to make it work. Think what you need and pass it in.
    4) Create a second version of the log Subroutine that allows you to specify whether you want to append to the log file or overwrite the log file.
    5) Create a third version of the log file that does what you did in #4, but has an Optional field that is passed in. The purpose of this field is to play a beep when the subroutine is called. The Optional field will be False by default, but the user can enter a True value to have the code beep when it is run.


    Create an application that allows you to test each of the Functions and Subroutines you created. I would suggest having 5 separate buttons, each triggering the individual Function or Subroutine. Bring in whatever input you need, and display whatever output is generated. Make sure to document your code."



    I normally would not ask for help or even the answers but i have no F-king clue what to do or how to begin.


    again, if anyone is willing to help, you will be appreciated!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Functions and Subroutines within a module

    If you can't understand what you covered in class then you should go over one or more of the many beginner tutorials on the web. There's a link to one in my signature below. That will cover methods as well as the rest of the fundamentals.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Functions and Subroutines within a module

    Essentially what you need to do is create methods (subroutine or function) that accept parameters and optionally return a value. In Visual Basic .NET there are two types of parameters: ByVal and ByRef. A ByVal parameter is the default parameter style and it does not change the underlying value of the argument (the data that is passed in the parameter) whereas a ByRef parameter does change the underlying value of the argument. To demonstrate, take a look at this example:
    Code:
    Imports System
    
    Public Module Module1
    	Public Sub Main()
    		'ByVal example
    		Dim byval_example As Integer = -1 'byval_example = -1
    		Console.WriteLine(foo(byval_example)) 'byval_example = -1 (no change)
    		
    		'ByRef example
    		Dim byref_example As Decimal = 25.0D 'byref_example = 25.0
    		Console.WriteLine(foo(byref_example)) 'byref_example = -25 (changed)
    	End Sub
    	
    	Private Function Foo(ByVal bar As Integer) As String
    		'If the value is negative, then make it positive
    		If bar < 0 Then
    			bar = -bar
    		End If
    		
    		'Return the value
    		Return bar.ToString()
    	End Function
    	
    	Private Function Foo(ByRef bar As Decimal) As String
    		'If the value is positive, then make it negative
    		If bar > 0 Then
    			bar = -bar
    		End If
    		
    		'Return the value
    		Return bar.ToString()
    	End Function
    End Module
    Fiddle: Live Demo

    Encapsulation essentially helps minimize code and also eliminates unused variables. With this information, you should be able to do 1-4. The only one that may trip you up is number 5, and this is because there is the Optional keyword in Visual Basic .NET which means that (depending on your instructor) they may want you to use either of the following:
    Code:
    Private Sub Bar(ByVal param1 As String)
        '...
    End Sub
    
    Private Sub Bar(ByVal param1 As String, ByVal optional_param2 As String)
        '...
    End Sub
    Or
    Code:
    Private Sub Bar(ByVal param1 As String, Optional ByVal optional_param2 As String = "Foo")
        '...
    End Sub
    If it were me, I would use the former, but I'm not sure what you've been taught.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Functions and Subroutines within a module

    Yeah these are very basic things. My advise, take it one step at a time,


    1) Create a Function that takes a decimal number and returns a hexadecimal value. Note that you will want to return a string.

    If you have no idea how to do this then follow jmc advise or Google something like "Visual Basic create function" and you will find lots of help. Then try writing the code, if you can't get it to work then post back here and someone will be able to help. Always post your current code and explain what the problem is.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Functions and Subroutines within a module

    Unfortunately, we get this often. I aint saying you are lying but if you have an issue like this ask the teacher. Every thing you need to know would of been covered in class.

  6. #6
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Functions and Subroutines within a module

    Quote Originally Posted by ident View Post
    Unfortunately, we get this often. I aint saying you are lying but if you have an issue like this ask the teacher. Every thing you need to know would of been covered in class.
    well obviously if it was covered in class then he wouldn't be here. We are here to give assistance in VB, it should not matter
    Last edited by BCVB; Mar 7th, 2018 at 06:07 PM.

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Functions and Subroutines within a module

    Quote Originally Posted by BCVB View Post
    well obviously if it was covered in class then he wouldn't be here... We are here to give assistance in VB, it should not matter
    That's not obvious at all. What if it was covered but the student was playing around on their phone and not paying attention (not suggesting that is the case, but it can't be ruled out)?

    And it does matter, at least to me and some other posters. Reading through that list of "to-do's" and not knowing where to begin is a huge red flag. And it's not solved by asking other people to do your work for you (which I'm not suggesting the OP was doing, by the way). That lets you slide until the next assignment, when you'll be even more lost and need someone else to bail you out yet again. Keep that up and you end up with a degree you know nothing about, and get a job that you aren't capable of doing.

    I will personally be glad to help people out if they show genuine effort to try to write the code themselves, but I won't take a bullet list of what the code needs to do and spit out functional code for them. If you disagree with me, that's completely fine with me. I wouldn't hold it against anyone who posted the code to check off all 5 of the requirements above.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Functions and Subroutines within a module

    Quote Originally Posted by BCVB View Post
    well obviously if it was covered in class then he wouldn't be here.
    The fact that someone didn't understand something does not in any way indicate that it was not covered in class. If the teacher really did go over things so quickly that most people didn't understand and that is not brought to their attention then things are unlikely to change and the cycle continues.
    Quote Originally Posted by BCVB View Post
    We are here to give assistance in VB, it should not matter
    That's your opinion. There are plenty of people who could do far more for themselves than they do but through ignorance, laziness or a combination of the two, they take the easy option and just wait for someone else to do their thinking for them. There is a ton of information on the web already and pretty much everyone has easy access to it. If you don't bother trying to use that information but just ask others repeat what's already out there then I want to encourage you to stop doing that because it may help you get marks in a class but it's not the best way to learn how to code in VB.

  9. #9
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Functions and Subroutines within a module

    Yea but my point is that ya'll are quick to assume this. How could you possibly know what he was doing in class, and what if you are wrong and he is telling the truth? Also you do realize that just because he is taking this class doesn't mean he wants to become a programmer or get a job that requires VB. What if this is just a requirement to graduate and he wants to become a cloud architect or a network engineer? And lastly, if you don't want to help, why are you even here breh.

  10. #10
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Functions and Subroutines within a module

    Quote Originally Posted by jmcilhinney View Post
    The fact that someone didn't understand something does not in any way indicate that it was not covered in class. If the teacher really did go over things so quickly that most people didn't understand and that is not brought to their attention then things are unlikely to change and the cycle continues.

    That's your opinion. There are plenty of people who could do far more for themselves than they do but through ignorance, laziness or a combination of the two, they take the easy option and just wait for someone else to do their thinking for them. There is a ton of information on the web already and pretty much everyone has easy access to it. If you don't bother trying to use that information but just ask others repeat what's already out there then I want to encourage you to stop doing that because it may help you get marks in a class but it's not the best way to learn how to code in VB.
    read my latest reply breh, also what if he doesn't want to learn, he just wants to pass a class he has no interest in and is just forced to take???? I bet you took classes in college where you didn't put forth an effort to learn.
    Last edited by BCVB; Mar 7th, 2018 at 06:25 PM.

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Functions and Subroutines within a module

    Merq, as others have suggested, the key with programming and many, many other things in life, is to break things down into small pieces. I think people new to programming like to bang out a ton of code to accomplish 20 things all at once and then try to run the program for the first time at the very end, and get overwhelmed by all of the errors and unexpected behavior. Break things down into small pieces, test the program often, get small functionality working, and then build on it.

    For example, for number 1, maybe start by writing a function that receives a numeric value and returns the same value that was passed to it. Getting that working should be fairly straightforward if you are familiar with creating functions and calling them.

    Then, modify the function so that, instead of returning the numeric value passed to it, it returns a String value that contains a String version of the number passed to it. So, if the function was passed the number 123, it would return the String "123".

    Then, read up on how to convert a numeric value to Hex, yada yada yada, and you've got part 1 done.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Functions and Subroutines within a module

    We've heard of some pretty poor teaching practices often enough that I'm no longer surprised by anything. There are some bad classes out there, which is unfortunate. With coding, you either love it or you don't, and a bad experience will certainly have a pretty severe impact on your view towards either coding, or your ability with it.

    I did a very small amount of teaching, myself (though not in coding). That was enough to show me that I don't much like it, but also to show me that if you present some topic one way, there will be people who understand it and people who do not. If you present it a different way, the same sets will be there, but it will be different students. The key is to present a concept in the way each student will best learn it, which is a hard task for anybody.

    So, I feel that the first two answers here sum up all I would say about it. The tutorial that JMC linked to isn't JUST a tutorial, it's also a reference. That's a very handy thing to have at hand. When I started in coding, there wasn't really an internet (not much of one, anyways), so all references were books. I wanted two of them on hand, because each might cover the same thing, but cover it in different ways. These days, you have the web, which is an amazing resource. You still want a tutorial as a reference, though, so that link is really good. There are other options out there, too, but you likely don't need more than one for most things. Meanwhile, the second answer provided some examples, which is also quite useful. Between the two, there's probably enough to get a good start at it, and maybe more. So, start with that, and if you need more, come back for that.
    My usual boring signature: Nothing

  13. #13
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Functions and Subroutines within a module

    Quote Originally Posted by BCVB View Post
    read my latest reply breh, also what if he doesn't want to learn, he just wants to pass a class he has no interest in and is just forced to take???? I bet you took classes in college where you didn't put forth an effort to learn.
    Now who's quick to assume? You would lose that bet x1000.

    Does being forced to take a class mean you deserve a passing grade even if you don't want to learn the material? I think not. Feel free to disagree.

  14. #14
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Functions and Subroutines within a module

    Quote Originally Posted by OptionBase1 View Post
    Now who's quick to assume? You would lose that bet x1000.

    Does being forced to take a class mean you deserve a passing grade even if you don't want to learn the material? I think not. Feel free to disagree.
    Chill man, we're just here to help a dude trying to a pass a class. If you don't want to help then why bother commenting. If you truly are passionate about VB, there shouldn't be an issue to assist someone, no matter how low of an understanding they may have.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Functions and Subroutines within a module

    Quote Originally Posted by BCVB View Post
    I bet you took classes in college where you didn't put forth an effort to learn.
    I never asked strangers to volunteer their time to do my work for me so that I could pass such a class.

    If the OP really isn't interested in learning or even doing enough work themselves to pass then, quite frankly, they don't deserve our help and I don't think anyone should encourage such people by providing it. I don't think that that is an accurate reflection of the OP. I think that they just don't understand what they were taught, possibly because of poor teaching (it wouldn't be the first time). The fact is though, not understanding what you were taught does not mean that your only option is to dump your assignment on strangers. There is still plenty that the OP can do for themselves first. They should do what they can for themselves first and then come to us for the bits they can't. If you've tried and failed, I'm here to help. If you haven't tried then you haven't failed, so asking for help is premature.

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Functions and Subroutines within a module

    I'd say that cover has been beaten to death. Let's leave it there. It doesn't help the OP who asked the question.

    EDIT: Crossed over JMC, but the point is still the same: We don't know, so let's stick to answering the question rather than questioning the question.
    My usual boring signature: Nothing

  17. #17
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Functions and Subroutines within a module

    Where is OP anyways...

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