Functions in C++ Quiz
9 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the key distinction between a void function and a value-returning function in C++?

  • A value-returning function can only return primitive types, while void functions can handle user-defined types.
  • Void functions can be used in any context, whereas value-returning functions must always be part of an assignment expression.
  • A void function can modify global variables, while a value-returning function cannot.
  • A value-returning function must include a `return` statement to send back a value, while a void function does not. (correct)
  • What is the primary purpose of default arguments in a C++ function?

  • To ensure that all function parameters are always initialized.
  • To specify the data type of the return value.
  • To allow the function to accept a variable number of arguments.
  • To provide a default value for a parameter if it is not supplied in the function call. (correct)
  • How does 'pass-by-reference' differ from 'pass-by-value' in terms of how changes to parameters affect variables in the calling function?

  • Pass-by-reference only works with primitive types, and does not allow modifications, whereas pass-by-value works with any type, and allows changes to be seen by the caller.
  • Pass-by-value allows multiple modifications, while pass-by-reference only allows modifications in the called function, so changes to parameters will not affect the variables in the calling function.
  • Pass-by-value creates a new copy, modifications do not affect the original, whereas pass-by-reference directly uses the original, so modifications do affect the original. (correct)
  • Pass-by-reference is significantly faster than pass-by-value but only if the variable is very large, and always allows modifications to affect the original, whereas in pass-by-value, modifications will never affect the original.
  • What happens when a function encounters a return statement?

    <p>The current function stops execution and returns control to its caller with an optional return value.</p> Signup and view all the answers

    Why is dividing a program into multiple functions beneficial for program design?

    <p>It improves readability and maintainability by promoting modularity.</p> Signup and view all the answers

    Given a function isEven that returns true if a number is even and false otherwise. Which is the correct implementation?

    <pre><code class="language-cpp">bool isEven(int num) { return num % 2 == 0; } </code></pre> Signup and view all the answers

    A calculateAreaPerimeter function takes the length and breadth of a rectangle by reference to compute the area and perimeter, how will the original variables passed into the function behave?

    <p>The original length and breadth variables passed into the function will be modified by the function's calculations.</p> Signup and view all the answers

    A function greetUser takes a name as a string and optionally a greeting string, and prints the greeting followed by the name. How would you call the function with a default greeting 'Hello'?

    <pre><code class="language-cpp">greetUser(&quot;Alice&quot;); </code></pre> Signup and view all the answers

    Signup and view all the answers

    Study Notes

    Functions in C++

    • C++ functions are reusable blocks of code that perform specific tasks.
    • Functions enhance code organization, reusability, and maintainability.

    Theoretical Questions

    • Question 1: Void functions don't return a value, while value-returning functions return a specific data type.

      • Void functions are useful for tasks like printing output or performing operations without needing to return a result.
      • Value-returning functions are used when you want to calculate a value and use it in other parts of the program.
    • Question 2: Default arguments provide preset values for function parameters.

      • Defining a default argument allows users to call the function with fewer arguments by using the default value for the parameter.
      • Default arguments improve code flexibility.
    • Question 3: Pass-by-value creates a copy of the argument, while pass-by-reference passes the memory address.

      • Pass-by-value doesn't modify the original variable, but pass-by-reference modifies the original variable.
      • Use pass-by-reference for efficiency when you need to modify the original variable inside the function.
    • Question 4: The return statement's role is to send a value back to the calling function.

      • A function can have multiple statements, but it only returns a value when it encounters a return statement.
    • Question 5: Dividing a program into functions improves code readability and maintainability.

      • Well-structured functions represent distinct units of work that make programs easier to understand.
      • Functions promote code reusability.

    Programming Practice Questions 1 & 2

    • Question 1: The isEven function checks if an integer is even or odd and returns true/false.

    • Question 2: The calculateAreaPerimeter function calculates the area and perimeter of a rectangle.

    Programming Practice Questions 3 & 4

    • Question 1: The greetUser function greets the user with a personalized greeting.
    • Question 2: The sumOfDigits function recursively sums the digits of an integer input.

    Programming Practice Question 5

    • Programs for calculating the GCD (Greatest Common Divisor) of two numbers.
      • Separate functions for input, calculation, and output.

    Syntax Error Questions

    • Question 1: Missing the output stream insertion operator << to print message. Missing semicolon.
    • Question 2: Missing semicolon after the line returning the sum.
    • Question 3: The missing parameter y in the multiply(int x, y) function. ( multiply(int x, int y)). Missing return type for main function (void). Return statement must be int for function that return int value.
    • Question 4: The displayMessage function was not correctly called in main function , missing semicolons or other errors in the code.
    • Question 5: Missing return statement, or data type for the variable. (In this case the avg variable should be declared as float and the rest is correctly specified.)

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    Test your knowledge on functions in C++. This quiz covers important concepts like void and value-returning functions, default arguments, and the differences between pass-by-value and pass-by-reference. Enhance your understanding of C++ function usage and best practices.

    More Like This

    C++ Functions and Variable Passing
    18 questions
    C++ Chapter 6 - Functions Quiz
    30 questions

    C++ Chapter 6 - Functions Quiz

    AdmirableMorningGlory3413 avatar
    AdmirableMorningGlory3413
    C++ Functions: Practice and Assessment
    8 questions
    Use Quizgecko on...
    Browser
    Browser