# Just learning Fuctions, what should i think of when making em?

• 04-11-2004
MrX8503
Just learning Fuctions, what should i think of when making em?
I understand how functions work, but if i'm unable to utilize it in a simple program, i guess i dont entirely understand it.

Anywho, i have to make a program where the users enter in an upper limit and a lower limit and the resrictions are....

y=-x when x<0
y=x^2 when 0<=x<=2
x>2 when y=4

Also the user gets to enter in an amount to increment.

heres an example, the user enters in -3 for lower and 3 for upper, it should display this...

xvalue yvalue
-3 3
-2.50 2.50
...... ......
..... ......
0 0
.50 .50
..... .......
..... ......
2 4
2.5 4
3 4

i got this far in my code...

Code:

```float slope(int x, int y); using namespace std; void main() {        float upnum, lownum, inc, a;         cout << "Slope Function Program" << endl;         cout << "Enter Lower Limit: " << endl;         cin >> lownum;         cout << endl;         cout << "Enter Upper Limit: " << endl;         cin >> upnum;         cout << endl;         cout << "Enter Increment: " << endl;         cin >> inc;         cout << endl;         cout << left << setw(8) << "X Value" << " " << left << setw(8) << "Y Value" << endl;         cout << "--------" << " " << "--------" << endl;         a = slope(lownum,upnum);         cout << a << endl; } float slope(float &x, float &y) {                if(x<0)         {                 y = x * (-1);         }         else if(x>=0 || x<=2)         {                 y = sqrt(x);         }         else if(x>2)         {                 y = 4;         }         return(x,y); }```
So can anyone tell me how many fucntions should i create and what should i be thinking about when making this program? Because i have a problem of how the program will work in my mind. Its not organized in my mind but all jumbled, which makes it hard to get it down to paper.
• 04-11-2004
JaWiB
First of all, make sure the prototype matches the function:
Code:

`float slope(float &x,  float &y);`
It looks like you have the right idea in your function, but there are a few things:

Code:

```float slope(float &x, float &y) {                if(x<0)         {                 y = x * (-1);         }         else if(x>=0 && x<=2) //both conditions must be true         {                 y = x*x; //unless you really want the square root of x (you said you wanted x^2 earlier)         }         else if(x>2)         {                 y = 4;         }         return(x,y); }```
Your return statement probably wont do what you want it to. If you want to just output the new values of x and y after you call the function, then just output the two numbers you pass to the function (since they are passed as a reference, the function can change their values).
• 04-11-2004
MrX8503
I thought i actually write the program out normally and make it work right first before changing it to a function so this is what i got...

Code:

```//Lownum is also X Value void main() {        float upnum, lownum, inc, y;         cout << "Slope Function Program" << endl << endl;         cout << "Enter Lower Limit: ";         cin >> lownum;         cout << "Enter Upper Limit: ";         cin >> upnum;         cout << "Enter Increment: ";         cin >> inc;         cout << endl;         cout << left << setw(8) << "X Value" << "  " << left << setw(8) << "Y Value" << endl;         cout << "--------" << "  " << "--------" << endl;         while(lownum<=upnum)         {                 if(lownum<0)                 {                         y = lownum * (-1);                 }                 else if(lownum>2)                 {                         y = 4;                 }                 else if(lownum>=0 && lownum<=2)                 {                         y = lownum * lownum;                 }                 cout << fixed << setprecision(2);                 cout << right << setw(8) << lownum << "  " << right << setw(8) << y << endl;                 lownum = lownum + inc;         } }```
So how would i change this to a function? This is the point where i see a function as completely useless, so i dont know how it would be utilized here.
• 04-11-2004
exluddite
I'm also new at this, so we are probably about on the same page. The first thing that I notice is that you have void main ()... for some reason there are those that consider that a hangin' offence. I don't know why, I've just gone with it so far :) . As far as the other functions, I just try to keep it simple as to what they are to do. If I want to display a message and ask for input, I'll do something like this:
Code:

``` float getinfo (float deg) {     cout<<"Please enter the temperature"         <<endl;     cin>>deg; }```
That way "getinfo" does two things, and I don't get (terribly) confused.
After that I follow suit with functions for the various tasks/calculations etc. Basically, the less that I have going on in one function, the easier it is to find out what is going wrong.
As far as how to determine what to put in which function and the whole flow of logic thing...well, that's something that I intend to post a question about once I have it worded correctly. Of course, I may have answered my own question at that point!
I'll let the more experienced explain the particulars.
• 04-11-2004
MrX8503
Code:

