Thread: return ?????;

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    101
    Rep Power
    12

    Question return ?????;


    Is there a way to return to a certain line rather than a certain function?? If so how do you do this?? Any help would be appreciated...Could not find answer on net. I am a newb lol.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    There are goto statements and longjmps, but they are a sign of bad programming practices in almost all instances. There are rarely needs to use them, use breaks, continues, etc.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    101
    Rep Power
    12
    ok...how would i do this...I am not worried about poor signs of programming lol I am just messing around with C++ seeing what I can do. Eventually that will matter 2 me, but for now like i said just want to figure out what everything does. I am taking a C++ class in school but our teacher isn't a very good teacher, and we are into our 3rd week of school and barely past the whole...Hello World program...actually we still are basically I mean we are only giving output in that class so i am just trying to figure it out at home.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    There is no point in learning to do programming a sloppy and inefficient way. Take the time to learn how to do things properly. Subroutines and do/while/for/switch were invented to attempt to elminiate these sloppy practices. I can almost guarentee that if a prospective employer sees code of yours with gotos scattered around in it you will not get a call back!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    basically you dont want to do that stuff until you understand why its not a good practice but also realize that it is a necessary part of the language. until then just listen to your teacher.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    101
    Rep Power
    12
    thanks for the advice but for now I am not so worried about an employer as I am getting good grade in high school. but I do appreciate the advice, thanx a ton, but for now I am thinkin that goto statement and longjmps are looking pretty good...In the future once i learn alternative and better ways then I shall do them. see the problem is I really dont know hardly anything about C++ at this point and I am basically writing a program with my knowlage in math. As far as the different functions and stuff go, that is something that I will have to wait for in my C++ class. To be honest the program I am writing now does not have alot of significance. I am just expieramenting...and your longjmps and goto statements are good for expieramenting. Like i said though I will remember that in the future when looking for an employer, and all that.
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    This a subroutine with a couple of loops, it isn't that difficult:

    Code:
    #include <stdio.h>
    
    //declaration must preceed use...
    float multValues(float a, float b);
    
    //function body of subroutine (you can combine these two)...
    float multValues(float a, float b){
        return a*b;
    }
    
    //use elsewhere...
    
    int main(){
        float x, y, retVal;
        int i;
    
        x = 1.234;
        y = 4.567;
    
        printf("a while loop...\n");
        while (x < 100){
            retVal = multValues(x, y);
            printf("\t%f\n", retVal);
            x += x;
        }
    
        printf("a for loop...\n");
        for (i=0; i<10; i++){
            retVal = multValues(x, y);
            printf("\t%f\n", retVal);
            y += y;
        }
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally posted by infamous41md
    basically you dont want to do that stuff until you understand why its not a good practice but also realize that it is a necessary part of the language. until then just listen to your teacher.
    In 15 years of using C professionally I have never used a goto, what makes you say it is a necessary part of the language?

    I have also never used a longjmp, however I have seen it used to perform a sort of poor man's multithreading in DOS and embedded systems, but with a modern desktop OS, there is no need to resort to that.

    Originally posted by lildrummerboy
    thanks for the advice but for now I am not so worried about an employer as I am getting good grade in high school.
    If goto is an anathema to structured programming, it certainly is to object oriented programming! And using it will not get you a good grade. Any serious course would actually drop you marks for using it, and probably would not even teach it. There is no need to use or even learn every aspect of a language, and its darker recesses are probably best avoided. It is better to learn good practices first. Learning the bad ones is useful only in helping to understand, fix, and improve other peoples bad code. That may be important if you are involved in maintaining legacy code or working in a team, but you will not get that kind of responsibility until you learn to code well in the first place.

    You may ask that if a language feature is bad, why does it exist? Well for a number of possible reasons. In the case of goto, it is partially to support older programming styles. If you invent a language, and want everyone to use it, then you want to make it easy for everyone to adopt, so you support thier style. Just as C++ supports C style structured programming, C supports 'spagetti' programming beloved of BASIC and Fortran programmers. Porting existing code is easier if the language supports the coding style of the original language.

    To quote Brian Kernighan who with Dennis Ritchie invented C: "C has a goto statement and labels, so you can branch about the way you used to. But most of the time goto's aren't needed. The code can almost always be more clearly expressed by for/while, if/else, and compound statements.". However, I do not think he has been string enough in discouraging goto here, and many poor programmers have used that "almost always" as an excuse! In my experience, if you get to the point where a goto leads to greater clarity than the alternatives, then your program design needs looking at. This is especially true when using C++ for object oriented programming.


    Clifford
  16. #9
  17. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    i donno i guess i figured it was necessary b/c i've seen goto calls in various parts of the linux kernel and figured those guys know what they're doing; and i've read lots of articles/literature that has said goto and longjmps are the simplest means of accomplishing certain tasks. i.e. why go out of your way to make something complex when you can accomplish it with a simple goto call. as long as you understand what you are doing i dont see the reason why you shouldn't do it. that said, i've only been programming for 2 years so im sure i have a lot to learn...

    edit: i was just thinking some more.. i've noticed that in cases where one might want to use a goto call - to not use one you need perhaps extra flag variables and extra tests to perform the logic. now if you were in a situation where you had to squeeze every drop of efficiency out of your code, wouldnt goto make more sense for your purpose? so it is still a necessary part, even if most of the time you will never use it. also that K&R quote, they say "most", not never. :)
    Last edited by infamous41md; September 23rd, 2003 at 01:49 AM.
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    infamous41md

    It is a point of view that has some validity. The problem is that we all think we know what we are doing, and it always looks simple when we write it. That may be the case, and K&R and Stroustrop both cite 'acceptable' uses, to do with performance, clarity, and machine generated code. However I've not come accross a compelling reason use it. One of the often cited use, to to exit deeply nested code on error is better handled by exceptions in C++, and if clarity were your aim, you may have acoided the deep nesting in the first place. The performance argument is usually invalid, partly because modern optimizing compilers are far cleverer than people generally assume, but also because many programmers add such 'hand optimizations' before they have even determined if a structured version was too slow in the first place.

    It probably helps to know where I am comming from. I work on real-time embedded systems in commercially valuable products. I work as part of a software team, my code is not the only code I see or modify, and my code is seen and modified by others. In this environment clarity and maintainability is key. Also, less experienced engineers are less expensive, the imposition of strict standards of language usage and coding style enables us to use the lower cost engineers in a controlled manner so that thier work is valuable and cost effective (errors cost money), and they do not get into trouble. They also become better and in time more expensive engineers.

    Also there is always just plain peer pressure to consider. If any of my team saw a goto in my code, they would probably justifiably mock and berate me!

    Clifford
  20. #11
  21. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    cool it's interesting to hear the views of people much more experienced than myself :) i have yet to work in any group environment in programming. in a way im almost dreading it b/c i have a feeling some of my "hackish" methods are going to be beaten out of me. i usually try to complete my task in the simplest and most efficient manner (as im sure we all do), and it seems that simple is a close relative to ugly, at least in my code. example, code like this:
    Code:
    //calculate tcp header length  
    tcphdrlen = (  ( (*(u_char *)(snifbuf + ETH_HDRLEN + iphdrlen + LSF_TCP_HL))  >> 4) << 2 );
    not exactly fun to match up those parentheses, but it certainly works and is the most efficient way imho if you already know the offsets. is that something that would be frowned upon?
    Last edited by infamous41md; September 23rd, 2003 at 01:55 PM.
  22. #12
  23. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Clifford:
    > To quote Brian Kernighan who with Dennis Ritchie invented C

    You incorrectly attribute credit to Brian Kernighan for inventing C with Dennis Ritchie. Although Brian co-authored K&R with Dennis in 1978, the language was pretty well developed by this stage - initially by Dennis up to about 1973, in conjunction with Ken Thompson the inventor of B, with additional contributions up to 1977 from several others. There's an article on Dennis Ritchie's home page that describes the development of C:

    http://cm.bell-labs.com/cm/cs/who/dmr/chist.pdf

    Brian's home page is at:

    http://cm.bell-labs.com/cm/cs/who/bwk/index.html
    Last edited by BigBadBob; September 23rd, 2003 at 06:03 PM.
  24. #13
  25. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    infamous41md:

    Your bit of code may be efficient and possibly make tremendous sense to all that see it in context, but the thing to keep in mind is they poor bastard who is going to clean up after you (keep in mind that poor bastard is just as likely to be YOU!). It may be a good idea to throw in a line or two explaining what the code is doing (it is stripped out by the compiler, be free with it). Or, if the rest of the code uses the same style, do as I often do and demand that the person following behind is at least as compentant as myself. Often during debugging I will split a line like that into several lines so it is easier to see what is going on, but I would probably leave it just the way you have it once I am sure it working. The real key to writing good, maintainable code is to pick up something you wrote and haven't looked at for a couple of years and see how long it takes you to trace through the logic. If you find it to be a struggle, why should you think some guy who never saw it before would find it easier? My personal style is to use 'self-documenting' variable and subroutine names and go lite on the comments (I believe in Hungarian notation). I like to have the most code on the screen at one time as is practical, though I find the indentation to be a BIG help. There are C beautifiers (and I am sure C++) that will set the code to certain styles (and often will reveal missing/extra braces!) making it more familiar. The most important thing to any development project is consistancy. If your code always looks the same (for better or worse), at least they know what kind of dumb mistakes you make and can anticipate them.

    At one of my previous jobs I was in charge of coding and naming standards for the company. I firmly believe in them as a way to rapidly understand code and make changes. In fact, since the other developers all were learing the language at the same time (VB, in case you are interested), all our code wound up looking almost exactly the same and you had to read the file header to figure out who initially wrote it.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  26. #14
  27. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Originally posted by mitakeet
    There are goto statements and longjmps, but they are a sign of bad programming practices in almost all instances. There are rarely needs to use them, use breaks, continues, etc.
    Is it OK to use this:
    Code:
    getIP:
    	printf("Please, enter an IP: ");
    	gets(handle);
    
    	cts.s_addr = inet_addr(handle);
    	host = gethostbyaddr((const char *)&cts, sizeof(struct in_addr), AF_INET);
    	if(host == NULL){
    		error();
    		goto getIP;
    	}
  28. #15
  29. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    NO!!!!! never use gets()! never ever for any reason. let me find some interesting code to show you why, brb:D
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo