Podcast
Questions and Answers
Which of the following best describes a function?
Which of the following best describes a function?
- A single value stored in memory.
- A collection of statements grouped to perform an operation. (correct)
- A data structure used to store related information.
- A collection of variables of the same type.
What constitutes the function signature in C++?
What constitutes the function signature in C++?
- The function name only.
- The combination of the function name and parameter list. (correct)
- The return type of the function.
- The parameter list only.
What is another term of actual parameter?
What is another term of actual parameter?
- Function Signature
- Formal Parameter
- Argument (correct)
- Return Type
In the provided code snippet, what does '4' represent?
int max(int num1, int num2) // 4
In the provided code snippet, what does '4' represent?
int max(int num1, int num2) // 4
What is the purpose of the return
statement in a function?
What is the purpose of the return
statement in a function?
Consider the following C++ code. What will be the output?
int main() {
int i = 5;
int j = 2;
int k = max(i, j);
std::cout << "The maximum between " << i << " and " << j << " is " << k;
return 0;
}
int max(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
Consider the following C++ code. What will be the output?
int main() {
int i = 5;
int j = 2;
int k = max(i, j);
std::cout << "The maximum between " << i << " and " << j << " is " << k;
return 0;
}
int max(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
What is the significance of a 'void' return type in a function definition?
What is the significance of a 'void' return type in a function definition?
In C++, if a function is defined to take two double
arguments but is called with an int
and a double
, what typically happens?
In C++, if a function is defined to take two double
arguments but is called with an int
and a double
, what typically happens?
What is the potential issue with the following function call, given the function definition double ApplyDiscount(double amount, double percentage)
?
ApplyDiscount(storeRate, finalAmount);
What is the potential issue with the following function call, given the function definition double ApplyDiscount(double amount, double percentage)
?
ApplyDiscount(storeRate, finalAmount);
What is the main advantage of breaking a program into smaller functions?
What is the main advantage of breaking a program into smaller functions?
What is function overloading?
What is function overloading?
Consider the following code. What will be the output?
#include <iostream>
void print(int num) {
std::cout << "Integer number: " << num << std::endl;
}
void print(double num) {
std::cout << "Double number: " << num << std::endl;
}
int main() {
int x = 10;
double y = 5.5;
print(x);
print(y);
return 0;
}
Consider the following code. What will be the output?
#include <iostream>
void print(int num) {
std::cout << "Integer number: " << num << std::endl;
}
void print(double num) {
std::cout << "Double number: " << num << std::endl;
}
int main() {
int x = 10;
double y = 5.5;
print(x);
print(y);
return 0;
}
Why might you use function prototypes in C++?
Why might you use function prototypes in C++?
What is a function prototype?
What is a function prototype?
What is the purpose of default arguments in C++ functions?
What is the purpose of default arguments in C++ functions?
Which of the following is the correct syntax for declaring a default argument in a C++ function?
Which of the following is the correct syntax for declaring a default argument in a C++ function?
Why should default arguments be the last parameters in a function's parameter list?
Why should default arguments be the last parameters in a function's parameter list?
What is a local variable?
What is a local variable?
What determines the scope of a variable?
What determines the scope of a variable?
If a local variable with the same name is declared in two non-nesting blocks within the same function, is this valid in C++?
If a local variable with the same name is declared in two non-nesting blocks within the same function, is this valid in C++?
What is the default initial value of a global variable in C++ if it is not explicitly initialized?
What is the default initial value of a global variable in C++ if it is not explicitly initialized?
Consider the following code snippet:
void myFunction() {
int localVar = 5;
}
What is the scope of localVar
?
Consider the following code snippet:
void myFunction() {
int localVar = 5;
}
What is the scope of localVar
?
Consider the following code snippet:
int globalVariable = 5;
void myFunction() {
// Code here
}
int main() {
// Code here
return 0;
}
What is the scope of globalVariable
?
Consider the following code snippet:
int globalVariable = 5;
void myFunction() {
// Code here
}
int main() {
// Code here
return 0;
}
What is the scope of globalVariable
?
What is the purpose of a static
local variable?
What is the purpose of a static
local variable?
Consider the following C++ code. What will be the value of staticVar
after the program executes?
#include <iostream>
void myFunction() {
static int staticVar = 0;
staticVar++;
std::cout << "StaticVar inside myFunction(): " << staticVar << std::endl;
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
Consider the following C++ code. What will be the value of staticVar
after the program executes?
#include <iostream>
void myFunction() {
static int staticVar = 0;
staticVar++;
std::cout << "StaticVar inside myFunction(): " << staticVar << std::endl;
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
In pass-by-value, what happens to the actual parameter when the formal parameter is modified inside the function?
In pass-by-value, what happens to the actual parameter when the formal parameter is modified inside the function?
In pass-by-value, what are the relationship between the formal parameter and the actual parameter?
In pass-by-value, what are the relationship between the formal parameter and the actual parameter?
Consider the following code. Which of the following describes num
's behaviour?
#include <iostream>
using namespace std;
void incrementByValue(int num) {
num++;
cout << "Inside function - Value of num: " << num << endl;
}
int main() {
int num = 5;
incrementByValue(num);
cout << "After function call - Value of num: " << num << endl;
return 0;
}
Consider the following code. Which of the following describes num
's behaviour?
#include <iostream>
using namespace std;
void incrementByValue(int num) {
num++;
cout << "Inside function - Value of num: " << num << endl;
}
int main() {
int num = 5;
incrementByValue(num);
cout << "After function call - Value of num: " << num << endl;
return 0;
}
What is a reference variable in C++?
What is a reference variable in C++?
What is the purpose of using a reference variable as a function parameter?
What is the purpose of using a reference variable as a function parameter?
Consider the following code snippet:
void incrementByReference(int &num) {
num++;
}
int main() {
int number = 5;
incrementByReference(number);
std::cout << number << std::endl;
return 0;
}
What will be the output?
Consider the following code snippet:
void incrementByReference(int &num) {
num++;
}
int main() {
int number = 5;
incrementByReference(number);
std::cout << number << std::endl;
return 0;
}
What will be the output?
In which of the following scenarios is it most appropriate to use pass-by-reference instead of pass-by-value?
In which of the following scenarios is it most appropriate to use pass-by-reference instead of pass-by-value?
What is the code difference between pass-by-value and pass-by-reference?
What is the code difference between pass-by-value and pass-by-reference?
What is the benefit of using const
with reference parameters?
What is the benefit of using const
with reference parameters?
Given the declaration int max(const int& num1, const int& num2);
, what does const int&
signify?
Given the declaration int max(const int& num1, const int& num2);
, what does const int&
signify?
Flashcards
Function
Function
A collection of statements that are grouped together to perform an operation.
Function Signature
Function Signature
The combination of the function name and the parameter list.
Formal Parameters
Formal Parameters
Variables defined in the function header (definition).
Actual Parameter/Argument
Actual Parameter/Argument
Signup and view all the flashcards
Void Function
Void Function
Signup and view all the flashcards
Parameter Order Association
Parameter Order Association
Signup and view all the flashcards
Global Variables
Global Variables
Signup and view all the flashcards
Static Local Variables
Static Local Variables
Signup and view all the flashcards
Passing by Value
Passing by Value
Signup and view all the flashcards
Reference Variable
Reference Variable
Signup and view all the flashcards
Passing by Reference
Passing by Reference
Signup and view all the flashcards
Function Overloading
Function Overloading
Signup and view all the flashcards
Function Prototype
Function Prototype
Signup and view all the flashcards
Default Arguments
Default Arguments
Signup and view all the flashcards
Local Variable
Local Variable
Signup and view all the flashcards
Scope of Variable
Scope of Variable
Signup and view all the flashcards
Study Notes
- Functions are a collection of statements grouped together to perform an operation.
Defining a Function
- A function's signature is its name combined with its parameter list.
- Formal parameters are the variables defined in the function header.
- When a function is invoked, a value is passed to the parameter; this value is the actual parameter, also known as an argument.
Function Anatomy
- The general structure of a function includes a return type, function name, and a parameter list within parentheses, followed by the function body enclosed in curly braces.
- The function body contains code statements and a return statement to provide a value back to the caller.
Calling Functions
- To use a function, you must define it first (specify the return type, name, and formal parameters).
- When calling the function you “invoke” it, and you provide a value for each parameter (arguments).
- A function call is, for instance,
max(x, y)
wherex
andy
are the actual parameters/arguments - The values of variables in the
main()
function are passed to themax()
function, and the return value is used inmain()
.
Trace Function Invocation
- When the
max()
function is called, the values of 'i' and 'j' frommain()
are passed to 'num1' and 'num2' inmax()
, respectively.
Call Stacks
- A call stack illustrates the memory allocation during function calls, showing the flow of variable declaration and value assignments for both the calling and called functions.
- The stack shows which function has been invoked.
Void Function
- A function that doesn’t return a value (void function), is declared with the
void
keyword as its return type. - There is no return value given, only a
return
; statement is given.
Passing Parameters to Functions
- When passing parameters, the order of the parameters passed to a function matters.
- The
parameter order association
should be considered (pass parameters in the order they are declared to ensure correct assignment).
Passing Parameters by Value
- A function can be defined to be passed by value or by reference.
- With pass by value, also known as call by value, changes made in the function to the parameters do not affect the variables the arguments are derived from
int max(int num1, int num2)
- parameters passed by value
Modularization and C Program Layout
- Functions are useful for modularizing code, breaking tasks down into subtasks.
Function Overloading
- C++ allows multiple functions to have the same name if their parameters differ (function overloading).
- Overloading the
max
function allows it to work withint
ordouble
data types through separate function definitions.
Function Prototypes
- Before a function is called, it must be declared.
- One can ensure a function is declared by: placing the function declaration before all function calls, or declare a function prototype before the function is called.
- A function prototype is a function declaration without implementation (the implementation can be given later).
Default Arguments
- C++ allows definition of functions with default argument values.
- Default values are used if no arguments are provided when the function is called.
- Default arguments should be the last parameters in the parameter list.
Scope of Variables
- A local variable is defined inside a function.
Global Variables
- Global variables are declared (defined) outside all functions, making them accessible to any function in that file.
- Local variables don't have default values, so they must be initialized before use.
- Global variables are defaulted to zero upon declaration.
Static Local Variables
- After a function completes its execution, all its local variables are destroyed.
- C++ allows declaring static local variables.
- It is desirable to retain the value stored in local variables so that they can be used in the next call.
- Static local variables are permanently allocated in the memory for the lifetime of the program.
- The keyword
static
is used to declare a static local variable.
Passing Parameters to Functions (Pass by Value vs. Pass by Reference)
- When passing parameters to functions, two main methods exist: pass by value and pass by reference.
Pass By Value Summary
- In pass-by-value, the actual parameter and its formal parameter are independent variables.
Note About Literals
- A literal value is a value assigned to the variable such as
(1,2,3...)
forint
or(1.5, 0.5, 3.5...)
forfloat
ordouble
or other types. - The value of the variable is the literal value that assigned to the parameter.
Reference Variable
- C++ provides a special type of variable called a reference variable.
- They can be used as a function parameter to reference the original variable.
- A reference variable is an alias for another variable.
Passing Parameters by Reference
- You can use a reference variable as a parameter in a function and pass a regular variable to invoke the function.
- When you change the value through the reference variable, the original value is actually changed.
- Using
&
in the function parameter defines it to be passed by reference. int max(const int& num1, const int& num2)
: parameters passed by reference
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.