FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 STUDY GUIDE FOR MODULE NO. 1 USER-DEFINED FUNCTIONS MODULE OVERVIEW Functions are an integral part of C++ programming such that you can’t even create C++ programs without using functions. In this module, we will be concentrating on the creation of your own functions. MODULE LEARNING OBJECTIVES By the end of this module you should be able to: Identify the structure and appropriate use of user-defined functions Differentiate and implement the proper use of void and non-void functions Implement function prototypes LEARNING CONTENTS (USER-DEFINED FUNCTION DEFINITION) Introduction A user-defined function is a group of codes performing a specific task and given with a specific name (function name or identifier). When the function name is called in any part of the program, all codes inside it are executed. Hence, we could use functions to segment our code to perform individual tasks. 1.1 Function Declaration In order to declare a function, the following structure shall be used: returnDataType functionName(parameter1, parameter2, ...) { //function body statements; return expression; } The returnDataType is the data type of the value in which the function will return. Some functions do not return a value; which the keyword void should be used. The functionName is the name of the function. The series of parameters after the function name is called the parameter list. Parameters are variables that hold the value passed to the function when it is called. Parameters are optional, that is why functions may contain no parameters. The function name and the parameter list forms the function signature. The function body contains the statements that are executed when the function is called. Let’s start with examining the following codes: C++ Code [function1.cpp] #include<iostream> using namespace std; void printHello() PANGASINAN STATE UNIVERSITY 1 FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 { cout<<"Hello from User-defined Function!\n"; } int main() { printHello(); cout<<"Hi from Main"; } [Sample Output] Hello from User-defined Function! Hi from Main Extracted from the code above, we have the following user-defined function: In the example, the return data type for the function is void, marking the function as a void function. Void functions do not return a value to the function caller. It is important to note that the function void printHello() is defined before the main function, as we have not yet created a function prototype. 1.2 Function Call The main function acts as our starting point for our program. Inside the function, we called the printHello() function. When the function is called, the function body of that function is executed. After executing the function body, the execution returns to the function caller, which is the main function. PANGASINAN STATE UNIVERSITY 2 FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 You can also call the function multiple times, executing the function body multiple times based on the number of function calls. 1.3 Return values We can create functions that return a value to the function caller. These functions are what we call non-void functions. To declare a non-void function, we need to indicate a data type in the returnDataType part of our function definition. Also inside the function body, a return statement shall be used to indicate the value to be returned to the function caller. Let’s examine the following code: C++ Code [functio21.cpp] #include<iostream> using namespace std; int square() { return 2*2; } int main() { int num=square(); cout<<num; return 0; } [Sample Output] 4 We have a square() function which returns an int value, given that the returnDataType is int. It has also a return statement which returns the value 4 from the given expression. In the main function, after declaring the num variable, it is initialized from the returned value of the square() function. Hence, the value of the num variable is 4. Non-void functions can return any value depending on the specified return type. We have provided before regarding void functions. These functions are not required to return a value which explains the absence of the return statement inside the function body. Also, void functions declared with void on the returnDataType. 1.4 Function Parameters A parameter is a variable that holds the value that is passed when the function is called. In the previous examples, our parameter list is empty. Let’s examine the following codes with a parameter. C++ Code [function3.cpp] #include<iostream> using namespace std; int square(int sq) { return sq*sq; } PANGASINAN STATE UNIVERSITY 3 FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 int main() { int num=0; cout<<"Enter a number:"; cin>>num; cout<<"The square of "<<num<<" is "<<square(num); return 0; } [Sample Output] Enter a number:5 The square of 5 is 25 The square() function has a parameter int sq. In the main function, when the square() function is called, the value of num is passed to the sq variable. That is used in the sq*sq expression and returned to the function caller. You can declare multiple parameters with different data types. Let’s provide another example: C++ Code [function4.cpp] #include<iostream> using namespace std; float sum(int num1, float num2) { float ans=num1+num2; return ans; } int main() PANGASINAN STATE UNIVERSITY 4 FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 { int n1{0}; float n2{0}; cout<<"Enter an integer number: "; cin>>n1; cout<<"Enter a float number: "; cin>>n2; cout<<"Sum: "<<sum(n1, n2); return 0; } [Sample Output] Enter an integer number: 3 Enter a float number: 4.7 Sum: 7.7 The sum() function returns a float value but accepts two parameters, int num1 and float num2. The result of the operation from the function is displayed inside the main function. 1.5 Arguments Looking back with the previous examples we have, we are passing values to the function through the use of function parameters. The parameters act as placeholders for the values being passed. However, the value that is passed to the function is what we call an argument. Code Snippet cout<<"Sum: "<<sum(n1, n2); In the code snippet above, the function call sum will pass the values of n1 and n2 to the parameters of the function. Hence, n1 and n2 are the arguments, while the num1 and num2 are the parameters. PANGASINAN STATE UNIVERSITY 5 FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 1.6 Function Prototype We have already defined functions in the previous examples. However, you may notice that the function definition comes before the function call. If we want to define a function after a function call, we need to use a function prototype. A function prototype consists of a function’s return type, parameters but with no function body. returnDataType functionName(parameter1, parameter2, ...); or returnDataType functionName(dataType, dataType, ...); Given the following example, check the line that serves as the function prototype. C++ Code [function5.cpp] #include<iostream> using namespace std; float sum(int num1, float num2); int main() { int n1{0}; float n2{0}; cout<<"Enter an integer number: "; cin>>n1; cout<<"Enter a float number: "; cin>>n2; cout<<"Sum: "<<sum(n1, n2); return 0; } float sum(int num1, float num2) { float ans=num1+num2; return ans; } [Sample Output] Enter an integer number: 1 Enter a float number: 1.2 Sum: 2.2 The line float sum(int num1, float num2); is the function prototype for our example. Also, notice that the function definition for the sum() can now be placed under the main() function. Another way of declaring a function prototype is: C++ Code [function5.cpp] #include<iostream> using namespace std; float sum(int, float); PANGASINAN STATE UNIVERSITY 6 FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 int main() { int n1{0}; float n2{0}; cout<<"Enter an integer number: "; cin>>n1; cout<<"Enter a float number: "; cin>>n2; cout<<"Sum: "<<sum(n1, n2); return 0; } float sum(int num1, float num2) { float ans=num1+num2; return ans; } [Sample Output] Enter an integer number: 1 Enter a float number: 1.2 Sum: 2.2 1.7 Function Overloading In C++, different functions can have the same name. We call these as overloaded functions. For example, int max (int x, int y); double max (double x, double y); In the example, two different functions share the same name which is max. The function max() was overloaded for int and double return types. However, remember that functions can have the same name if the number and/or data type of the arguments passed is different. Based on the type of parameter passed during the function call, the appropriate function is called. C++ Code [function5.cpp] #include<iostream> using namespace std; int max(int, int); double max(double, double); int main(){ int n1 = 0, n2 = 0; double n3 = 0, n4 = 0; cout << "Enter two numbers(int):" << endl; cin >> n1 >> n2; cout << "Max: " << max(n1, n2) << endl; cout << "Enter two numbers(double):" << endl; cin >> n3 >> n4; cout << "Max: " << max(n3, n4); PANGASINAN STATE UNIVERSITY 7 FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC 103 Intermediate Programming Module No. 1 return 0; } int max (int x, int y){ if(x>y) return x; else return y; } double max(double x, double y){ if(x>y) return x; else return y; } [Sample Output] Enter two numbers(int): 5 2 Max: 5 Enter two numbers(double): 3.4 6.2 Max: 6.2 LEARNING ACTIVITY 1 Create a non-void function in your program that will ask the user to enter an int number. The entered number will be returned to the main function and will be displayed. Create a void function named celsiusToFahrenheit that will convert a Celcius temperature passed to it and displays the converted Fahrenheit temperature. The input should be done in the main function. SUMMARY User-defined functions make your code reusable as you can declare them once and use them multiple times. Further, your codes divided into functions can be managed more easily and increases code readability. Hence, the use of user-defined functions is encouraged. REFERENCES Zak, D. (2016). An Introduction to Programming with C++ (8E), pages 291-341 Online Reading Materials: http://cplusplus.com/doc/tutorial/functions/ https://www.programiz.com/cpp-programming/function https://www.programiz.com/cpp-programming/user-defined-function-types https://www.programiz.com/cpp-programming/function-overloading PANGASINAN STATE UNIVERSITY 8