-
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?
-
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;
}
-
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.
-
>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. :)
-
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.
-
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 :) )
-
Here is a tutorial on basic functions Section 2.2
Functions