Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.7 User Defined Functions II Prepared for By: Name: ID: Section: Semester: Total Marks: Obtained Marks: Signature: Date:
Contents of Lab 1 7.1. Objectives:... 3 7.2. Void Functions... 3 Actual parameter list... 4 Example 1:... 4 7.3. Lab Tasks:... 8 Task 1:... 8 7.4. Home Assignment:... 9 Task 1:... 9 Task 2:... 10 2 Engr. Syed Intazar Hyder
User Defined Functions II 7.1. Objectives: To learn how to construct and use void functions in a program To learn and practice reference parameter and value-returning functions 7.2. Void Functions Void functions and value-returning functions have similar structures. Both have a heading and a body. Like value-returning functions, you can place user-defined void functions either before or after the function main. However, the program execution always begins with the first statement in the function main. If you place user-defined void functions after the function main, you should place the function prototype before the function main. A void function does not have a data type. Therefore, functiontype that is, the return type in the heading part and the return statement in the body of the void functions are meaningless. However, in a void function, you can use the return statement without any value; it is typically used to exit the function early. Like value-returning functions, void functions may or may not have formal parameters. Function Definition: The function definition of void functions with parameters has the following syntax: in which statements are usually declaration and/or executable statements. The formal parameter list may be empty, in which case, in the function heading, the empty parentheses are still needed. Formal parameter list The formal parameter list has the following syntax: You must specify both the data type and the variable name in the formal parameter list. Function call The function call has the following syntax: 3 Engr. Syed Intazar Hyder
Actual parameter list The actual parameter list has the following syntax: In which expression can consist of a single constant value. As with value-returning functions, in a function call, the number of actual parameters together with their data types must match the formal parameters in the order given. Actual and formal parameters have a one-to-one correspondence. A function call results in the execution of the body of the called function. Example 1: // Lab_7_Example_1.cpp //Program: Print a triangle of stars #include <iostream> using namespace std; void printstars(int blanks, int starsinline); int nooflines; //variable to store the number of lines int counter; //for loop control variable int noofblanks; //variable to store the number of blanks cout << "Enter the number of star lines (1 to 20) " << "to be printed: "; cin >> nooflines; while (nooflines < 0 nooflines > 20) cout << "Number of star lines should be " << "between 1 and 20" << endl; cout << "Enter the number of star lines " << "(1 to 20) to be printed: "; cin >> nooflines; cout << endl << endl; noofblanks = 30; for (counter = 1; counter <= nooflines; counter++) printstars(noofblanks, counter); noofblanks--; return 0; void printstars(int blanks, int starsinline) Result 4 Engr. Syed Intazar Hyder
int count; for (count = 1; count <= blanks; count++) cout << ' '; for (count = 1; count <= starsinline; count++) cout << " * "; cout << endl; Value Parameters When a function is called, the value of the actual parameter is copied into the corresponding formal parameter. If the formal parameter is a value parameter, then after copying the value of the actual parameter, there is no connection between the formal parameter and actual parameter; that is, the formal parameter has its own copy of the data. Therefore, during program execution, the formal parameter manipulates the data stored in its own memory space. Example 2: // Lab_7_Example_2.cpp //Program illustrating how a value parameter works. #include <iostream> using namespace std; void funcvalueparam(int num); int number = 6; cout << "Before calling the function " << "funcvalueparam, number = " << number << endl; funcvalueparam(number); cout << "After calling the function " << "funcvalueparam, number = " << number << endl; return 0; void funcvalueparam(int num) cout << "In the function funcvalueparam, " << "before changing, num = " << num << endl; num = 15; cout << "In the function funcvalueparam, " << "after changing, num = " << num << endl; Result 5 Engr. Syed Intazar Hyder
Reference Variables as Parameters A reference parameter receives the address (memory location) of the actual parameter, reference parameters can pass one or more values from a function and can change the value of the actual parameter. Reference parameters are useful in three situations: When the value of the actual parameter needs to be changed When you want to return more than one value from a function When passing the address would save memory space and time relative to copying a large amount of data Example 3: // Lab_7_Example_3.cpp //This program reads a course score and prints the //associated course grade. #include <iostream> using namespace std; void getscore(int& score); void printgrade(int score); int coursescore; cout << "Line 1: Based on the course score, \n" << " this program computes the " << "course grade." << endl; //Line 1 getscore(coursescore); //Line 2 printgrade(coursescore); //Line 3 return 0; void getscore(int& score) cout << "Line 4: Enter course score: "; //Line 4 cin >> score; //Line 5 cout << endl << "Line 6: Course score is " << score << endl; //Line 6 void printgrade(int cscore) cout << "Line 7: Your grade for the course is "; //Line 7 if (cscore >= 90) //Line 8 cout << "A." << endl; else if (cscore >= 80) cout << "B." << endl; else if(cscore >= 70) cout << "C." << endl; Result 6 Engr. Syed Intazar Hyder
else if (cscore >= 60) cout << "D." << endl; else cout << "F." << endl; Value and Reference Parameters and Memory Allocation Example 3: // Lab_7_Example_4.cpp Result //Reference and value parameters #include <iostream> using namespace std; void funone(int a, int& b, char v); void funtwo(int& x, int y, char& w); int num1, num2; char ch; num1 = 10; //Line 1 num2 = 15; //Line 2 ch = 'A'; //Line 3 cout << "Line 4: Inside main: num1 = " << num1 << ", num2 = " << num2 << ", and ch = " << ch << endl; //Line 4 funone(num1, num2, ch); //Line 5 cout << "Line 6: After funone: num1 = " << num1 << ", num2 = " << num2 << ", and ch = " << ch << endl; //Line 6 funtwo(num2, 25, ch); //Line 7 cout << "Line 8: After funtwo: num1 = " << num1 << ", num2 = " << num2 << ", and ch = " << ch << endl; //Line 8 return 0; void funone(int a, int& b, char v) int one; one = a; //Line 9 a++; //Line 10 b = b * 2; //Line 11 v = 'B'; //Line 12 cout << "Line 13: Inside funone: a = " << a << ", b = " << b << ", v = " << v << ", and one = " << one << endl; //Line 13 void funtwo(int& x, int y, char& w) x++; //Line 14 7 Engr. Syed Intazar Hyder
y = y * 2; //Line 15 w = 'G'; //Line 16 cout << "Line 17: Inside funtwo: x = " << x << ", y = " << y << ", and w = " << w << endl; //Line 17 7.3. Lab Tasks: Task 1: Consider the definition of the function main. int x, y; char z; double rate, hours; double amount;... The variables x, y, z, rate, and hours referred to in items a through f below are the variables of the function main. Each of the functions described must have the appropriate parameters to access these variables. Write the following definitions: a) Write the definition of the function initialize that initializes x and y to 0 and z to the blank character. b) Write the definition of the function gethoursrate that prompts the user to input the hours worked and rate per hour to initialize the variables hours and rate of the function main. c) Write the definition of the value-returning function paycheck that calculates and returns the amount to be paid to an employee based on the hours worked and rate per hour. The hours worked and rate per hour are stored in the variables hours and rate, respectively, of the function main. The formula for calculating the amount to be paid is as follows: For the first 40 hours, the rate is the given rate; for hours over 40, the rate is 1.5 times the given rate. d) Write the definition of the function printcheck that prints the hours worked, rate per hour, and the salary. e) Write the definition of the function funcone that prompts the user to input a number. The function then changes the value of x by assigning the value of the expression 2 times the (old) value of x plus the value of y minus the value entered by the user. f) Write the definition of the function nextchar that sets the value of z to the next character stored in z. g) Write the definition of a function main that tests each of these functions. 8 Engr. Syed Intazar Hyder
7.4. Home Assignment: Task 1: Consider the following C++ code: #include <iostream> #include <cmath> #include <iomanip> using namespace std; void func1(); void func2(/*formal parameters*/); int num1, num2; double num3; int choice; cout << fixed << showpoint << setprecision(2); do func1(); cin >> choice; cout << endl; if (choice == 1) func2(num1, num2, num3); cout << num1 << ", " << num2 << ", " << num3 << endl; while (choice!= 99); return 0; void func1() cout << "To run the program, enter 1." << endl; cout << "To exit the pogram, enter 99." << endl; cout << "Enter 1 or 99: "; void func2(/*formal parameters*/) //Write the body of func2. The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: a) Prompt the user to input two integers and store the numbers in a and b, respectively. b) If both of the numbers are nonzero: i. If a >= b, the value assigned to c is a to the power b, that is, a b. ii. If a < b, the value assigned to c is b to the power a, that is, b a. c) If a is nonzero and b is zero, the value assigned to c is the square root of the absolute value of a. d) If b is nonzero and a is zero, the value assigned to c is the square root of the absolute value of b. e) Otherwise, the value assigned to c is 0. The values of a, b, and c are passed back to the calling environment. After completing the definition of the func2 and writing its function prototype, test run your program. 9 Engr. Syed Intazar Hyder
Task 2: For research purposes and to better help students, the admissions office of your local university wants to know how well female and male students perform in certain courses. You receive a file that contains female and male student GPAs for certain courses. Due to confidentiality, the letter code f is used for female students and m for male students. Every file entry consists of letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Your program should use the following functions: a) Function openfiles: This function opens the input and output files, and sets the output of the floating-point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros. b) Function initialize: This function initializes variables such as countfemale, countmale, sumfemalegpa, and summalegpa. c) Function sumgrades: This function finds the sum of the female and male students GPAs. d) Function averagegrade: This function finds the average GPA for female and male students. e) Function printresults: This function outputs the relevant results. f) There can be no global variables. Use the appropriate parameters to pass information in and out of functions. 10 Engr. Syed Intazar Hyder