You are on page 1of 6

FAKULTI TEKNOLOGI KEJURUTERAAN

UNIVERSITI TEKNIKAL MALAYSIA MELAKA

COMPUTER PROGRAMMING

BTIG 1213

SEMESTER 2

SESSION 2011/2012

Lab 5 - Developing programs using functions

DATE
NAME OF GROUP MEMBERS
& MATRIX NUMBER

1.
2.
3.

NAME OF INSTRUCTORS

1.
2.
3.

EXAMINERS COMMENTS

VERIFICATION STAMP

TOTAL MARKS

Document Number
N/A
Revision Number
1.0 (sulaiman)

Issue Number
29.02.2012
Total Pages

1.0

LEARNING OUTCOMES
1. To apply function technique in the program development in C++ programming.
2. To apply function call and function with returning value
3. To identify error and troubleshoot a C++ program

2.0

EQUIPMENTS
1. Personal Computer
2. 'DEV C++' or 'Microsoft Visual C++ 2010 Express Edition' installed.

3.0

SYNOPSIS & THEORY


A function is a group of statements that is executed when it is called from some point of
the program. In C++, a program is made of one or more functions. One and only one of
which must be named int main( ).
A function is an independent module that will be called to do specific task. Each function
has its own name. When that name is encountered in a program, the execution of the
program branches to the body of that function. When the function is finished, execution
returns to the area of the program code from which it was called, and the program
continues on to the next line of code.

Type of function;
Pre-defined (or Standard) Function
User-defined Function
Pre-defined (or Standard) Function
Example :mathematical functions in <cmath> (called cmath header file) are:
The power function, pow(x,y), The square root function, sqrt(x), The floor function, floor(x)
etc.
User-defined Function
It has three parts:
Function declaration - is done first with a prototype declaration.
Function definition - contains the code to complete the task.
Function call (or invocation) - to call function to perform its task.
To use this type of function, the programmer has to call (or invoke) the function. But the
function must first be declared and defined before it can be used.

Please refer to lecture slides for more details about the how to write function declaration,
function call and function definition.
This lab we will investigate some application C++ programming which is applying various
types of function in the program development process, include function with and without
parameter, with and without return value.
4.0

PROCEDURE
You need to complete this procedure during your 3 hours labs.

4.1 Functions with No Parameters


1. Open your preferred C++ IDE, and create new console project or file.
2. Type the following program code 4.1:
1. // Student Name: xxxxxxxx Student ID: xxxxxxx
2. // Lab Title: do-while loop Date: xx/xx/xxxx
3.
4. #include<iostream>
5. using namespace std;
6. //--------Global declaration functions and variables areas
7. void display_text();
8.
9. //--------Main function
10.
int main()
11.
{
12.
cout<<"Function to display output text and no
parameter involve."<<endl;
13.
display_text();
14.
system("PAUSE");
15.
}//Ending main loop function
16.
17.
//--------Functions areas
18.
void display_text ()
19.
{
20.
cout<<"Welcome. This text execute form function
'display_text'"<<endl;
21.
return;
22.
}//Ending display_text function
3. Compile, run your program, and write your output. Please use your own paper.
[2 marks]
4. Determine the location of the following.
No. of Line
Function Declaration
Function Calling
Function Definition
[3 marks]
Explain the difference between a function declaration and function definition?
[2 marks]
6. In general format, function definition has two parts. Explain. [2 marks]
7. Add another function, with function name is display_text2 (to write any text
output). Compile and run. Write your code in your report.
[3 marks]
5.

4.2 Functions with Parameters passing by value


1. Open your preferred C++ IDE, and create new console project or file.
2. Type the following program code 4.2:
1. // Student Name: xxxxxxxx Student ID: xxxxxxx
2. // Lab Title: do-while loop Date: xx/xx/xxxx
3. #include <iostream>
4. using namespace std;
5. //--------Global declaration functions and variables areas
6. int subtraction (int,int);
7.
8. int main ()
9. {
10.
int num_1, num_2;
11.
int answer;
12.
cout<<Enter first number : ;
13.
cin>> num_1;
14.
cout<<Enter second number : ;
15.
cin>> num_2;
16.
cout<<endl<<endl;
17.
18.
answer = subtraction (7,2);
19.
cout << The first result is << answer << \n;
20.
cout << The second result is << subtraction (7,2)
<< \n;
21.
cout << The third result is << subtraction
(num_1,num_2) << \n;
22.
answer = 4 + subtraction (num_1,num_2);
23.
cout << The fourth result is << answer << \n;
24.
system (PAUSE);
25.
return 0;
26.
}//Ending main loop function
27.
28.
//--------Functions areas
29.
int subtraction (int a, int b)
23.
{
24.
int return_value;
25.
return_value=a-b;
26.
return (return_value);
27.
}
3. Compile, run your program. Enter the following inputs;

Enter first number : 90


Enter second number : 80
4. Write down your output. Use your own paper.
[2 marks]
5. Run your program with the following inputs;

Enter first number : 90.98


Enter second number : 80.97
6. Do you have the output answer? Write your observation?
[1 marks]

7. Now, you need to modify the program 4.2 to get the correct answer. Write down
your code in your report.
[7 marks]

4.3 Functions with Parameters passing by reference


8. Open your preferred C++ IDE, and create new console project or file.
9. Type the following program code 4.2:
1. // Student Name: xxxxxxxx Student ID: xxxxxxx
2. // Lab Title: do-while loop Date: xx/xx/xxxx'
3. #include <iostream>
4. using namespace std;
5. //--------Global declaration functions and variables areas
6. int multiplication(int a, int b, float &result);
7. int main ()
8. {
9. int num_1, num_2;
10. float answer=0, result;
11. cout<<"Enter first number : ";
12. cin>> num_1;
13. cout<<"Enter second number : ";
14. cin>> num_2;
15. cout<<endl<<endl;
16. cout << "The contain in answer before pass by reference: "
<< answer << '\n';
17. result = multiplication (num_1,num_2, answer);
18. cout << "The result is " << result << '\n';
19. cout << "The contain in answer after pass by reference: " <<
answer << '\n';
20. system ("PAUSE");
21. return 0;
22. }//Ending main loop function
23. //--------Functions areas
24. int multiplication(int a, int b, float &result)
25. {
26. result = a*b;
27. return result;
28. }

10. Compile, run your program. Enter the following inputs;

Enter first number : 9


Enter second number : 3
11. Write down your output. Use your own paper.
[2 marks]
12. Run your program with the following inputs;

Enter first number : 90.98


Enter second number : 80.97
13. Do you have the output answer? Write your observation?

[1 marks]
14. Now, you need to modify the program 4.2 to get the correct answer. Write down
your code in your report.
[7 marks]

4.4 Program Development by using function


Program Development is multi-step process that requires you understand the problem,
design a solution, write the program, and test the program. In this lab, you require to
design a solution by implementing function technique.
Develop a program by implementing function to these following processes. Follow the
function name as given. The process of calculation must be executed in the function.
1. To find the product of 3 numbers. --- function name product
2. To find the average of 3 numbers in process 1. --- function name
average_of_numbers
3. To find the area of a circle. --- function name area_of_circle
4. To find the perimeter of a square. --- function name perimeter_of_square
Verify your result with your lecturer and write down your code in your report.
[40 marks]
1. Input and output definition-10 marks
2. Process is correct-10 marks
3. Functions declaration, call, return value is correctly writing- 20 marks

You might also like