* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Functions Continued
Survey
Document related concepts
Transcript
Agenda Take-up Lab #7 (Functions) Additional C Programming Tools: Constants vs. Global Variables Additional Formatting with printf In-Class Exercise (Using Functions) Global Variables A global variable is a variable that applies to all parts of a program. The global variable is declared at the top of the program before the main function. Although global variables may appear to simplify programming, they can cause errors since other people using functions may use the same variable name for other purposes Constantly using global variables is not considered to be good programming practice. Global Variables #include <stdio.h> int NUM = 1; void function_1(void); int main (void) { printf (“NUM = %d\n”, NUM); function_1(); printf (“NUM = %d\n”, NUM); } void function_1(void){ NUM=NUM+5; } Global variables are declared before the main function. If using global variables, it is recommended to Capitalize them for identification The variable NUM is applied through all functions in the program When the global variable is changed within a function, it automatically changes its value anywhere in the program. WARNING: The use of global variables is not considered good programming practice, and you cannot use global variables for any of your assignments. Constants A constant is a variable that does not have its value change within the program, but its value remains constant throughout all of the functions in a program. Make certain that when using constants, that you select a constant that doesn’t change such as 60 minutes in an hour or pst=25% Constants are declared before the main function using the #define command. Constants #include <stdio.h> #define NUM 4 void function_1(void); int main (void) { printf (“NUM = %d\n”, NUM); function_1(); printf (“NUM = %d\n”, NUM); return 0; } void function_1(void){ int num2 = 2; num2 = num2 * NUM; printf (“num2 = %d\n”, num2); } Constant are declared before the main function. If using constants, it is recommended to Capitalize them for identification The value of NUM is applied through all functions in the program A constant cannot be changed. It can be used in other equations, but a constant cannot be changed in any function. Formatting with printf You can specify the width of a field in which your value will be displayed – this allows you to better align data. A number in immediately after the % symbol is used to specify the field width. A negative field width will left-justify values. A positive field width will rightjustify values. Formatting with printf Examples (Assume num1=34, num2=2.3, num3=2.5) Command printf (“Number:%5d\n”, num1); printf (“Number:%5.1lf\n”, num2); printf (“Number:%5.1lf\n”, num3); printf (“Number:%-5dEnd.\n”, num1); printf (“Number:%-5.1lfEnd.\n”, num2); printf (“Number:%-5.1lfEnd.\n”, num3); Note: If you do not select a field with for a double that is too large, the entire unformatted value will be displayed Output (“-” represent space) Number:---34 Number:--2.3 Number:--2.5 Number:34---End. Number:2.3--End. Number:2.5--End. In Class Exercise #1 Note the following information: num1 = 45 num2 = 546 num3 = 34567 num4 = 2.34 num5 = 12.345 num6 = 34.5654 Assuming that “-” represents a space, write the printf command to display the following: num1 is:--------45 numbers 1 and 2 are:---45—546 numbers 1 and 2 are:45---546. That’s all! ------2.34 ----12.345 ---34.5654 In Class Exercise #2 Write a program that will prompt the user for the quantity and price of an item. The program will calculate and then display an invoice displaying the amount of the sale, taxes (pst=8%, gst=7%) and invoice total. Within your program use constants for pst and gst, and use a function called double Calc_Taxes (double amount) that will be used to calculate and return total taxes. Suggested Answer #include <stdio.h> #define GST 7.0 #define PST 8.0 double Calc_Taxes (double); int main (void){ int quantity; double price, amount, taxes, total_amount; printf ("\nEnter the quantity of the item: "); scanf ("%d", &quantity); printf ("\nEnter the price of the item: "); scanf ("%lf", &price); amount = quantity * price; taxes = Calc_Taxes (amount); total_amount = amount + taxes; printf ("\nSales:\t$%.2lf\tTaxes:\t$%.2lf\tTotal:\t$%.2lf\n\n", amount, taxes, total_amount); return 0; } double Calc_Taxes (double amt){ double total_taxes, pst, gst; pst = amt * (PST / 100); gst = amt * (GST / 100); total_taxes = pst + gst; return total_taxes; }