vl - simple functions

Introduction to Simple Functions

  • Focus: Writing simple functions for basic programming tasks rather than advanced concepts.

  • Advanced functions (e.g., return functions, anonymous functions) will be utilized in client-server programming in the future.

Detecting Odd or Even Numbers

  • Function Name: isEven

    • Purpose: To determine if a given number is even.

    • Parameters:

    • number (n)

    • Returns: A boolean value (true or false).

    • Logic: The function checks if the remainder of the variable n when divided by 2 is equal to zero using modulo.

    • Expression Used:

    • nextmod2=0n ext{ mod } 2 = 0

    • Example:

    • When n = 16, output is true (even).

    • When n = 17, output is false (odd).

Converting Kilometers to Miles

  • Function Name: toMiles

    • Purpose: To convert kilometers to miles.

    • Parameters:

    • kilometers

    • Returns: Calculated miles based on the formula.

    • Conversion Formula:

    • extmiles=extkilometersimes0.6214ext{miles} = ext{kilometers} imes 0.6214

    • Example Invocation: Setting kilometers to 10 and displaying results.

Insurance Calculation Functionality

  • Function Name: calcInsuranceNeeded

    • Purpose: To calculate the minimum insurance required for homeowners.

    • Parameters:

    • costOfStructure

    • Returns: The minimum insurance required which is 80% of the cost of the structure.

    • Calculation Formula:

    • 0.80imesextcostOfStructure0.80 imes ext{costOfStructure}

    • Example:

    • If cost of the building is 5,200,000, the function returns:

    • Minimum insurance=(0.80imes5200000)(0.80 imes 5200000).

Monthly Automobile Costs Calculation

  • Function Name: calcTotalMonthlyCost

    • Purpose: To calculate the total monthly and annual automotive expenses.

    • Import Parameters: An array of monthly expenses.

    • Monthly Expense Example Variables:

    • Auto payment: $685

    • School Loans: $585

    • Insurance: $385

    • Gas: $45

    • Oil Change: $28

    • Tires Replacement: $20/year (monthly equivalent is rac2012rac{20}{12})

    • Maintenance: $35/month

  • Total Monthly Cost Calculation Logic:

    • Initialize total variable to sum all expenses.

    • Annual Cost Calculation:

    • extannualCost=exttotalMonthlyCostimes12ext{annualCost} = ext{totalMonthlyCost} imes 12

    • Example Invocation: Calling function with expenses to calculate total monthly expense and then annual cost.

Error Handling and Code Adjustment

  • Troubleshoot issues where incorrect parameters are passed while calculating annual costs.

  • Importance of storing intermediate calculation results for clarity and accuracy.

Conclusion

  • Summarized the use of simple, effective function definitions for common programming tasks such as number checks, unit conversion, financial calculations, and understanding function parameters and return values.

  • Encouragement to further explore and refine function applications as programming skills develop.