# Need help with functions

• 05-20-2010
jusfry01
Need help with functions
I am learning how to use functions to program, but am having trouble with correctly executing them. The C programming book I have only speaks about functions for about 20 pages, and it is not enough for me to grasp the concept. I have written the code for the equations which I must execute, but I need to figure out how to perform this code with functions. I only have a few rules to follow, which are written below.

The main routine must be a 'manager' routine, which means
* The function can declare variables
* The function can call other functions
* The function can save return values from functions and pass those values to other functions
* The function must not perform any task other than the management of other functions
* The function may not call any function other than user-defined functions

Here is my code thus far:

Code:

```// J.F. #include <stdio.h> #include <stdlib.h> #include <math.h> int main() {  //First String//     int varA, varB, varC, varD, varE, varF;     char charA, charB, charC, charD, charE;     int exprA, exprB, exprC, exprD, exprE;     //Second String//     float varFA, varFB, varFC, varFD;     char charF, charG, charH;     float exprF, exprG, exprH;        //Third String//     float  varFE, varFF;     char charI, charJ, charK;     int varG, varH;         float exprI, exprJ, exprK;     printf("Enter '(INT * INT) + (INT / INT) + (INT %% INT): ");     scanf("(%d %c %d) %c (%d %c %d) %c (%d %c %d)%*c", &varA, &charA, &varB,               &charB, &varC, &charC, &varD, &charD, &varE, &charE, &varF);     printf("(%d %c %d) %c (%d %c %d) %c (%d %c %d)= ",varA,charA,varB,charB,                                     varC,charC,varD,charD,varE,charE,varF);     switch(charA) { // First String, First Expression //                   case '+':                   exprA = varA + varB;                   break;                   case '-':                   exprA = varA - varB;                   break;                   case '*':                   exprA = varA * varB;                   break;                   case '/':                   exprA = varA / varB;                   break;                   case '%':                   exprA = varA % varB;                   break;                   default:                   exprA = 0;                   printf("?'%c' is not valid. \"(%d %c %d)\" set to 0\n"                       ,charA, varA, charA, varB);                   break;                   }     switch(charC) { // First String, Second Expression //                   case '+':                   exprB = varC + varD;                   break;                   case '-':                   exprB = varC - varD;                   break;                   case '*':                   exprB = varC * varD;                   break;                   case '/':                   exprB = varC / varD;                   break;                   case '%':                   exprB = varC % varD;                   break;                   default:                   exprB = 0;                   printf("?'%c' is not valid. \"(%d %c %d)\" set to 0\n"                       ,charC, varC, charC, varD);                   break;                   }         switch(charB) { // First String, Processing First Two Expressions //                   case '+':                   exprC = exprA + exprB;                   break;                   case '-':                   exprC = exprA - exprB;                   break;                   case '*':                   exprC = exprA * exprB;                   break;                   case '/':                   exprC = exprA / exprB;                   break;                   case '%':                   exprC = exprA % exprB;                   break;                   default:                   exprC = 0;                   printf("?'%c' is not valid. \"(%d %c %d)\" set to 0\n"                       ,charB, exprA, charB, exprB);                   break;                   }     switch(charE) { // First String, Third Expression //                   case '+':                   exprD = varE + varF;                   break;                   case '-':                   exprD = varE - varF;                   break;                   case '*':                   exprD = varE * varF;                   break;                   case '/':                   exprD = varE / varF;                   break;                   case '%':                   exprD = varE % varF;                   break;                   default:                   exprD = 0;                   printf("?'%c' is not valid. \"(%d %c %d)\" set to 0\n"                       ,charE, varE, charE, varF);                   break;                   }         switch(charD) { // First String, Final Expression Processing //                   case '+':                   exprE = exprC + exprD;                   break;                   case '-':                   exprE = exprC - exprD;                   break;                   case '*':                   exprE = exprC * exprD;                   break;                   case '/':                   exprE = exprC / exprD;                   break;                   case '%':                   exprE = exprC % exprD;                   break;                   default:                   exprE = 0;                   printf("?'%c' is not valid. \"(%d %c %d)\" set to 0\n"                       ,charD, exprC, charD, exprB);                   break;                   }     printf("%d\n", exprE);     printf("Enter (FLOAT / FLOAT) * (FLOAT / FLOAT): ");     scanf("(%f %c %f) %c (%f %c %f)%*c",&varFA, &charF, &varFB, &charG,                                                 &varFC, &charH, &varFD);         printf("(%f %c %f) %c (%f %c %f) = ",varFA, charF, varFB, charG,                                                 varFC, charH, varFD);         switch(charF) { // Second String, First Expression //           case '+':               exprF = varFA + varFB;               break;           case '-':               exprF = varFA - varFB;               break;           case '*':               exprF = varFA * varFB;               break;           case '/':               exprF = varFA / varFB;               break;           case '%':               exprF = ((int)varFA % (int)varFB);               break;           default:               exprF = 0;               printf("?'%c' is not valid. \"(%f %c %f)\" set to 0\n"                       , charF, varFA, charF, varFB);               break;     }         switch(charH) { // Second String, Second Expression //           case '+':               exprH = varFC + varFD;               break;           case '-':               exprH = varFC - varFD;               break;           case '*':               exprH = varFC * varFD;               break;           case '/':               exprH = varFC / varFD;               break;           case '%':               exprH = ((int)varFC % (int)varFD);               break;           default:               exprH = 0;               printf("?'%c' is not valid. \"(%f %c %f)\" set to 0\n"                       ,charH, varFC, charH, varFD);               break;     }                   switch(charG) { // Second String, Final Expression Processing //           case '+':               exprG = exprF + exprH;               break;           case '-':               exprG = exprF - exprH;               break;           case '*':               exprG = exprF * exprH;               break;           case '/':               exprG = exprF / exprH;               break;           case '%':               exprG = ((int)exprF % (int)exprH);               break;           default:               exprG = 0;               printf("?'%c' is not valid. \"(%f %c %f)\" set to 0\n"                       , charG, exprF, charG, exprH);               break;     }        printf("%.3f\n",exprG);           printf("Enter (INT - INT) %% (FLOAT * FLOAT): ");     scanf("(%d %c %d) %c (%f %c %f)%*c",&varG, &charI, &varH, &charJ,                                               &varFE, &charK, &varFF);         printf("(%d %c %d) %c (%f %c %f) = ",varG, charI, varH, charJ,                                               varFE, charK, varFF);         switch(charI) { // Third String, First Expression //           case '+':               exprI = varG + varH;               break;           case '-':               exprI = varG - varH;               break;           case '*':               exprI = varG * varH;               break;           case '/':               exprI = varG / varH;               break;           case '%':               exprI = varG % varH;               break;           default:               exprI = 0;               printf("?'%c' is not valid. \"(%d %c %d)\" set to 0\n"                       , charI, varG, charI, varH);               break;     }         switch(charK) { // Third String, Second Expression //           case '+':               exprJ = ((int)varFE + (int)varFF);               break;           case '-':               exprJ = ((int)varFE - (int)varFF);               break;           case '*':               exprJ = ((int)varFE * (int)varFF);               break;           case '/':               exprJ = ((int)varFE / (int)varFF);               break;           case '%':               exprJ = ((int)varFE % (int)varFF);               break;           default:               exprJ = 0;               printf("?'%c' is not valid. \"(%f %c %f)\" set to 0\n"                       ,charK, varFC, charK, varFD);               break;     }                   switch(charJ) { // Third String, Final Expression Processing //           case '+':               exprK = exprI + exprJ;               break;           case '-':               exprK = exprI - exprJ;               break;           case '*':               exprK = exprI * exprJ;               break;           case '/':               exprK = exprI / exprJ;               break;           case '%':               exprK = ((int)exprI % (int)exprJ);               break;           default:               exprK = 0;               printf("?'%c' is not valid. \"(%f %c %f)\" set to 0\n"                       , charJ, exprI, charJ, exprJ);               break;     }        printf("%f\n",exprK);             scanf("%*c");     return 0; }```
• 05-21-2010
The idea of using functions is simple - and it's a mystery to me why you didn't at least TRY to use them - since clearly they are REQUIRED by your assignment.

