Q on sample program

This is a discussion on Q on sample program within the C++ Programming forums, part of the General Programming Boards category; #include <iostream> // Prototypes int WidthInFeet(); int WidthInInches(int feet); int main() { // Initialize variables by calling functions. int feet ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    13

    Q on sample program

    #include <iostream>

    // Prototypes
    int WidthInFeet();
    int WidthInInches(int feet);

    int main()
    {
    // Initialize variables by calling functions.
    int feet = WidthInFeet();
    int wd = WidthInInches(feet);
    // Display results.
    std::cout << "Width in inches = " << wd;
    return 0;
    }

    int WidthInFeet()
    {
    int feet;
    std::cout << "Enter width in feet: ";
    std::cin >> feet;
    return feet;
    }

    int WidthInInches(int feet)
    {
    return feet * 12;
    }

    I'm a new to C++ and I understand that it reads up to down. However, I don't understand how the last part could return a number and then that number is returned to the out line in the main function. Can someone please explain this?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    The way I understand it.. in simplest terms... When you compile this code.. it "sees" you declared those functions... now when you call upon it it "jumps" to the function.

    I myself do it the other way...

    Code:
    #include <iostream>
    using namespace std;
    
    int WidthInFeet()
    {
      ///blah blah
    }
    
    int WidthInInches(int feet)
    {
      return feet * 12;
    }
    
    int main()
    {
      int feet = WidthInFeet();
      int wd   = WidthInInches(feet);
    
      cout << wd;
     
      return 0;
    }

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    int wd = WidthInInches(feet);
    Effectively this line calls the WidthInInches function, and passes the value in 'feet' to it. The function does the conversion, then returns a value. This value is stored in 'wd'. 'wd' is then passed to 'cout', which outputs it to the screen.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >However, I don't understand how the last part could return a
    >number and then that number is returned to the out line in the main function.
    Remember that the source code and the executable program are very different things. The source isn't interpreted in a linear manner as you're probably thinking, it's compiled (in a linear manner, that's why you need prototypes) and linked into an executable file. That file is then loaded into memory and executed. From there on you're pretty much working with addresses. At any point in the execution, you can have an instruction that causes the flow of execution to jump to another address, execute the instructions there, then jump back and copy the result. We only use the source code as a reference for how the program will execute because it's convenient.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    13
    Thanks for the help guys but I have 1 more question to clarify prelude's reponse. You said the source code isn't linear but it's compiled linearly. What exactly is compiled lineraly? As you can see, I'm new to C++ and I'm trying to get a head start before I start my Comp Sci courses next year at college so excuse me if that's a bad question.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Basically what happens during execution is that the program is loaded into memory. After it gets done setting some stuff up, it calls the main function which means that it goes to that memory address and starts executing whatever instructions are there. Prior to the call it copies the address of where it is coming from so it knows where to return to. This happens every time you make a function call. Its kinda like leaving a trail of bread crumbs.
    When you return a value from a function a copy is placed in a particular location and then the program jumps back to the location from whence it came and can now look at that particular location and it has access to that return value.

    I hope that made it a little clearer (if not well sorry )

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    Here is a tutorial on basic functions Section 2.2
    Functions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. Help with sample tax program
    By DaMonsta in forum C Programming
    Replies: 1
    Last Post: 05-15-2003, 04:45 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21