CSC166 ยท 2080 ยท 10 marks
When inline functions may not work? Define and write syntax for default arguments. Write a program to display N number of characters by using default arguments for both parameters. Assume that the function takes two arguments, one character to be printed and other how many times the character to be
When inline functions may not work? Define and write syntax for default arguments. Write a program to display N number of characters by using default arguments for both parameters. Assume that the function takes two arguments, one character to be printed and other how many times the character to be printed respectively.[10]
Quick answer
The compiler may ignore the inline request and treat the function as a normal function in the following situations: Function contains loops - If the inline function contains looping statements such as for, while, or do-while, the compiler will not inline it; Function contains static variables - If the function...
Answer: Inline Functions, Default Arguments, and Program
Part 1: When Inline Functions May NOT Work (3 marks)
The compiler may ignore the inline request and treat the function as a normal function in the following situations:
-
Function contains loops - If the inline function contains looping statements such as
for,while, ordo-while, the compiler will not inline it. -
Function contains static variables - If the function has static local variables declared inside it, it cannot be inlined.
-
Function is recursive - A recursive inline function (a function that calls itself) cannot be expanded inline.
-
Function contains switch or goto statements - Functions containing
switchorgotocontrol statements may not be inlined. -
Function is too large/complex - If the function body is too large, inlining it would increase code size significantly, so the compiler refuses to inline it.
-
Function returns a value and contains multiple return statements - In some cases, multiple return points prevent inlining.
Note: The
inlinekeyword is only a request to the compiler, not a command. The compiler has the final decision.
Part 2: Default Arguments - Definition and Syntax (3 marks)
Definition:
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function does not provide a value for that argument. It allows a function to be called with fewer arguments than it is defined to accept.
Key Rules:
- Default arguments must be specified from right to left (trailing arguments first).
- Once a default value is assigned to a parameter, all parameters to its right must also have default values.
- Default values are specified in the function declaration (prototype).
Syntax:
return_type function_name(datatype param1, datatype param2 = default_value);
Example of syntax with two default arguments:
void display(char ch = '*', int n = 10);
Here, if no arguments are passed, ch defaults to '*' and n defaults to 10.
Part 3: Program to Display N Number of Characters Using Default Arguments (4 marks)
#include <iostream>
using namespace std;
// Function declaration with default arguments
// ch = character to print (default: '*')
// n = number of times to print (default: 3)
void printChar(char ch = '*', int n = 3);
int main()
{
cout << "Using both default arguments (no arguments passed):" << endl;
printChar();
// Output: ***
cout << "\nUsing one argument (only character passed):" << endl;
printChar('#');
// Output: ###
cout << "\nUsing both arguments (no defaults used):" << endl;
printChar('@', 7);
// Output: @@@@@@@
cout << "\nUsing character '$' with 5 times:" << endl;
printChar('$', 5);
// Output: $$$$$
return 0;
}
// Function definition
void printChar(char ch, int n)
{
cout << "Character: " << ch << " | Printed " << n << " time(s): ";
for (int i = 1; i <= n; i++)
{
cout << ch;
}
cout << endl;
}
Output:
Using both default arguments (no arguments passed):
Character: * | Printed 3 time(s): ***
Using one argument (only character passed):
Character: # | Printed 3 time(s): ###
Using both arguments (no defaults used):
Character: @ | Printed 7 time(s): @@@@@@@
Using character '$' with 5 times:
Character: $ | Printed 5 time(s): $$$$$
Explanation of the Program:
| Function Call | ch used | n used | Source |
|---|---|---|---|
printChar() | '*' | 3 | Both defaults used |
printChar('#') | '#' | 3 | n default used |
printChar('@', 7) | '@' | 7 | No defaults used |
printChar('$', 5) | '$' | 5 | No defaults used |
Important Note: Default arguments are written in the function declaration/prototype, not in the definition. The compiler substitutes the default value when the argument is omitted in the function call.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.