Hi All,

I need help figuring out how I can call the various functions in this program from main. I've figured out as much of the code I could, but now I'm stumped. Any help would be appreciated.

#include <stdio.h>

    float deposits[50] = {0}; 
    float withdrawals[50] = {0};
    char  first_name[20] = {0};
    int   num_withdrawals, num_deposits, x;
    float start_bal;
    float current_balance = 0;
    float total_deposits = 0;
    float total_withdrawals = 0;
    float balance;
    float get_startingbalance(void);
    float getCntOfWithdrawls();
    float getCntOfDeposits();
    float getEachDeposit();
    float getEachWithdrawl();
    float checkBalance();
    float calcAndDisplayBalance();
    float displayBankRecord();
    
int main (void)

{

    /* Output initial greeting */

    printf("Welcome to the Banking System.\n\n");
    
    printf("Please enter your first name: ");
    scanf("%s", &first_name);
    
    printf("\nHello, %s.\n\n", first_name);
    
}    
    
    /* GET STARTING BALANCE */

    float get_start_balance() {
    
        float start_bal;
    
    do {
        
        printf("%s, Please enter your current balance in dollars and cents: "); /* Prompt user for current balance. */
        scanf("%f", &start_bal);
        
        if (start_bal < 0)
            printf("Invalid entry. Starting balance must be at least zero!\n\n");

    } while (start_bal < 0); 
    
    return start_bal;
    
}   /* end function get starting balance */
    
    /* GET NUMBER OF WITHDRAWALS */

    float getCntOfWithdrawls() {
    
        int num_withdrawls;

    do {
        
        printf ("\nEnter the number of withdrawals: ");
        scanf ("%i", &num_withdrawals);
        printf ("\n");
        
        if (num_withdrawals < 0 || num_withdrawals > 50)
            printf ("Error: Number of withdrawals must be between zero and 50, please re-enter!\n\n");
    
    } while (num_withdrawals < 0 || num_withdrawals > 50); 
    
    return num_withdrawals;
    
}   /* end function number of withdrawls */

    /* GET NUMBER OF DEPOSITS */

    float getCntOfDeposits() {
    
        float num_deposits;
    
    do {
        
        printf ("Enter the number of deposits: ");
        scanf ("%i",&num_deposits);
        printf ("\n");
                
        if ( num_deposits < 0 || num_deposits > 50)
            printf ("Error: Number of deposits must be between 0 and 50, please re-enter!\n\n");
    
    } while (num_deposits < 0 || num_deposits > 50); /* end do-while loop */
    
    return num_deposits;

}   /* end function number of deposits */

    /* GET EACH DEPOSIT */

    float getEachDeposit() {
    
        float num_deposits;
    
    for (x = 0; x < num_deposits; x++)
    {
        
    do {
        
        printf ("Enter the amount of deposit #%i: ", x + 1);
        scanf ("%f",&deposits[x]);
                
        if (deposits[x] <= 0)
            printf ("*** Deposit amount must be greater than zero. Please re-enter! ***\n");
    
    } while (deposits[x] <= 0);
    
    total_deposits = total_deposits + deposits[x];
    
    } /* end for loop */
    
}    /*end function get each deposit*/
    
//    current_balance = total_deposits + start_bal;     
    
    /* Get Each Withdrawal */
    
    float getEachWithdrawl() {
    
        float num_withdrawls;

    for (x = 0; x < num_withdrawals; x++)
    {
        
    do {
        
        printf ("\n");
        printf ("Enter the amount of withdrawal #%i: ", x + 1);
        scanf ("%f",&withdrawals[x]);
                
        if (withdrawals[x] > current_balance)
            printf ("***Withdrawal amount exceeds current balance.***\n");
        
        else
            if (withdrawals[x] <= 0)
            printf ("*** Withdrawal amout must be greater than zero. Please re-enter! ***");
        
    } while (withdrawals[x] > current_balance || withdrawals[x] <= 0); /* end do-while loop */
     
     total_withdrawals = total_withdrawals + withdrawals[x];
     
    } /* end for loop */
     
}   /* end function get each withdrawl */

    /* Check Balance */
    
    float checkBalance() {
        
        float current_balance;

    balance = current_balance - total_withdrawals + total_deposits;
     
    if (balance == 0)
        
        {
         
         printf ("\n *** Balance is now zero. No more withdrawals can be made at this time. ***\n");
         
         num_withdrawals = x;
         
        }
        
}   /* end function check balance */
    
    /* Calculate and Display Balance */
    
    float calcAndDisplayBalance() {
        
    {
    
    printf ("\n*** Your closing balance is $%.2f, %s*** \n", balance, first_name);
        if (balance >= 5000.00)
        
    printf ("*** Time to invest some money!*** \n\n");
        else if (balance >= 15000.00 && balance <= 49999.99)
        
    printf ("*** Maybe you should consider a CD.*** \n\n");
        else if (balance >= 1000.00 && balance <= 14999.99)
            
    printf ("*** Keep up the good work.*** \n\n");
    
        else
    printf ("*** %s, your balance is getting low!*** \n\n", first_name);
    
    }
    
}   /*end function calc and display balance*/
    
    /* Calculate and display balance*/
    
    float displayBankRecord() {
        
    printf ("     *** Bank Record ***\n");
    
    printf ("\nStarting Balance:   $%13.2f\n\n", current_balance);
    
    for (x = 0; x < num_deposits; x++)
        {
        
        printf ("Deposit #%i:          %13.2f\n", x + 1, deposits[x]);
        
        }
    
    for (x = 0; x < num_withdrawals; x++)
        
        {
        
        printf ("\nWithdrawal #%i        %13.2f", x + 1, withdrawals[x]);
        
        }
    
        printf ("\n\nEnding Balance is:  $%13.2f\n", balance);
        
}   /* end function display bank record */

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.

Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities - called functions in C - to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind

The name of the function is unique in a C Program and is Global. It neams that a function can be accessed from any location with in a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.
We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.
---------------------------------------------------------------------------
anna jennifer
Promoter

Just a simple example of calling a function:

get_start_balance();

To call a function, you just need to use the function name, and pass whatever parameters needed (in the above case the function doesn't take in any parameters).

Actually, you are already calling functions. "printf" and "scanf" are functions too.

This article has been dead for over six months. Start a new discussion instead.