- What is a function?
- What is a return (output)?
- What is a parameter (input)?
Keep these examples in your notes:
- Function NO INPUT / NO OUTPUT
Function definition example:
Python:
def DisplayMainMenu(): print( "1. Play game" ) print( "2. Load game" ) print( "3. Quit" )
C++:
void DisplayMainMenu() { cout << "1. Play game" << endl; cout << "2. Load game" << endl; cout << "3. Quit" << endl; }
Function call example:
Python:
DisplayMainMenu()
C++:
DisplayMainMenu();
- Function YES INPUT / NO OUTPUT
Function definition example:
Python:
def Display( name, price ): print( "Product:", name, "Price: $", price )
C++:
void Display( string name, float price ) { cout << "Product: " << name; cout << "Price: $" << price << endl; }
Function call example:
Python:
product1name = "Burger"; product1price = 7.75 Display( product1name, product1price )
C++:
string product1name = "Burger"; float product1price = 7.75; Display( product1name, product1price );
- Function YES INPUT / YES OUTPUT
Function definition example:
Python:
def Add( num1, num2 ): return num1 + num2
C++:
int Add( int num1, int num2 ) { return num1 + num2; }
Function call example:
Python:
number1 = int( input( "Enter first number: " ) ) number2 = int( input( "Enter second number: " ) ) result = Add( number1, number2 ) print( "Result is:", result )
C++:
int number1, number2; cout << "Enter first number: "; cin >> number1; cout << "Enter number 2: "; cin >> number2; int result = Add( number1, number2 ); cout << "Result is: " << result << endl;
- Function NO INPUT / YES OUTPUT
Function definition example:
Python:
def GetTax(): return 0.091
C++:
float GetTax() { return 0.091; }
Function call example:
Python:
tax = GetTax() total_price = price + price * tax print( "Total price; $", total_price )
C++:
float tax = GetTax(); total_price = price + price * tax; cout << "Total price: $" << total_price << endl;