LAB 6 FUNCTIONS I School of Computer and Communication Engineering 1
Universiti Malaysia Perlis 1. OBJECTIVES: 1.1 To apply functions as building blocks of programs. 1.2 To write C programs using functions. 2 INTRODUCTION: A C program is generally formed by a set of functions, which subsequently consist of many programming statements. Using functions, a large computing task can be broken into smaller ones. Functions can be created to execute small, frequently-used tasks. In C, there are predefined functions or sometimes called standard functions, and userdefined functions. Predefined functions are already available functions which can be used, called library, such as stdio.h, math.h, string.h and stdlib.h. The library name must be included at the top of the source code (preprocessor directive). User-defined functions in a program are built using: Function prototype Function definition Function call 2.1 Function Prototype To use function in a program, it has to be declared at the beginning of a program, using function prototype. Function prototype has the following form: <return_type> <function_name> (arg_type arg_name,...); For example: int sum (int num1,int num2); //function named sum with two (2) arguments and returns integer data type. void sum (int num1,int num2); //function named sum with two (2) arguments but does not return any data. 2.2 Function Definition Function definition is the function body. It is used to define what the function does. The coding is written inside the function definition. Function definition has the following form: <return_type> <function_name> (arg_type arg_name,...) statements For example: int sum (int num1,int num2) int add; add = num1 + num2; return(add); 2
2.3 Function Call Function call can be made in the main function or in other functions. Function call has the following form: <function_name> (exp, exp...) exp is an expression can be variable or constant For example: result = sum(x,y); 3. TASKS: 3.1 a. Identify and correct the errors, if there are any, in the following program segments. Briefly explain why. i) int fnfunc (double ia); int fnfunc(int ia)) return 10*iA; ii) int fnfunc (int ia) return 2*; fnfunc (2, 3); iii) void fnfunc1 (int ia); int fnfunc1(int ia) int ib; ib=2*ia; return ib; 3
iv) double sum(double ia, double ib) return ia+ib; void fnfunc(double ia, double ib) double sum; sum = sum(ia,ib); v) int fnfunc3(int in) return in*in; ; int in; in= (int)fnfunc3(in); fnfunc3 (in); in = fnfunc3 (8); b. What is displayed by the program that follows? #include <stdio.h> void fnbizarre(int in); int main(void) double ix; ix = 35.8; fnbizarre(ix); printf("%.2f\n", ix); return (0); void fnbizarre(int in) printf("%4d ", in); 4
c. What is displayed by the program defined below? #include <stdio.h> double fnad1(double dx); double fntrpl(double dx); double fnhlf(double dx); int main(void) printf("%.3f\n", fnhlf(fntrpl(fnad1(8.2)))); return (0); double fnad1(double dx) return (dx + 1); double fntrpl(double dx) return (3 * dx); double fnhlf(double dx) return (0.5 * dx); d. Give the output of the following code: #include <stdio.h> void fnfunc(int ia, int ib, double dc); void fnfunc(int ia, int ib, double dc) printf( ia = %d\n, ia); printf( ib = %d\n, ib); printf( dc = %lf\n, dc); int main() fnfunc(3.3, 3.7, 4); return 0; 5
3.2 The formula for the volume of sphere is πr 3, where r is the radius of the sphere. Write a function to return the volume of a sphere with the input argument for the radius of the sphere. Test your program with a radius of 5 meters. 6
3.3 Write a program that calculates the area and perimeter of a rectangle. The area and perimeter are calculated using two separated functions, which both take two arguments of a double type for the width and length of the rectangle. Calculate the area and perimeter using these two functions in the main program for a rectangle with width and length of 2 cm and 5.5, respectively. 7
3.4 An integer number is a prime number if it is divisible only by 1 and itself. For example 2, 3, 5, 7 and 11 are prime numbers. Write a program that reads an integer and determines if it is a prime number. If it is not a prime number, print out the smallest divisor. Otherwise, print out the prime number. Create a function to return the smallest divisor for an integer. Use this function to determine and print all the prime numbers between 1 and 1000. (Hint: use the modulus operator % to determine if a number is divisible by another number. The smallest divisor of even number is 2. However you only need to test up to n to verify if it is divisible) 8
4. ADDITIONAL TASK: Write a C program that calculates a customer water bill. The water bill includes RM 5.00 basic cost and cost for water usage with rate of RM 1.10 per thousand liter. Water usage is calculated by subtracting current meter reading with previous month meter reading (meter is read in thousand liter unit).your program should check whether there is an unpaid bill. If the balance of unpaid bill is greater than 0, RM 2.00 fine is charged together with the unpaid bill and this will be included in the current monthly bill. The program will also calculate the bill collected for the day. The program will continue if user enters y- yes, else it will stop and display the collection for the day. Your program should use these functions : 1. calc_usage_cost - accepts previous and current meter reading, returns usage cost. 2. calc_unpaid_cost - accepts unpaid bill, returns unpaid cost. 3. calc_total_bill - accepts usage cost and unpaid cost, returns total bill. 4. print_bill - accepts account number and total bill. Sample Output -----------------Perlis Water---------------------- This program generates monthly water bill ------------------------------------------------------- Enter account number : 44444 Enter unpaid bill: 0 Enter previous month and current month meter reading: 1000 2000 Your account number is 4444 Your total bill is RM 6.10. Do you want to continue : y or n? y Enter account number : 22222 Enter unpaid bill: 10 Enter previous month and current month meter reading: 1000 2000 Your account number is 2222 Your total bill is RM 18.10. Do you want to continue : y or n? n Perlis Water collection : RM 24.20. 9