0% found this document useful (0 votes)
5 views6 pages

Additional Notes Basic and Functions

The document outlines basic programming concepts, including the definitions of parameters, arguments, and functions/methods. It describes four types of functions based on parameter passing and return values, and provides code examples for each type. Additionally, it presents a comprehensive program that integrates all function types to solve a practical problem involving calculating balance after a purchase.

Uploaded by

Siti Farhana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Additional Notes Basic and Functions

The document outlines basic programming concepts, including the definitions of parameters, arguments, and functions/methods. It describes four types of functions based on parameter passing and return values, and provides code examples for each type. Additionally, it presents a comprehensive program that integrates all function types to solve a practical problem involving calculating balance after a purchase.

Uploaded by

Siti Farhana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ADDITIONAL NOTES

BASIC PROGRAMMING KNOWLEDGE

parameter same meaning with argument

function same meaning with method

parameter/argument = variable that we used in coding

such as:

//data type = int, double, string,char, void,float...

//variable = created with any name without used reserve word/library word

= cannot start with number

= cannot include space or any special character

//example: int num1;

: double x;

: string y; and etc;

TYPES OF FUCNTION/METHOD OTHER THAN main METHOD

1. pass parameter & return value

= bagi/terima variable pulangkan nilai

2. pass parameter NOT return value

= bagi/terima variable tapi TIDAK pulangkan nilai

3. NOT pass parameter & NOT return value

= TIDAK menerima apa2 variable dan TIDAK juga pulangkan nilai

4. NOT pass parameter & return value

= TIDAK terima apa2 variable tetapi memulangkan nilai

**kesemua function di atas boleh digabungkan dalam satu coding


EXAMPLES:

Scenario/problem

Your mom ask you to buy a tin of sardin and 1kg flour. Then your mom give RM 50, how many
balance you get?

1. pass parameter & return value

#include <iostream>

using namespace std;

//declare function

double kira(double,double);

int main()

double total, baki, bayaran;

cout<<"Menu:";

cout<<"Sardin = RM 3.50";

cout<<"Flour = RM 2.55";

cout<<"Total: ";

total= 3.50+2.55;

cout<<"Total: "<<total;

cout<<"Payment: ";

cin >> bayaran;

baki = kira(total,bayaran);

cout<<”Balance: ”<<baki;

}//end main

double kira(double total, double bayaran)

double bal;

bal = bayaran - total;

return bal;

}
2. pass parameter NOT return value

#include <iostream>

using namespace std;

//declare function

void kira(double,double);

int main()

double total, bayaran;

cout<<"Menu:";

cout<<"Sardin = RM 3.50";

cout<<"Flour = RM 2.55";

cout<<"Total: ";

total= 3.50+2.55;

cout<<"Total: "<<total;

cout<<"Payment: ";

cin >> bayaran;

kira(total,bayaran);

}//end main

void kira(double total, double bayaran)

double bal;

bal = bayaran - total;

cout<<”Balance: ”<<bal;

}
3. NOT pass parameter NOT return value

#include <iostream>

using namespace std;

//declare function

void kira( );

int main()

cout<<"Menu:";

cout<<"Sardin = RM 3.50";

cout<<"Flour = RM 2.55";

kira( );

}//end main

void kira( )

double bal, bayaran, total;

cout<<"Total: ";

total= 3.50+2.55;

cout<<"Total: "<<total;

cout<<"Payment: ";

cin >> bayaran;

bal = bayaran - total;

cout<<”Balance: ”<<bal;

}
4. NOT pass parameter & return value

#include <iostream>

using namespace std;

//declare function

double kira( );

int main()

double baki;

cout<<"Menu:";

cout<<"Sardin = RM 3.50";

cout<<"Flour = RM 2.55";

baki = kira( );

cout<<”Balance: ”<<baki;

}//end main

double kira( )

double bal, bayaran, total;

cout<<"Total: ";

total= 3.50+2.55;

cout<<"Total: "<<total;

cout<<"Payment: ";

cin >> bayaran;

bal = bayaran - total;

return bal;

}
5. Used all types of function above in one full program/coding

1. #include <iostream>
2. using namespace std;

3. //declare functions
4. void display_menu( ); //NOT pass parameter NOT return value
5. double jumlah (double,double); //pass parameter & return value
6. double bayar( ); //NOT pass parameter & return value
7. void kira (double,double); //pass parameter NOT return value

8. int main()
9. {
10. display_menu();
11. }

12. void display_menu( )


13. {
14. double total1,total2,total,pay;
15. cout<<"Menu:\n";
16. cout<<"Sardin = RM 3.50\n";
17. cout<<"Flour = RM 2.55\n";
18. cout<<"Total 1: \n";
19. cin>>total1;
20. cout<<"Total 2: \n";
21. cin>>total2;
22. total= jumlah(total1, total2);
23. cout<<"TOTAL = \n"<< total;
24. cout<< "\nPayment: \n";
25. pay= bayar();
26.
27. kira (total,pay);
28. }

29. double jumlah (double total1,double total2)


30. {
31. double jum;
32. jum= total1+total2;
33. return jum;
34. }

35. double bayar( )


36. {
37. double bayaran;
38. cin >> bayaran;
39. return bayaran;
40. }

41. void kira(double total, double pay)


42. {
43. double bal=0;
44. bal = pay - total;
45. cout<<"Balance: \n"<< bal;
46. }

You might also like