Thread: Return function

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    50

    Return function

    Hi guys, I've read about return and didn't understand that much about its functionally in C programming .
    what's confusing me is that if I have code after return function then the PC will neglect all the code rows that're found after return function, so how the PC neglect them if they are instructions in my program?
    I mean for instanct like this -

    Code:
    /*code1 does something*/
    return 0;
    /*code2 does something else*/
    so what's found after return 0 is neglected , so how the PC is neglect them although there're instruction-code- in the program itself? really weird, so /*code2 does something else*/ is trash , what does that mean how the PC neglects them although they are code found in the program which means they are instructions that must be executed? thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Brian_Teir
    what's confusing me is that if I have code after return function then the PC will neglect all the code rows that're found after return function, so how the PC neglect them if they are instructions in my program?
    Because that's the way it is. It is like asking why the code in the else branch of an if statement is not executed when the condition is true. It is just how it was designed to be. If you write garbage, you get garbage.

    Quote Originally Posted by Brian_Teir
    so what's found after return 0 is neglected , so how the PC is neglect them although there're instruction-code- in the program itself? really weird, so /*code2 does something else*/ is trash.
    It looks like /*code2 does something else*/ is unreachable, i.e., you might as well delete that piece of code because it is useless, and the compiler could treat it as if it didn't exist, and would likely warn you about it.

    In other cases, it may depend on the flow of control, e.g.,
    Code:
    if (condition())
    {
        return;
    }
    foo();
    So what we have here is an early return if condition() evaluates to true. If it doesn't, the flow of control goes on to foo().

    Quote Originally Posted by Brian_Teir
    what does that mean how the PC neglects them although they are code found in the program which means they are instructions that must be executed? thanks.
    Actually, because they are unreachable, they are instructions that must not be executed. As I said, garbage in, garbage out. Program uselessly, get a useless program. Write code that is unreachable, get code that is unreachable junk.
    Last edited by laserlight; 04-22-2020 at 04:03 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return pointer to a vector of objects as a function return
    By Adaptron in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2016, 09:23 AM
  2. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. return a function
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2002, 03:56 PM
  5. return in a function
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2002, 10:37 AM

Tags for this Thread