I've written a simple void function (provided below), and I had a very simple question that I'm not sure about.
In my function I'm referencing b, PHI, PHIa, PHIb, h, N, and MAX. My question is, if I wanted to use this function for a different set of variables can I just call the function as such:
gauss(x,y,a,b,e,M,I)
provided that they're all of the same type defined in my function or do the variables have to have the same name? My confusion is that I'm not sure if b, PHI, PHIa, PHIb, h, N, and MAX in my function are just placeholders or they're actually referencing those exact values.
My goal is to have one general function that can handle everything.
I hope what I just asked made sense. Thanks for all the help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//1-D Gauss-Seidel Function
void gauss(double * b, double * PHI, double PHIa, double PHIb, double h, int N, int MAX)
{
//Set Boundary Conditions
PHI[0] = PHIa; PHI[N-1] = PHIb;
//Perform Gauss-Seidel Method
int step = 0;
while (step < MAX)
{
for (int i = 1; i < N-1; i++)
{
PHI[i] = 0.5*(PHI[i-1] + PHI[i+1]) - 0.5*h*h*b[i];
}
step++;
}
}
The entire point of functions is to repeat the same steps on different variables. So for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int sqr(int inInt)
{
return inInt * inInt;
}
int main()
{
int x, y;
cout << "Enter a value for x: ";
cin >> x;
cout << "Enter a value for y: ";
cin >> y;
cout << "The square of x is " << sqr(x) << endl;
cout << "The square of y is " << sqr(y) << endl;
}
As you can see, I really don't care from the point of view of int main() what the name of any variable named in int sqr is. As long as I'm giving it an int, it knows what to do with it.
Perfect. That's what I was thinking, but I just wanted to make sure. Now my next question is if the following operation would be valid:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int sqr(int inInt)
{
return inInt * inInt;
}
int main()
{
int x, y;
cout << "Enter a value for x: ";
cin >> x;
cout << "Enter a value for y: ";
cin >> y;
cout << "The square of x is " << sqr(2*x) << endl;
cout << "The square of y is " << sqr(2*y) << endl;
}
Where I'm performing a mathematical operation on the input as I'm inputting it in. This is regardless of whether it's of type double, int, float, etc? I would imagine that it's valid as long as your operation maintains the type. As in,
If int a = 7, then sqr(a/13) would be considered invalid. But if int a = 4, then sqr(a/2) would be valid.
I'm not sure if you can or can't do that. You're passing the variable by value, so it would make sense that you can, but it would not be within the standards that I've ever seen in any kind of code. If you accept a value by reference, though, I'm absolutely positive that you can not do that.
EDIT: Also, squaring (7/13) is a valid mathematical operation, but you wouldn't get a float back, it would truncate your result, and give you a different than expected answer.
The reason I ask is because certain values can change throughout the iterations in the rest of my code, so I want to make it as general as possible without having to store a ton of variables. Something like this would work for my code as well, but I'm not sure if this would be valid or not.
1 2 3 4 5 6 7 8 9 10 11
void restrct(double * r, int N, int M, double * r2)
{
r2[0] = r[0];
r2[M] = r[N];
for (int i = 1; i < M-1; i++)
{
r2[i] = r[2*i];
}
}
restrct(r,N,M,10,r2);
Where instead of entering h, I just entered the numerical value. When I run the compiler, I don't get any errors, but I'm not sure if it there is another fundamental issue in there that I'm not seeing.
If you're passing pointers the way I think you are, and for the same reasons, I think you would be better off passing by reference double& r for example. Unless it's an array...