# Thread: How simple can a Function make life?

1. ## How simple can a Function make life?

Hi everyone, this is my 2nd post. My first post I was working on a banking program, well unfortunately I am still working on it. This is my final output on the bottom. It works fine I like it, but I want to simplify it with the use of functions. Primarily I just want to convert these 3 blocks of code below into functions and arrange it so the program will run smoothly. Any input would be greatly appreciated.

1.
Code:
```do
{
printf ("Now enter your current balance in dollars and cents: ");
scanf	("%f", &starting_balance);
fflush(stdin);

if (starting_balance < 0)
printf("Error: Starting Balance must be at least zero!\n\n");

} while (starting_balance < 0);```
2.
Code:
```do
{
printf ("\nEnter the number of withdrawals: ");
scanf	("%i", &number_of_withdrawals);
fflush(stdin);

if (number_of_withdrawals > 50)
printf ("*** Too many withdrawals. ***\n");
if (number_of_withdrawals < 0)
printf ("*** Negative number of withdrawals not allowed. ***\n");

} while (number_of_withdrawals > 50 || number_of_withdrawals < 0);```
3.
Code:
```do
{
printf ("\n\nEnter the number of deposits: ");
scanf	("%i", &number_of_deposits);
fflush(stdin);

if (number_of_deposits > 50)
printf ("*** Too many deposits. ***\n");
if (number_of_deposits < 0)
printf ("*** Negative number of deposits not allowed. ***\n");

} while ( number_of_deposits > 50 || number_of_deposits < 0);```

Code:
```#include <stdio.h>

void	main (void)
{

/* Variable Declarations */
/* --------------------- */

int     number_of_deposits, number_of_withdrawals, i;
float	amount_to_deposit[50], amount_to_withdraw[50];
float   starting_balance, current_balance;
char    name[30];

/* Display an introduction header */
/* ------------------------------ */

printf ("Welcome to the Sears Bank Balancing System\n\n");

gets(name);                                  /* or use scanf("%s", name);  */
fflush(stdin);

printf("\nHello %s\n\n", name);

/* Query the user for the starting balance (starting_balance >= 0) */
/* ------------------------------------------------------------ */

do
{
printf ("Now enter your current balance in dollars and cents: ");
scanf	("%f", &starting_balance);
fflush(stdin);

if (starting_balance < 0)
printf("Error: Starting Balance must be at least zero!\n\n");

} while (starting_balance < 0);

/* Prompt for the number of withdrawals                     */
/* Ensure that user did not enter more than 50 withdrawals. */
/* -------------------------------------------------------- */

do
{
printf ("\nEnter the number of withdrawals: ");
scanf	("%i", &number_of_withdrawals);
fflush(stdin);

if (number_of_withdrawals > 50)
printf ("*** Too many withdrawals. ***\n");
if (number_of_withdrawals < 0)
printf ("*** Negative number of withdrawals not allowed. ***\n");

} while (number_of_withdrawals > 50 || number_of_withdrawals < 0);

/* Prompt for the number of deposits.                    */
/* Ensure that user did not enter more than 50 deposits. */
/* ----------------------------------------------------- */

do
{
printf ("\n\nEnter the number of deposits: ");
scanf	("%i", &number_of_deposits);
fflush(stdin);

if (number_of_deposits > 50)
printf ("*** Too many deposits. ***\n");
if (number_of_deposits < 0)
printf ("*** Negative number of deposits not allowed. ***\n");

} while ( number_of_deposits > 50 || number_of_deposits < 0);

/* Retain starting balance for bank record. */
/* ---------------------------------------- */

current_balance = starting_balance;

/* Prompt for all deposit amounts, keeping track of current balance. */
/* ----------------------------------------------------------------  */

printf ("\n");     /* spacing   */
for (i = 0; i < number_of_deposits; i++ )
{
do
{
printf ("Enter the amount of deposit #%i: ", i+1 );
scanf	("%f", &amount_to_deposit[i]);
fflush(stdin);

if (amount_to_deposit[i] <= 0)
printf ("*** Deposit amount must be positive! Please re-enter ***\n");

} while (amount_to_deposit[i] <= 0);

current_balance = current_balance + amount_to_deposit[i];

} /* end for loop */

/* Prompt for all withdrawal amounts, keeping track of current balance. */
/* -------------------------------------------------------------------  */

printf ("\n");   /* spacing   */
for (i = 0; i < number_of_withdrawals; i++ )
{
/* Enter amounts, and be sure amount does not exceed current balance. */
/* ------------------------------------------------------------------ */

do
{
printf ("Enter the amount of withdrawal #%i: ", i+1 );
scanf	("%f", &amount_to_withdraw[i]);
fflush(stdin);

if (amount_to_withdraw[i] > current_balance)
printf ("*** Withdrawal amount exceeds current balance. ***\n");
else
if (amount_to_withdraw[i] <= 0)
printf ("*** Withdrawal amount must be  greater than zero! ***\n");

} while (amount_to_withdraw[i] > current_balance || amount_to_withdraw[i] <= 0);

current_balance = current_balance - amount_to_withdraw[i];

/* If Balance goes to zero, break the loop, no more withdrawals allowed, reset the
withdrawal count so the bank record prints properly! */

if(current_balance == 0)
{
printf ("\n*** Balance is now zero.  No more withdrawals allowed! ***\n");
number_of_withdrawals = i + 1;
break;
}  /* end-if */

} /* end for loop.*/
printf ("\n");       /* spacing   */

/* Output closing balance and appropriate closing message. */
/* ------------------------------------------------------- */

printf ("*** The closing balance %s is \$%.2f ***\n\n", name, current_balance);

if (current_balance >= 50000.00 )
printf ("*** %s time to invest some money! ***", name);
else
if (current_balance >= 15000.00 )
printf ("*** %s maybe you should consider a CD. ***", name);
else
if (current_balance >= 1000.00 )
printf ("*** %s keep up the good work. ***", name);
else
printf ("*** %s your balance is very low. ***", name);

/* Output bank record: Starting balance, deposits and withdrawals. */
/* -------------------------------------------------------------   */

printf ("\n\n");  /* spacing   */
printf ("   *** Bank Record ***\n");
printf ("Starting Balance: \$%13.2f\n\n", starting_balance);

for (i = 0; i < number_of_deposits; i++ )
printf ("Deposit #%2i:     %15.2f\n", i+1, amount_to_deposit[i]);

if (number_of_deposits > 0)
printf ("\n");           /*  spacing   */

for (i = 0; i < number_of_withdrawals; ++i )
printf ("Withdrawal #%2i:  %15.2f\n", i+1, amount_to_withdraw[i]);

printf ("\nEnding Balance: \$%15.2f\n\n", current_balance);

getchar();

} /* end main. */```

