Code lines

Mar 29, 2015 at 10:18pm
I have been wondering, can I call a code line just as I can call a function by writing its name with parentheses?

for example

5. int main ( ) {
6.
7. //make a call to the code on line 11
8.
9. }
10.
11. std::cout<<"I am on line 11. \n";
Mar 29, 2015 at 10:20pm
uhm, What? xD

There is a function for this, called goto but uhm... Pretend I never said that. goto does not exist.

Srsly though, dont fucking use goto
Last edited on Mar 29, 2015 at 10:21pm
Mar 29, 2015 at 10:27pm
lol, so a line can't be called?
Mar 29, 2015 at 10:29pm
A line could technically be called. But no it cant. The question is. Why in the world would you want to call a line? Just use a loop instead.

Edit: Should add that the non existant goto does not go to a specific line of code, but rather a specific place in your program. Atleast thats how it would have worked, if a thing called goto existed.
Last edited on Mar 29, 2015 at 11:22pm
Mar 29, 2015 at 11:19pm
Here's some reasons why a call to certain line of code can't and shouldn't be a feature:

a) would make source code one hell of a pain to debug
b) disallows freedom of formatting
c) The following scenerio:
1
2
3
4
5
6
7
8
int main()
{
    int a = 2;
    EXECUTE_LINE(7);

   
    std::cout << "Line 7?" << std::endl;
}


Oh let's say I want to add, well, literally any other feature to my code

1
2
3
4
5
6
7
8
9
10
11
int random()
{ return 5; }

int main()
{
    int a = 2;
    EXECUTE_LINE(7);


    std::cout << "Line 7?" << std::endl;
}

Didn't change main at all... but... now you're in some recursive infinite loop or something.

But it would be funny to see a language that implemented this just to see how bad you could possibly make spaghetti code. :D

________________
And in case I totally misunderstood the poster, and you didn't literally mean going to a numbered line, goto labels are still a mess in places where simple loop would do the same thing, as TarikNeaj mentioned.
Last edited on Mar 29, 2015 at 11:21pm
Mar 30, 2015 at 3:29am
As Ganado pointed out, calling a line of code by line number would be impractical since every time you edited the file, the line number would change. This is why we call functions by name. Even assembly code refers to functions by name for this reason.

Just turn it into a function. In your first example:
1
2
3
4
5
6
void L11();
int main ( ) {
L11();
}

void L11() { std::cout<<"I am (oops, WAS) on line 11. \n"; }


Mar 30, 2015 at 8:07am
ummm, that explains alot, Ganado you understood perfectly.
Last edited on Mar 30, 2015 at 8:09am
Apr 1, 2015 at 5:10pm
No as in your example cout cannot be called . It is also one of the function only but it is inbuilt. Its definition will be in iostream.cpp file .
Last edited on Apr 1, 2015 at 5:11pm
Apr 1, 2015 at 8:20pm
Guys, calling a line of code isn't impractical, it's impossible. Did everyone here suddenly forget that C++ is not an interpreted language? Lines don't exist in the executable.
Apr 1, 2015 at 8:25pm
Well, the language could technically generate labels in the assembly for every line of code...

-Albatross
Apr 1, 2015 at 8:52pm
I agree, I don't see why being compiled prevents this from being possible.
Apr 1, 2015 at 9:09pm
Because that's not what the language does.
Apr 1, 2015 at 9:18pm
We're just talking hypothetical here, obviously C++ doesn't currently keep track of source line markers + having explicit line jumping would make a ton of optimization impossible.
Apr 1, 2015 at 10:07pm
There is labels and the goto option, but usually it's not considered good practice to use it, and you only should in very few situations when there is no better alternative (though... there almost always is)

I really suggest you think of some way you can structure your code to use functions or something that doesn't require skipping to a part in the code but, if you want you can use the goto option with labels. To make a label, you simply put

labelname:

on a line. Like uh....

int count = 0;
loopStart:
printf("%d\n",++count)
if(count < 10)
goto loopStart;

This won't go to specific line numbers, but you can place labels on the lines you'd want to go to. This way also, it won't be hard if the line number changes.

Though again, it's not really a good thing to use the "goto" option, but it is a feature so it's good to learn about it.
Topic archived. No new replies allowed.