Steps for Writing Functions • Identify the inputs, processes and outputs that your main function will handle. • Identify the inputs, processes and outputs that other functions will handle. • Develop pseudocode for each of your functions. • Code your program.
Guidelines for Writing Functions • Have your main function handle (a) getting initial inputs from the user, (b) processing of those inputs (if necessary) and (c) outputs to the user ONLY. Your main function should do little else. • Keep function length short – about 1 editor screen per function, give or take. • Use the main function as a mechanism for communicating among functions.
Let’s Try One! • PROBLEM:“Develop an application for car loans. Your application will ask for the sales price, down payment amount, APR and the length of the loan (in months). Using the formula on the next screen, your program will return a monthly payment.”
Step 1a – Identify Inputs for main() • Yes/No to question “Do you want to calculate the monthly payment for a car loan?” (Validate for Yes/No) • Sales Price (Validate for Number) • Down Payment (Validate for Number) • APR (Validate for Number) • Length of Loan (In Months; Validate for Number)
Step 1b – Identify Processes for main() • Call Yes/No validation function • Call number validation function • Calculate Loan Amount:Car Price – Down Payment • Convert APR from percentage format to floating point number:APR / 100 • Call function to calculate monthly payment • Concatenate output message
Step 1c – Output for main() • Output a message that includes loan amount, APR, monthly payment and loan length.
Step 2 for Yes/No Validator Function • Inputs: • Question to Ask (get from main()) • Default Text (get from main()) • Processes • Ask user the question and see if their answer did not equal “YES” AND did not equal “NO.” • If the user didn’t answer “YES” or “NO”, let them know that “YES” and “NO” are the only acceptable answers. Ask the user the question again until they answer “YES” or “NO.” • Outputs • Give the validated answer back to main().
Step 2 for Number Validator Function • Inputs: • Question to Ask (get from main()) • Default Text (get from main()) • Processes • Ask the user to enter a number and check to see if they entered data that can be converted to a number. • If the user didn’t enter numeric data, let them know that the program only accepts numbers. Ask the user the question again until they enter numeric data. • Outputs • Give the validated answer back to main().
Step 2 for Calculate Monthly Payment Function • Inputs: • Loan Amount • APR • Length of the Loan, in months • Processes • Figure the monthly interest:APR / 12 • Use the formula below to calculate monthly payment • Outputs • Give the monthly payment back to main().
Step 3: Develop Pseudocode • NOTE: Instructor will give you pseudocode in class.
Step 4: Code the Program • Finished Code: http://www.cs.iupui.edu/~rmolnar/n341/examples/n341WritingFunctions.html
Let’s Try Another One! • PROBLEM:“Develop an application to calculate a student’s semester grade. Your application will ask for the scores from three exams. The application will then calculate the average of the exam scores and return the average along with a letter grade. The teacher uses a standard grading scale.”
The Coded Product • Finished Code: http://www.cs.iupui.edu/~rmolnar/n341/examples/n341FunctionsPassingValues.html
Commenting Functions • In your code, you need to provide descriptive comments for each of your functions. • When writing function comments, include the following: • A brief (less than 1 line) description of the function • A list of arguments for the function • A list of values returned by the function • A statement of the function’s purpose
Commenting Functions • Example: /*FUNCTION: calcAvg() * *PARAMETERS: IntX, IntY, IntZ – All *integer numbers * *RETURNS: The calculated average; *stored in local variable rtnAvg * *PURPOSE: To calculate the average *of three integer values, passed *by a calling procedure and then *return that average to the procedure. */