```float slope(float x); using namespace std; //Lownum is also X Value void main() {        float upnum, lownum, inc, y, a;         cout << "Slope Function Program" << endl << endl;         cout << "Enter Lower Limit: ";         cin >> lownum;         cout << "Enter Upper Limit: ";         cin >> upnum;         cout << "Enter Increment: ";         cin >> inc;         cout << endl;         cout << left << setw(8) << "X Value" << "  " << left << setw(8) << "Y Value" << endl;         cout << "--------" << "  " << "--------" << endl;         a = slope(lownum);         } float slope(float x) {        float upnum, x, inc, y, lownum;         while(x<=upnum)         {                 if(x<0)                 {                         y = x * (-1);                 }                 else if(x>2)                 {                         y = 4;                 }                 else if(x>=0 && x<=2)                 {                         y = x * x;                 }                 cout << fixed << setprecision(2);                 cout << right << setw(8) << lownum << "  " << right << setw(8) << y << endl;                 lownum = lownum + inc;         } }```

theres my updated code. Ok this is really ........in me off. ITs taking me like 3 hours. I can write the program fine, i just dont understand the use of a function,why its there, its naming scheme, and what name goes with what name. Like does the name inside of slope() have any relation at all? I'm just all around confused.
• 04-11-2004
MrX8503
Well i guess i want be majoring in Computer Science anymore.
• 04-11-2004
exluddite
Quote:

Originally Posted by MrX8503

Like does the name inside of slope() have any relation at all? I'm just all around confused.

Well yeah, it does insofar as the type of variable it is ( int, float, char, etc. ) but the naming of the variables themselves is entirely up to you. You can call it "float scoobiedoobie" if you want.
One thing that I have found is that the position of the variable can change things:
Code:

```     #include <iostream>     #include <iomanip>     using namespace std;   float whosthere (int, float); int main () {             int fred;             float bob;           whosthere (fred, bob); } float whosthere (int fred, float bob) {       ...       ...  // whatever you want to happen in this function }```
If I put fred before bob (or vice versa) somewhere, that seems to screw me up bigtime, especially when things get more complicated.
Once again, I'll defer to those more experienced, just putting in my two cents.

Speaking of which, has anyone else notice the lack of a "cents" symbol on a keyboard or in ASCII ?
• 04-11-2004
Learning any new language or learning any new task can be confusing and frustrating at first. There are very few prodigies out there, so don't give up too easily!

In my view, exluddite did a good job of explaining the basic tenants of functions. Try to keep them small. Try to have them do a specific task.

I think of variables as the information used in a program and functions as the way manipulate that information.

Every program in C++ must have at least one function---main(). But many programs will make more sense if you break out the various tasks into separate functions. That way, when you write a function to calculate the least common denominator in one program, you can reuse it in another, etc.

Each function must have four things.

First is a name for the function, else how will you be able to know which one you are using?

Second is a list of material to manipulate. That material may come from somewhere outside the function---either from global variables or variables passed to the function as arguments (aka parameters) or from variables declared locally to the function. The information passed to the function is located in the parethesis immediately following the name of the function.

Third, the function must do something to manipulate the data it is given. It may print the information to the screen, send the information to a file, look for the number of the letter i in a word like Mississippi, or calculate the 13th number in the Fibonacci series. The instructions as to how to manipulate what is given in the body of the function---between the opening and closing curly braces.

Fourth, the results of the manipulation should be used somehow--or else why bother. Maybe the use is done within the body of the function itself--like displaying information to the screen, or maybe the results are used back to the calling function by way of a return value by passing information by reference rather than by value, etc.

To make it easier for yourself there are a few tips I find useful. If I pass information by value, I find it easier to rename the variable so I know that whatever I do to the information in the current function, it won't make a difference back in the calling function. Likewise, I use the same name for information in the current function as in the calling function if I want the results of the manipulation in the current function to be recognized back in the calling function.

Keeping the order of information passed to the function is another important detail----after all I'd rather have 10 10 dollar bills and 1 penny than 10 pennies and 1 10 dollar bill if I transposed the information.

After a while this becomes second nature, like realizing you're really learning a foreign language when you have your first dream in that language!
• 04-11-2004
exluddite
Well elad, I don't know about MrX, but that makes things a bit clearer to me. I have found that explaining things that I am trying to understad (or listening to anothers explaination) makes things more easily understood.
Actually I think your post should go to the FAQ section (although I don't know what to put it under) if for no other reason that it goes a long way towards answering my question about understanding the flow of logic.
type in code....
compile...
see if it works...
fix code...
compile...
I'll get it eventually. :rolleyes: