Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.6 Pre-Defined Functions, User-Defined Function: Value Returning Functions Prepared for By: Name: ID: Section: Semester: Total Marks: Obtained Marks: Signature: Date:
Contents of Lab 1 6.1. Objectives:... 3 6.2. Predefined Functions... 3 Example 1:... 4 6.3. User-Defined Functions... 5 6.4. Value-Returning Functions... 5 6.5. Syntax: Value-Returning function... 6 Example 2:... 6 Example 3:... 7 6.6. Lab Tasks:... 8 Task 1:... 8 Task 2:... 8 Task 3:... 8 6.7. Home Assignment:... 8 Task 1:... 8 Task 2:... 8 Task 3:... 9 2 Engr. Syed Intazar Hyder
Pre-Defined Functions, User-Defined Function: Value Returning Functions 6.1. Objectives: To learn about predefined functions. To learn how to use them in programs. To learn about User-defined, Value Returning Functions To learn how to use them 6.2. Predefined Functions In C++, the concept of a function, either predefined or user-defined, is similar to that of a function in algebra. For example, every function has a name and, depending on the values specified by the user, it does some computation. Some of the predefined mathematical functions are pow (x, y), sqrt (x), and floor (x). Table 1: Pre-Defined Functions 3 Engr. Syed Intazar Hyder
To use predefined functions in a program, you must include the header file that contains the function s specification via the include statement. For example, to use the function pow, the program must include: #include <cmath> Example 1: // Lab_6_Example_1_Predefined functions Result //How to use predefined functions. #include <iostream> #include <cmath> #include <cctype> using namespace std; int main() int x; double u, v; cout << "Uppercase a is " << static_cast<char>(toupper('a')) << endl; u = 4.2; v = 3.0; cout << u << " to the power of " << v << " = " << pow(u, v) << endl; cout << "5.0 to the power of 4 = " << pow(5.0, 4) << endl; u = u + pow(3.0, 3); cout << "u = " << u << endl; x = -15; cout << "Absolute value of " << x 4 Engr. Syed Intazar Hyder
<< " = " << abs(x) << endl; return 0; 6.3. User-Defined Functions As Example-1 illustrates, using functions in a program greatly enhances the program s readability because it reduces the complexity of the function main. Also, once you write and properly debug a function, you can use it in the program (or different programs) again and again without having to rewrite the same code repeatedly. Because C++ does not provide every function that you will ever need and designers cannot possibly know a user s specific needs, you must learn to write your own functions. User-defined functions in C++ are classified into two categories: Value-returning functions-functions that have a return type. These functions return a value of a specific data type using the return statement, which we will explain shortly. Void functions-functions that do not have a return type. These functions do not use a return statement to return a value. 6.4. Value-Returning Functions The previous section introduced some predefined C++ functions such as pow, abs, islower, and toupper. These are examples of value-returning functions. To use these functions in your programs, you must know the name of the header file that contains the functions specification. You need to include this header file in your program using the include statement and know the following items: 1. The name of the function 2. The number of parameters, if any 3. The data type of each parameter 4. The data type of the value computed (that is, the value returned) by the function, called the type of the function Because the value returned by a value-returning function is unique, the natural thing for you to do is to use the value in one of three ways: Save the value for further calculation. Use the value in some calculation. Print the value. This suggests that a value-returning function is used: In an assignment statement. As a parameter in a function call. In an output statement. That is, a value-returning function is used (called) in an expression. 5. The code required to accomplish the task. 5 Engr. Syed Intazar Hyder
6.5. Syntax: Value-Returning function The syntax of a value-returning function is: Computing Fundamentals Lab 6 functiontype functionname(formal parameter list) statements in which statements are usually declaration statements and/or executable statements. In this syntax, functiontype is the type of the value that the function returns. The functiontype is also called the data type or the return type of the value-returning function. Moreover, statements enclosed between curly braces form the body of the function. Example 2: // Lab_6_Example_2_user-defined functions #include<iostream> using namespace std; double country(); double city(); double institute(); int main() country(); institute(); return 0; Result double country() cout<<"my country is Pakistan\n"; city(); double city() 6 Engr. Syed Intazar Hyder
cout<<"my city is Lahore\n"; double institute() cout<<"my istitute is Superior University"; Example 3: // Lab_6_Example_3_user-defined functions //Program: Largest of three numbers #include <iostream> using namespace std; double larger(double x, double y); double comparethree(double x, double y, double z); int main() double x, y; cout << "Enter two numbers: \n"; cin >> x >> y; cout << endl; cout << "The larger of " << x << " and " << y << " is: " << larger(x, y) << endl; Result double a, b, c; cout << "Enter three numbers: \n"; cin>>a>>b>>c; cout<<endl; cout<<"the Larger of "<< a <<", " << b <<" and "<<c <<" is: " <<comparethree(a, b, c) << endl; return 0; double larger(double x, double y) double max; if (x >= y) max = x; else max = y; return max; double comparethree (double x, double y, double z) return larger(x, larger(y, z)); 7 Engr. Syed Intazar Hyder
6.6. Lab Tasks: Task 1: Write a function to calculate the factorial of a number that is being generated by random from random generator and call it main function. Task 2: Write a function that rolls a pair of dice until the sum of the numbers rolled is a specific number. We also want to know the number of times the dice are rolled to get the desired sum. The smallest number on each die is 1, and the largest number is 6. So the smallest sum of the numbers rolled is 2, and the largest sum of the numbers rolled is 12. Task 3: Write a program that uses the function isnumpalindrome given in Example 6-5 (Palindrome Number). Test your program on the following numbers: 10, 34, 22, 333, 678, 67876, 44444, and 123454321. 6.7. Home Assignment: Task 1: Write a function to with function larger is used to determine the largest number from a set of 10 numbers. Task 2: Write a function that calculates and prints the bill for a cellular telephone company. The company offers two types of service: regular and premium. Its rates vary, depending on the type of service. The rates are computed as follows: Regular service: $10.00 plus first 50 minutes are free. Charges for over 50 minutes are $0.20 per minute. Premium service: $25.00 plus: a. For calls made from 6:00 a.m. to 6:00 p.m., the first 75 minutes are free; charges for more than 75 minutes are $0.10 per minute. b. For calls made from 6:00 p.m. to 6:00 a.m., the first 100 minutes are free; charges for more than 100 minutes are $0.05 per minute. Your program should prompt the user to enter an account number, a service code (type char), and the number of minutes the service was used. A service code of r or R means regular service; a service code of p or P means premium service. Treat any other character as an error. Your program should output the account number, type of service. For the premium service, the customer may be using the service during the day and the night. Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was used during the day and the number of minutes the service was used during the night. 8 Engr. Syed Intazar Hyder
Task 3: Write a function that rolls a pair of dice until the sum of the numbers rolled is a specific number. We also want to know the number of times the dice are rolled to get the desired sum. The smallest number on each die is 1, and the largest number is 6. So the smallest sum of the numbers rolled is 2, and the largest sum of the numbers rolled is 12. No. of attempts must limit to 6 times. 9 Engr. Syed Intazar Hyder