C++ Functions Practice and Assessment PDF
Document Details
Uploaded by InfluentialAtlanta542
Kamran Ahmad Awan
Tags
Summary
This document contains a collection of practice questions and assessments on C++ functions. The questions cover various aspects of functions, including void functions, value-returning functions, default arguments, pass-by-value and pass-by-reference, return statements, and recursive functions. The document is suitable for undergraduate-level students learning functions in C++.
Full Transcript
Functions in C++: Practice and Assessment Kamran Ahmad Awan Kamran Ahmad Awan Functions in C++: Practice and Assessment 1 / 14 Theoretical Questions Theoretical Question 1 What is the difference between a void function and a value-returning fun...
Functions in C++: Practice and Assessment Kamran Ahmad Awan Kamran Ahmad Awan Functions in C++: Practice and Assessment 1 / 14 Theoretical Questions Theoretical Question 1 What is the difference between a void function and a value-returning function? Provide examples of when each might be used. Kamran Ahmad Awan Functions in C++: Practice and Assessment 2 / 14 Theoretical Questions Theoretical Question 2 Explain the purpose of default arguments in a function. How are they defined and used? Kamran Ahmad Awan Functions in C++: Practice and Assessment 3 / 14 Theoretical Questions Theoretical Question 3 Compare and contrast pass-by-value and pass-by-reference with examples. How does each affect the variables in the calling function? Kamran Ahmad Awan Functions in C++: Practice and Assessment 4 / 14 Theoretical Questions Theoretical Question 4 What is the significance of the return statement in a function? Can a function have multiple return statements? Kamran Ahmad Awan Functions in C++: Practice and Assessment 5 / 14 Theoretical Questions Theoretical Question 5 Imagine you are designing a program to calculate the grades of students. How would dividing the program into multiple functions improve its readability and maintainability? Kamran Ahmad Awan Functions in C++: Practice and Assessment 6 / 14 Programming Practice Questions Programming Practice Questions 1 and 2 1 Write a function isEven that takes an integer as input and returns true if the number is even and false otherwise. 2 Write a function calculateAreaPerimeter that takes the length and breadth of a rectangle by reference and calculates the area and perimeter. Demonstrate its usage in main(). Kamran Ahmad Awan Functions in C++: Practice and Assessment 7 / 14 Programming Practice Questions Programming Practice Questions 3 and 4 1 Write a function greetUser that takes a user’s name as a string and a default argument for the greeting (e.g., ”Hello”). The function should print the greeting followed by the name. 2 Implement a recursive function sumOfDigits that takes an integer as input and returns the sum of its digits. Kamran Ahmad Awan Functions in C++: Practice and Assessment 8 / 14 Programming Practice Questions Programming Practice Question 5 Create a program with separate functions for: Taking two numbers as input. Finding the greatest common divisor (GCD) of the two numbers. Displaying the GCD. Kamran Ahmad Awan Functions in C++: Practice and Assessment 9 / 14 Syntax Error Questions Syntax Error Question 1 The following code has syntax errors. Identify and correct them: void printMessage ( string msg ) { cout