Say you're recovering from the flu in the living room, and you've missed a few critical days at school. Your big brother, and a good friend, are all willing to help get your school projects finished: building a bird on the wing plaque, for wood shop, rewriting a chemistry paper. from your rough draft.

You can't do these things yet, Dad says no, you rest here - but they need to be done, so:

1) You have your big brother take your layout drawings, out to the garage, and start on the plaque of the duck flying.

That's a function:
plaque duck on the wing makePlaque(person big brother, plans drawing, wood, walnut),

The return is the duck on the wing plaque, the parameters are a (person - your big brother, plans - your drawing, wood - walnut)

2) You friend is in your chemistry class, and he'll re-write your chemistry paper, but that will be done at the kitchen table.

That's another function.

paper chemistry is the return type. redoPaper(person friend, paper your original, laptop Yourlaptop)

Your friend, your original paper, and your laptop, are the parameters that will be used

These functions form the basis of modular programming, and make it significantly easier to work on finding errors, having fewer errors to start with, and understanding the logic.

Try again, and this time, don't find the answer to the question - that's not the real point. Find out how to work with a few small functions, from main().
• 05-22-2010
jusfry01
Thank you,
I actually got it rather easily after looking at it for a few minutes. Since I am new to C programming, I tend to look at a new concept and over-think it a lot.
Thanks again