Function in C++ || Project OPTIMIZATION
Function in C++
Functions are used to perform certain actions, and they are important for reusing code: Define the code once and use it many times. It also enables programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task.
For our projects, we will start off by performing the task below. Try to do this on your own before seeing the final answer.
- Create a deposit function
 - Declare a variable inside the function with a value of 1000.
 - Accept user integer input.
 - Add to the existing variable of 1000.
 - Display the final result.
 
For the other function,
- Create a withdraw function
 - Declare a variable inside the function with a value of 1000.
 - Accept user integer input.
 - Subtract from the existing variable of 1000.
 - Display the final result.
 
#include<bits/stdc++.h>
using namespace std;
int Deposit()
{
    int existing_amount = 1000,amt;
    cout<<endl<<"Enter how much to Deposit - ";
    cin>>amt;
    amt+=existing_amount;
    return amt;
}
int Withdraw()
{
    int existing_amount = 1000,req;
    cout<<endl<<"Enter how much to Withdraw - ";
    cin>>req;
    existing_amount-=req;
    return existing_amount;
}
int main()
{
    string username;
    cout<<"Enter your username - ";
    cin>>username;
    cout<<endl;
    cout<<"Welcome to ABC Online Banking "<<username<<endl<<endl;
    cout<<"What would you like to do?"<<endl;
    cout<<"1.Deposit"<<endl;
    cout<<"2.Withdraw"<<endl;
    cout<<"3.Loans"<<endl;
    cout<<"4.Credit Score"<<endl;
    int dothis;
    cout<<"Enter your option - ";
    cin>>dothis;
    //dothis=1 assigns, dothis==1 checks
    if(dothis==1)
    {
        cout<<endl<<"You selected Deposit"<<endl;
        int final_amt=Deposit();
        cout<<endl<<"Your final amount is - "<<final_amt<<endl;
    }
    else if(dothis==2)
    {
        cout<<endl<<"You selected Withdraw"<<endl;
        int final_amt=Withdraw();
        cout<<endl<<"Your final amount is - "<<final_amt<<endl;
    }
    else if(dothis==3)
    {
        cout<<endl<<"You selected Loans"<<endl;
    }
    else
    {
        cout<<endl<<"You selected Credit Score"<<endl;
    }
    //there will be changes eventually
}
The output would look something like this - 
Comments
Post a Comment