Sie sind auf Seite 1von 25

FAKULTI TEKNOLOGI KEJURUTERAAN

UNIVERSITI TEKNIKAL MALAYSIA MELAKA

COMPUTER PROGRAMMING

BETR 1343 SEMESTER 2 SESSION 2016/2017

LAB 4: DEVELOPING PROGRAMS USING FUNCTIONS

DATE 11/5/17

SUHANTHAN A/L RAVINTHERAN B071610805


NAME OF GROUP MEMBERS
& MATRIX NUMBER

3.

1.
NAME OF INSTRUCTORS
2.

2
VERIFICATION STAMP
EXAMINERS COMMENTS

TOTAL MARKS

3
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++

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.

2
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
3
PART A: EXERCISES

Section 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


4
3. Compile, run your program, and write your output. Please use your own paper.

Function to display output text and no parameter involve.

Welcome.This text execute form function display_text

Press any key to continue

[2 marks]

4. Determine the location of the following.

No. of Line

Function Declaration 7

Function Calling 13

Function Definition 18

[3 marks]

5. Explain the difference between a function declaration and function definition?

5
Function declaration is a prototype declaration while the function definition is
contains the statements that make up the function.

[2 marks]

6. In general format, function definition has two parts. Explain.

Function definition has two parts which is function header and function body.

[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.

#include<iostream>

using namespace std; //--------Global declaration functions and


variables areas

void display_text1();

void display_text2();

//--------Main function

int main()

cout<<"Function to display output text and no parameter involve."<<endl;

display_text1();

display_text2();

system("PAUSE");

} //Ending main loop function

//--------Functions areas

void display_text1()

cout<<"Welcome. This text execute form function 'display_text1'"<<endl;


6
} //Ending display_text function

void display_text2()

cout<<"Welcome. This text execute form function 'display_text2'"<<endl;

[3 marks]

Section 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 : ;


7
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.

Enter first number :90

Enter second number :80

8
The first result is 5

The second result is 5

The third result is 10

The fourth result is 14

Press any key to continue

[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?

Yes .but the first number can be key in and operation is running and the second
cannot key in

[1 marks]

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

#include <iostream>

using namespace std;

//--------Global declaration functions and variables areas

float subtraction (float,float);

int main ()

float num_1, num_2;

float answer;

cout<<"Enter first number : ";

cin>> num_1;

cout<<"Enter second number : ";

cin>> num_2;

9
cout<<endl<<endl;

answer = subtraction (7,2);

cout << "The first result is " << answer <<endl;

cout << "The second result is " << subtraction (7,2) <<endl;

cout << "The third result is "<< subtraction (num_1,num_2) << endl;

answer = 4 + subtraction (num_1,num_2);

cout << "The fourth result is " << answer <<endl;

system("pause");

}//Ending main loop function

//--------Functions areas

float subtraction (float a, float b)

float return_value;

return_value=a-b;

return (return_value);

[7 marks]

10
Section 3: Functions with Parameters Passing by Reference

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 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");

11
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. }

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

Enter first number : 9


Enter second number : 3

4. Write down your output. Use your own paper.

Enter the first number:9

Enter the second number:3

The contain in answer before pass by reference:0

The result is 27

The contain in answer before pass by reference:27

Press any key to continue_

[2 marks]

5. Run your program with the following inputs;

12
Enter first number : 90.98
Enter second number : 80.97

6. Do you have the output answer? Write your observation?

Can run.The program can run but not complete because only can run the first
number while number second does not run complete.

[1 marks]

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

#include <iostream>

using namespace std;

//--------Global declaration functions and variables areas

float subtraction (float,float);

int main ()

float num_1, num_2;

float answer;

cout<<"Enter first number : ";

cin>> num_1;

cout<<"Enter second number : ";

cin>> num_2;

cout<<endl<<endl;

answer = subtraction (7,2);

cout << "The first result is " << answer <<endl;

cout << "The second result is " << subtraction (7,2) <<endl;

cout << "The third result is "<< subtraction (num_1,num_2) << endl;

answer = 4 + subtraction (num_1,num_2);

13
cout << "The fourth result is " << answer <<endl;

system ("pause");

}//Ending main loop function

//--------Functions areas

float subtraction (float a, float b)

float return_value;

return_value=a-b;

return (return_value);

[7 marks]

PART B: PROBLEM SOLVING

Section 1: System Requirement

Write a C++ program based on the following requirements:

i. Write a program to determine whether a person qualifies for a credit card.


14
ii. To qualify, the person must have worked on his or her current job for at least 2
years and make at least RM 35000 per year.

iii. Write 2 function after inserting how many years working and total salary per year:

Function Qualify

Should explain that the applicant qualifies for the card and the annual interest
rate is 12800

Function No_Qualify

Should explain that the applicant does not qualify for the card and give
general explanation why.

iv. Copy+paste your program code and submit it with your lab report.

Example of program output:

25

15
Section 2: System Analysis

Input Process Output

Salary ,years if(salary>35000 && years>-2) qualify

qualify(); Congratulation!! You are qualify


for our credit card The annual
else interest rate for our card is 2% per
no_qualify(); year.Please call 1800-800-800 for
our services details.Thank you

no_qualify

Dear Customer,We are pleased to


inform you that you are not
qualified for our credit card Thank
You.Please Come Again

16
Section 3: System Design

1.Main function

Start

Enter
salary,year

(salary>35000 Display
&& years>-2) No_Qualify

Display Qualify

End

17
2.Qualify function

Start

18
Congratulation!!You are qualify for our
credit card The annual interest rate for
our card is 2% per year.Please call 1800-
800-800 for our services details.Thank
you

End

3.No Qualify

Start

Dear Customer,We are pleased to inform


you that you are not qualified for our
credit card Thank You.Please Come
Again

19
End

Section 4: Coding

#include <iostream>

using namespace std;

void qualify();

void no_qualify();

int main()

float salary,years;

cout<<"This program will determine if you qualify for our credit


card"<<endl;

cout<<"what is your annual salary ?"<<endl;

cin>>salary;

cout<<"How many years have you worked at your current jobs"<<endl;

cin>>years;

20
if(salary>35000 && years>-2)

qualify();

else

no_qualify();

system("pause");

void qualify()

cout<<"Congratulation!! You are qualify for our credit card


"<<endl;

cout<<"The annual interest rate for our card is 2% per


year."<<endl;

cout<<"Please call 1800-800-800 for our services details.Thank


you"<<endl;

void no_qualify()

cout<<"Dear Customer,We are pleased to inform you"<<endl;

cout<<"that you are not qualified for our credit card "<<endl;

cout<<"Thank You.Please Come Again"<<endl;

21
Section 5: System Testing

**Show the input you have inserted and also the program output console.

Test 1

Input Salary: 36000;

Years: 4;

22
Output

Test n

Input Salary:34000;

Years:3;

Output

23
PART C: DISCUSSION & CONCLUSION

1. Discussion

Based on discussion,we could know 3 parts of function which is no data


communication, one way data communication and two way data communication.
Besides ,if we had more time we also can improve by doing this program in
goodly. In making the improvement the function program ,must have many
exercise to make students get easier to understand.

2. Conclusion
While doing this program or lab ,we be able to easier to understand and we also
can doing the program goodly. However, our first impression we think this
program too hard but by doing many exercise such as compile the program it
will make students easier to understand.

24

Das könnte Ihnen auch gefallen