Additional Notes Basic and Functions
Additional Notes Basic and Functions
such as:
//variable = created with any name without used reserve word/library word
: double x;
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?
#include <iostream>
//declare function
double kira(double,double);
int main()
cout<<"Menu:";
cout<<"Sardin = RM 3.50";
cout<<"Flour = RM 2.55";
cout<<"Total: ";
total= 3.50+2.55;
cout<<"Total: "<<total;
cout<<"Payment: ";
baki = kira(total,bayaran);
cout<<”Balance: ”<<baki;
}//end main
double bal;
return bal;
}
2. pass parameter NOT return value
#include <iostream>
//declare function
void kira(double,double);
int main()
cout<<"Menu:";
cout<<"Sardin = RM 3.50";
cout<<"Flour = RM 2.55";
cout<<"Total: ";
total= 3.50+2.55;
cout<<"Total: "<<total;
cout<<"Payment: ";
kira(total,bayaran);
}//end main
double bal;
cout<<”Balance: ”<<bal;
}
3. NOT pass parameter NOT return value
#include <iostream>
//declare function
void kira( );
int main()
cout<<"Menu:";
cout<<"Sardin = RM 3.50";
cout<<"Flour = RM 2.55";
kira( );
}//end main
void kira( )
cout<<"Total: ";
total= 3.50+2.55;
cout<<"Total: "<<total;
cout<<"Payment: ";
cout<<”Balance: ”<<bal;
}
4. NOT pass parameter & return value
#include <iostream>
//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( )
cout<<"Total: ";
total= 3.50+2.55;
cout<<"Total: "<<total;
cout<<"Payment: ";
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. }