2. Take a moment to think about what each function should be doing. What information do they need to know about, and what information will they hand back? Once you sort that out, turning them into functions should be pretty clear to you.

Quzah.

3. Well you've got the unholy trinity in your code, perhaps you could write the same thing without those 3 deadly sins?
SourceForge.net: Void main - cpwiki
SourceForge.net: Gets - cpwiki
SourceForge.net: Fflush - cpwiki

4. Originally Posted by Salem
Well you've got the unholy trinity in your code, perhaps you could write the same thing without those 3 deadly sins?
SourceForge.net: Void main - cpwiki
SourceForge.net: Gets - cpwiki
SourceForge.net: Fflush - cpwiki
Blasphemy! No mention of cprogrammings FAQs?

5. lol....so I'm off to a bad start. Its sad Im doing this as a project for school. I passed this in last week got an B+, but the prof never mentioned anything in those pages as a matter of fact we lose points if we do not include fflush

6. Fflush works only on outward streams. On input streams, it's undefined. Think of it like the plumbing in your home - you can flush the toilet, but trying to flush the faucet, is quite goofy.

Sounds like yes, your instructor is WAY behind the times for C.

Grouping your code in functions is easiest generally, when you think about the functional aspect of the code: is it for input, output, a certain type of calculation, or what?

Congrats on completing your class. Now learn how C really should work! < smile >

7. > but the prof never mentioned anything in those pages as a matter of fact we lose points if we do not include fflush
If your "prof" is teaching you about fflush(stdin) and such like, then they really don't know C at all.
Feel free to "play along" with them just to pass the course, but make sure you seek out plenty of other sources of information.

8. What makes a good candidate for a function...

1) It does 1 useful thing (not 30)
2) It encapsulates data
3) It is called from more than one location in your code
4) It makes your code easier to maintain.

Any 3 out of 4... make it a function.

9. The whole point of functions is to break up a program into smaller bits that are easier to manage. Small functions that do one thing CAN BE easier to implement, easier to get right, easier to verify, and easier to reuse.

There is an art to breaking large systems into smaller pieces though.

It is possible to make functions so small that they are difficult to use (sort of like trying to build a house from grains of sand). Although obviously functions that are huge are hard to use, get right, etc. The trick is the Goldilocks approach to everything: not too big, not too small, just right. Working out what is "just right", however, takes thought.

Naming functions is an important as what goes in them. For example, code that repeatedly calls a function named DrawLineOnScreen() is much easier to understand than it would be if it called a function named ScreenOperation27().

10. Originally Posted by quzah
Take a moment to think about what each function should be doing. What information do they need to know about, and what information will they hand back? Once you sort that out, turning them into functions should be pretty clear to you.

Quzah.

I don't want to do that. What I want is for you to use your extensive knowledge of C to do my homework for me, that way one day I'll get my degree and know absolutely nothing and still get a six figure salary............... j/k

11. Originally Posted by Alint808
I don't want to do that. What I want is for you to use your extensive knowledge of C to do my homework for me, that way one day I'll get my degree and know absolutely nothing and still get a six figure salary............... j/k
LOL... nothing like overstating the obvious

Actually, you seem to be doing pretty well so far.... My only suggestion, based soley upon what little I've seen of your stuff, is that you should perhaps make a second pass over your code with an eye to simplifying it. In my experience, it's the simple stuff that works the best.