#include <iostream>
#include <conio>
int subtraction (int,int);
int main ()
{
int x=5,y=3,z;
z=subtraction (7,2);
cout<<"The first result is "<<z<<'\n';
cout<<"The second result is "<<subtraction(7,2)<<endl;
cout<<"The third result is "<<subtraction(x,y)<<endl;
z=4+subtraction(x,y);
cout<<"The fourth result is "<<z<<'\n';
getch();
return 0;
}
int subtraction(int a, int b)
{
int r;
r=a-b;
return (r);
}
Why the above program need declare in line 4, but the below program no need declare like the one above? TQ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <conio>
void duplicate (int &a, int &b, int &c)
{
a*=2;
b*=2;
c*=2;
}
int main()
{
int x=1,y=3,z=7;
duplicate (x,y,z);
cout<<"x="<<x<<", y="<<y<<", z="<<z;
getch();
return 0;
}
You only need forward declarations if you are unable/unwilling to write the function definition before it is used. For example, your first example is a case of unwillingness because you can move lines 19-24 up to replace line 4 without issues.
There are cases, usually writing classes, where forward declaration is REQUIRED because there would be no other way of finishing the declarations or definitions of methods or variables.
#include <iostream>
#include <conio>
int subtraction(int a, int b)
{
int r;
r=a-b;
return (r);
}
int main ()
{
int x=5,y=3,z;
z=subtraction (7,2);
cout<<"The first result is "<<z<<'\n';
cout<<"The second result is "<<subtraction(7,2)<<endl;
cout<<"The third result is "<<subtraction(x,y)<<endl;
z=4+subtraction(x,y);
cout<<"The fourth result is "<<z<<'\n';
getch();
return 0;
}
How about line 4 now? I mean in this line 4, we need to declare variable a and b, but in the original program in the 1st code(line 4), we don't need to declare variable is it? Why? Thanks...
Your compiler needs to know what a function looks like before it can call that function. Using a forward declaration is called prototyping. You're not putting a fully formed function in, you're just adding in a prototype of the function:
int subtraction(int, int); // Prototype takes 2 ints, is named subtraction, and returns int
In the second example, you're actually defining the function, which means not only are you telling the compiler what it looks like, you're also telling it what it does. Since you're telling it what it does, it needs more information, such as what to call the two ints that you're passing it:
1 2 3 4
int subtraction(int a, int b) // Naming the variables...
{
return a - b; // ...so that they can be used here.
}
You'll also note that I cleaned up your code a little bit. You can actually return mathematical evaluations, or even other function calls, not just variables.
#include <iostream>
#include <conio>
int subtraction (int,int);
int main ()
{
int x=5,y=3,z;
z=subtraction (7,2);
cout<<"The first result is "<<z<<'\n';
cout<<"The second result is "<<subtraction(7,2)<<endl;
cout<<"The third result is "<<subtraction(x,y)<<endl;
z=4+subtraction(x,y);
cout<<"The fourth result is "<<z<<'\n';
getch();
return 0;
}
int subtraction(int a, int b)
{
int r;
r=a-b;
return (r);
}
@atjm88: More than asking simple Yes/No questions you should actually try them. It is far simpler and much more informative. If you don't have a compiler at hand, try www.ideone.com, an online compiler.
Also in your last sample your prototype should be: void duplicate(int&, int&, int&);