Q&A: Functions

What are the 3 parts of a function header?

What is a return type? Which return type is used if the function returns no data?

What is an input parameter?

What is a function name?

How do you declare a function?

void DisplayMenu();                          // NO input    NO output
void Format( string fname, string lname );   // YES input   NO output
float GetTax();                              // NO input    YES output
float Sum( float a, float b );               // YES input   YES output

How do you define a function?

void DisplayMenu()                           // NO input    NO output
{
  cout << "0. QUIT" << endl;
  cout << "1. Withdraw" << endl;
  cout << "2. Deposit" << endl;
  cout << "3. View balance" << endl;
}

void Format( string fname, string lname )    // YES input   NO output
{
  cout << lname << ", " << fname << endl;
}

float GetTax()                               // NO input    YES output
{
  return 0.091;
}

float Sum( float a, float b )                // YES input   YES output
{
  return a + b;
}

How do you call a function?

int main()
{
  DisplayMenu();                             // NO input    NO output
  Format( "RW", "Singh" );                   // YES input   NO output
  float tax = GetTax();                      // NO input    YES output

  float num1 = 5, num2 = 10;
  float result = Sum( num1, num2 );          // YES input   YES output
}

What is an argument? How is it different from a parameter?

What is scope? How do you tell what the scope of a variable is?

Are variables pass-by-value or pass-by-reference as a default in C++?

How do you tell if a parameter is pass-by-value or pass-by-reference?

void WorkWithCopies( int a, string b );       // pass-by-value
void WorkWithReferences( int& a, string& b ); // pass-by-reference
void WorkWithPointers( int* a, string* b );   // pointers

What does pass-by-value mean?

void WorkWithCopies( int a, string b );       // pass-by-value

int main()
{
  int my_number = 50;
  string my_string = "Hello";

  // my_number is an argument, and its value is copied to WorkWithCopies' "a" variable.
  // my_string is an argument, and its value is copied to WorkWithCopies' "b" variable.
  WorkWithCopies( my_number, my_string );

  return 0;
}

What does pass-by-reference mean?

void WorkWithReferences( int& a, string& b ); // pass-by-reference

int main()
{
  int my_number = 50;
  string my_string = "Hello";

  // my_number is an argument, this variable is directly accessed via the WorkWithReferences' "a" variable.
  // my_string is an argument, this variable is directly accessed via the WorkWithReferences' "b" variable.
  WorkWithReferences( my_number, my_string );

  return 0;
}

What does passing a pointer look like?

void WorkWithPointers( int* a, string* b );   // pointers

int main()
{
  int my_number = 50;
  string my_string = "Hello";

  // my_number is an argument, its address is passed to the WorkWithPointers function,
  // where the address is stored in the "a" variable.
  // my_string is an argument, its address is passed to the WorkWithPointers function,
  // where the address is stored in the "b" variable.
  WorkWithPointers( &my_number, &my_string );

  return 0;
}

Author: Rachel Wil Sha Singh

Created: 2023-10-27 Fri 16:05

Validate