SO, after doing practicing some programs from my book; I noticed that they always define the function at the beginning before INT MAIN(); and defining the function after the end of INT MAIN(); like the two examples below. Each example below is different. The first Declares the function before int Main and defines it after Int main. the Second declares and defines the function BEFORE int Main. Also, can you define a function before you declare it? Thanks.
#include <iostream>
float convert(float); //prototype
int main()
{
float fahrenheit;
float celsius;
std::cout << "Please enter the temperature in Fahrenheight: ";
std::cin >> fahrenheit;
celsius = convert(fahrenheit);
std::cout << "\nHere's the temperature in Celsius: ";
std::cout << celsius << "\n";
return 0;
}
//function to convert fahrenheit to celsius
float convert(float fahrenheit)
{
float celsius;
celsius = ((fahrenheit - 32) *5) /9;
return celsius;
}
. BUT Sometimes, they have it like this:
#include <iostream>
class Tricycle
{
public:
int getSpeed();
void setSpeed(int speed);
void pedal();
void brake();
private:
int speed;
};
// get the trike's speed
int Tricycle::getSpeed()
{
return speed;;
}
// set the trike's speed
void Tricycle::setSpeed(int newSpeed)
{
if (newSpeed >= 0)
{
speed = newSpeed;
}
}
//apply the brake on the trike
void Tricycle::brake()
{
setSpeed(speed - 1);
std::cout << "\nBraking; tricycle speed " << speed << "mph\n";
}
In this one, the functions of the class is defined at the beginning. So, can you please clarify if the location of defining and declaring functions matter? Say you were to declare and define functions at the very end of a program source code, do the functions still work if written on top?
Also, can you define a function before you declare it?
You don't need to. If you define a function, you also declare it. The only reason for declaring functions (excluding member functions with in a class) is to allow other function to call it before it is defined. That is, they know what parameter(s) it needs and what it will return. How the function does this (i.e. it's body) doesn't matter until compile time.
Thanks again Mathhead200. That actually helped a lot.
I didn't know that if you defined a function, you also declared it.
So, you declare a function if you want to reuse it.
Other functions can call other functions?
So is each scope a function itself?
So once you declare and define a function, each scope could use that function without rewriting it right?
But must you declare and define the function outside of EVERY scope?
You can't declare and define it, say, in the int main() function right? You must do it "outer space", right?
I'm not exactly sure what you are asking. No, you can not define a function within another function, and it makes little sense as the function would leave scope (although some other languages allow this behavior.) As for the difference between a function declaration and definition see the following example:
#include <iostream>
#include <string>
usingnamespace std;
/* Here we are forward declaring a function named "message"
that takes no parameters and returns a string. However,
this function has not been defined yet. Even though there is
enough information to use this function, if we were to try to
without defining it, we would get a linker-error. */
string message();
int main() {
/* Even though message() has yet to be defined, the compiler know it exists
and has enough information for the following line to compile.
(That is, it knows it exists, needs no parameters, and returns a string
which is valid in this context, i.e. a string can be "cout"-ed.) */
cout << message() << endl;
return 0;
}
//Here we actually define how our message function will run.
string message() {
return"Hello, world!";
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
/* Here we are defining a function named message
that takes no parameters and returns a string. */
string message() {
return"Hello, world!";
}
int main() {
//And here we can use that previously defined function method "message"
cout << message() << endl;
return 0;
}
Thanks Mathhead for spending time on my questions with a detailed answer. I've started reading the tutorial on the site - it's WAY better than my book - and it taught me the fundamentals of functions all over again which answered most of my questions regarding them. And your answer strengthened my understandings of how and what they can do even more. Thanks again.