Errors/improvements marked with
red..
#include <cmath>
#include <iostream>
using namespace std;
void Square(int &a, double &b)[COLOR=Red];[/COLOR] [COLOR=Red]// Semicolon after function declaration [/COLOR]
int main()
{
[COLOR=Red]// You better put them variables inside main :)[/COLOR]
int a;
double b;
cout <<"please enter an integer to be squared" << endl;
cin >> a;
[COLOR=Red]// I would put some error checking here, but OK, we'll leave that for now[/COLOR]
cout << " please enter a number with a decimal to be squared" << endl;
cin >> b;
[COLOR=Red]// Same here..[/COLOR]
Square(a, b); [COLOR=Red]// That is just fine, no problem
[/COLOR]
cout << "Square a = " << a << endl; [COLOR=Red]// Don't do a*a, then you are actually calculating OUTSIDE of your function..
// Your assignment wants you to do that inside... So just DISPLAY a, Square function will modify it to become a*a.[/COLOR]
cout << "Square b = " << b << endl;[COLOR=Red] // Same here, just display the variable, mod it in the function.. What else would the function do?![/COLOR]
return 0;
}
[COLOR=Red]// You need to make a definition for Square();[/COLOR]
void Square(int &a, double &b)
{
a = a*a; [COLOR=Red]// The function now modifies a [/COLOR]
b = b*b; [COLOR=Red]// and b.. And all you'll have to do, is display it :)[/COLOR]
}
Ok, you need to understand parameter passing... Unless we're talking about arrays, arguments in C++ are passed
by value by default. That means, your integers and strings and whatever arguments are
copied, their values are literally
copied into other variables for the function. That is good, because that means your function won't change your variables outside itself, like important global variables etc. You only gives the function permission to
read data this way, your passed variables are still the same. If the function is gonna do something, it will then be through return values. Example:
int add(int a, int b)
{
return (a + b);
}
See that? This function takes it by value. No way it can change whatever variable you put into a or b. It just returns a value, that's the only way it has to give you data.
Look at this:
void mess(int a)
{
a = 15;
}
What will this do if you say this:
int main(int argc, char **argv)
{
int x = 10;
mess(x);
// Display x here....
return 0;
}?
Nothing. It has no power over it's argument at all. X is still 10, although mess() changed it's input to 15. That is because mess() changed another variable,
where the content of x was copied to. That variable contained the value 10, mess() changed it to 15, nothing happens in main().
You got that? So it's references. Now, your program does not pass your arguments by value anymore, the are not copied. Instead, their
memory addresses are passed to the function, which means that the function will
directly modify the argument.
If we now change mess() to this:
void mess(int &a)
{
a = 15;
}
Look, it takes a reference. That means, whatever variable you put into mess(), that evil dude is gonna change it to 15. So in main, x would no longer be 10, it would actually
be 15. So whenever you use functions with references, you actually modifies the variables passed to it.
Examples from the real world:
Look at a function like sqrt() (square root). It takes a number and returns a number. It does not modify the number it takes! It does not pass by reference (or if it does, it's const-reference, you'll come to that later :D)! That way, you can safely pass it any variable, knowing nothing bad will happen to it without your permission.
You have to do:
x = sqrt(x);
to modify x.
So another example, with references... This is more common in C, because there you would like to return an error value, while using pointers (they don't have references in C) to pass by reference and modify. An example of this is sprintf(), which takes a "buffer" argument it fills. How the heck can it modify your variable like that?! References...
Here's the function declaration of sprintf.. Notice the *buffer which can be modified by the function.
int sprintf( char *buffer, const char *format, ... );
You might want to look up this function for more about it..
I hope you got that, I'm no teacher.. :D Just ask if there is anything, I can repeat myself in some other way or something :D