Code lines

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";
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
lol, so a line can't be called?
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
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
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"; }


ummm, that explains alot, Ganado you understood perfectly.
Last edited on
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
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.
Well, the language could technically generate labels in the assembly for every line of code...

-Albatross
I agree, I don't see why being compiled prevents this from being possible.
Because that's not what the language does.
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.
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.