Podcast
Questions and Answers
What is the primary characteristic of a user-defined function in C++?
What is the primary characteristic of a user-defined function in C++?
- It can only be used within the `main()` function.
- It is defined by the programmer to perform a specific task. (correct)
- It is a pre-built function provided by the C++ standard library.
- It automatically executes at the start of the program.
Which of the following describes a 'void' function in C++?
Which of the following describes a 'void' function in C++?
- A function that is used for exception handling.
- A function that does not return any value. (correct)
- A function that must return an integer value.
- A function that can only accept integer parameters.
In C++, what is the purpose of a function prototype?
In C++, what is the purpose of a function prototype?
- To call a function from within another function.
- To declare a function without defining it, providing information to the compiler. (correct)
- To define the actual code that the function will execute.
- To specify the return type of the `main()` function.
Which of the following statements is true about built-in functions in C++?
Which of the following statements is true about built-in functions in C++?
What happens to variables declared inside a function when the function finishes executing?
What happens to variables declared inside a function when the function finishes executing?
In the following C++ code snippet, what does srand(time(0))
accomplish?
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0));
int randomNumber = rand() % 100;
return 0;
}
In the following C++ code snippet, what does srand(time(0))
accomplish?
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0));
int randomNumber = rand() % 100;
return 0;
}
Which of the following is a correct example of calling a user-defined function named calculateSum
that takes two integer arguments?
Which of the following is a correct example of calling a user-defined function named calculateSum
that takes two integer arguments?
Which statement accurately describes the difference between arguments and parameters in a C++ function?
Which statement accurately describes the difference between arguments and parameters in a C++ function?
Given the following function declaration, what is the correct way to call this function?
void printMessage(std::string message = "Hello");
Given the following function declaration, what is the correct way to call this function?
void printMessage(std::string message = "Hello");
In C++, what is function overloading?
In C++, what is function overloading?
Which of the following describes a function with no arguments and no return value?
Which of the following describes a function with no arguments and no return value?
What is the purpose of the return
statement in a C++ function?
What is the purpose of the return
statement in a C++ function?
When a function is called in C++, how does the program know where to return after the function is executed?
When a function is called in C++, how does the program know where to return after the function is executed?
What happens if you declare a variable with the same name inside a function as a global variable?
What happens if you declare a variable with the same name inside a function as a global variable?
In C++, which of the following is NOT a valid reason for using functions?
In C++, which of the following is NOT a valid reason for using functions?
Given the following C++ code:
#include <iostream>
int add(int a, int b) {
int result = a + b;
return result;
}
int main() {
int x = 5, y = 7;
add(x, y);
return 0;
}
What is the issue with this code?
Given the following C++ code:
#include <iostream>
int add(int a, int b) {
int result = a + b;
return result;
}
int main() {
int x = 5, y = 7;
add(x, y);
return 0;
}
What is the issue with this code?
What is a key benefit of using user-defined functions in C++?
What is a key benefit of using user-defined functions in C++?
In terms of function execution flow, what does it mean for a function to 'call' another function?
In terms of function execution flow, what does it mean for a function to 'call' another function?
Which of the following best describes the concept of 'code modularity' achieved through the use of functions?
Which of the following best describes the concept of 'code modularity' achieved through the use of functions?
Consider the following C++ function definition:
int calculate(int x, int y = 10) {
return x * y;
}
What will be the output of calculate(5)
?
Consider the following C++ function definition:
int calculate(int x, int y = 10) {
return x * y;
}
What will be the output of calculate(5)
?
In C++, what happens if you define two functions with the same name and the exact same parameter list?
In C++, what happens if you define two functions with the same name and the exact same parameter list?
What is the significance of the cmath
library in C++ programming?
What is the significance of the cmath
library in C++ programming?
Given the following code snippet, what is the output?
#include <iostream>
void myFunc(int x) {
x = x + 10;
std::cout << x << std::endl;
}
int main() {
int y = 5;
myFunc(y);
std::cout << y << std::endl;
return 0;
}
Given the following code snippet, what is the output?
#include <iostream>
void myFunc(int x) {
x = x + 10;
std::cout << x << std::endl;
}
int main() {
int y = 5;
myFunc(y);
std::cout << y << std::endl;
return 0;
}
Which of the following is a reason to use a function with arguments?
Which of the following is a reason to use a function with arguments?
In C++, what is the purpose of the cstdlib
library?
In C++, what is the purpose of the cstdlib
library?
How does the use of function prototypes contribute to the compilation process in C++?
How does the use of function prototypes contribute to the compilation process in C++?
Given the following code, what does the function labs do?
#include <iostream>
#include <cstdlib>
int main() {
long num = -70000;
long absoluteValue = labs(num);
std::cout << absoluteValue << std::endl;
return 0;
}
Given the following code, what does the function labs do?
#include <iostream>
#include <cstdlib>
int main() {
long num = -70000;
long absoluteValue = labs(num);
std::cout << absoluteValue << std::endl;
return 0;
}
Consider this scenario: You need to create a function that calculates the area of a circle. Which of the following prototypes is the most appropriate?
Consider this scenario: You need to create a function that calculates the area of a circle. Which of the following prototypes is the most appropriate?
What is the potential issue with the following C++ function?
int myFunction() {
int result;
return result;
}
What is the potential issue with the following C++ function?
int myFunction() {
int result;
return result;
}
In C++, what is the purpose of the exit()
function?
In C++, what is the purpose of the exit()
function?
How do default arguments in C++ functions affect function overloading?
How do default arguments in C++ functions affect function overloading?
What is the primary difference between passing an argument 'by value' versus 'by reference' to a function?
What is the primary difference between passing an argument 'by value' versus 'by reference' to a function?
Consider the following C++ code:
#include <iostream>
void modifyValue(int x) {
x = 100;
}
int main() {
int y = 50;
modifyValue(y);
std::cout << y << std::endl;
return 0;
}
What value will be printed to the console?
Consider the following C++ code:
#include <iostream>
void modifyValue(int x) {
x = 100;
}
int main() {
int y = 50;
modifyValue(y);
std::cout << y << std::endl;
return 0;
}
What value will be printed to the console?
What is the main purpose of the floor()
function in C++?
What is the main purpose of the floor()
function in C++?
What does the term 'function signature' refer to in C++?
What does the term 'function signature' refer to in C++?
Consider the following C++ code:
#include <iostream>
int myFunc(int a, double b) {
return a + b;
}
int main() {
int x = 5;
double y = 2.5;
std::cout << myFunc(x, y) << std::endl;
return 0;
}
What data type will be returned by myFunc
in this code?
Consider the following C++ code:
#include <iostream>
int myFunc(int a, double b) {
return a + b;
}
int main() {
int x = 5;
double y = 2.5;
std::cout << myFunc(x, y) << std::endl;
return 0;
}
What data type will be returned by myFunc
in this code?
Which header file is required to use the sqrt()
function in C++?
Which header file is required to use the sqrt()
function in C++?
When should a function's return type be declared as void
?
When should a function's return type be declared as void
?
Flashcards
What is a function?
What is a function?
A block of code that runs when called.
What are functions?
What are functions?
Functions that may return a value, or perform actions, void functions don't return value.
What are built-in functions?
What are built-in functions?
A function provided within C++ libraries.
What does sqrt() do?
What does sqrt() do?
Signup and view all the flashcards
What does abs() do in C++?
What does abs() do in C++?
Signup and view all the flashcards
What does fabs() do in C++?
What does fabs() do in C++?
Signup and view all the flashcards
What does ceil() do?
What does ceil() do?
Signup and view all the flashcards
What does floor() do?
What does floor() do?
Signup and view all the flashcards
What does exit() do?
What does exit() do?
Signup and view all the flashcards
What does rand() do?
What does rand() do?
Signup and view all the flashcards
What does srand() do?
What does srand() do?
Signup and view all the flashcards
What is user-defined function?
What is user-defined function?
Signup and view all the flashcards
What is main() function?
What is main() function?
Signup and view all the flashcards
What is a function prototype?
What is a function prototype?
Signup and view all the flashcards
What does it mean to call a function?
What does it mean to call a function?
Signup and view all the flashcards
What is a function definition?
What is a function definition?
Signup and view all the flashcards
What is a function with no arguments?
What is a function with no arguments?
Signup and view all the flashcards
What are Void Functions?
What are Void Functions?
Signup and view all the flashcards
What is a function with no arguments but a return value?
What is a function with no arguments but a return value?
Signup and view all the flashcards
What is a local variable?
What is a local variable?
Signup and view all the flashcards
What are parameters?
What are parameters?
Signup and view all the flashcards
What are arguments?
What are arguments?
Signup and view all the flashcards
Passing arguments to function means..?
Passing arguments to function means..?
Signup and view all the flashcards
Return statement means..?
Return statement means..?
Signup and view all the flashcards
What are default arguments?
What are default arguments?
Signup and view all the flashcards
What is a function overloading?
What is a function overloading?
Signup and view all the flashcards
Study Notes
- Module 1 discusses user-defined functions and parameters in C++.
- Lesson 1 focuses on user-defined functions
- Lesson 2 will discuss function parameters
- C++ is used throughout
Objectives
- At the end of this subtopic, the goal is to identify types and create user-defined functions in C++.
Functions Overview
- Functions are procedures, subprograms, and methods
- Functions are blocks of code
- Functions only run when called
- Two types of functions:
- Built-in
- User-defined
- Functions may return a value or perform an action without returning a value
- Functions not returning a value are void functions
Built-in Functions
- C++ provides libraries of predefined functions.
sqrt()
: Calculates the square root of a number; takes a double and returns a double. (ex:sqrt(4.0)
= 2.0)pow()
: Raises a number to a power; takes two doubles and returns a double. (ex:pow(2.0,3.0)
= 8.0)abs()
: Returns the absolute value of an integer; takes an integer and returns an integer. (ex:abs(-7)
= 7)labs()
: Returns the absolute value of a long integer; takes a long and returns a long (ex:labs(-70000)
= 70000)fabs()
: Returns the absolute value of a double; takes a double and returns a double. (ex:fabs(-7.5)
= 7.5)ceil()
: Rounds a number upwards to the nearest integer; takes a double and returns a double. (ex:ceil(3.2)
= 4.0)floor()
: Rounds a number downwards to the nearest integer; takes a double and returns a double. (ex:floor(3.2)
= 3.0)exit()
: Ends program execution; takes an integer and returns voidrand()
: Generates a random number; takes None and returns an intsrand()
: Seeds the random number generator; takes an unsigned int and returns void
User-Defined Functions
- They are defined by the programmer
- A user-defined function groups code to perform a specific task
- The group of code is named with an identifier
- Invoking the function calls executes the defined code in the body of the function
How User-Defined Functions Work
- When running a program, the system calls the
main()
function and executes the code within - When program control reaches a function name in
main()
, it moves to that function - It executes the code within the function
- Once the function is complete, control returns to the
main()
function - It continues with the code after the function call.
User-Defined Function Example
- Code can be written to add integers and display the sum using user defined functions such as
add()
Function Prototype (Declaration)
- In C++, a function prototype declares a function without its body
- In C++, the prototype informs the compiler about a user-defined function
Function Call
- Function can be called to execute its body
- Example:
add(num1, num2)
insidemain()
calls the user-defined function - The function returns an integer in variable add.
Function Definition
- The 'function definition' refers to the actual function: int add(int a, int b) { int add; add = a + b; return add; }
Types of User-Defined Functions
- Function with no argument and no return value
- Function with no argument but return value
- Function with argument but no return value
- Function with argument and return value
Void Functions
- Void functions accomplish tasks and return control to the caller
- Void functions are also called nonvalue-returning functions
- The void function call is a stand-alone statement
Function with No Argument/Return Value Example
- The
prime()
function is called frommain()
without arguments - The
prime()
function takes a number from the user - It checks if the number is prime without returning a value
- The return type of the
prime()
function is void
No Arguments Passed, but a Return Value
- Return type of the prime() function is an int
- The function returns the inputted number from the user back to the
main()
function
Local Variables
- A variable declared inside a function is called a 'local variable'
- It only exists locally within the function
- The local variables are no longer available when the function returns
Parameters and Arguments
- Information can be passed via a parameter
- Parameters are specified after the function name and within parentheses
- Parameters act as variables inside the function
- When a parameter is passed to the function, it is an argument
- A 'parameter' is a variable in a method definition
- The 'arguments' are the data passed to the parameter
- The parameter is a variable that acts as a place holder for the argument
Passing Arguments to a Function
- Arguments refer to the data is that passed to a function when called
Return Statement
- A function can return a single value using the return statement.
Default Arguments in Functions
- Can write code to demonstrate use of default arguments
Arguments Passed, but No Return Value
- Code passes arguments to
prime()
- The program checks if that number is a prime, and prints without a return
Arguments Passed, With a Return Value
- The user is asked for a positive number
- The
prime()
function runs whether the input number is a prime - The return type of
prime()
is anint
, themain()
function returns - The
main()
returns "1" if number is prime - The
main()
returns "0" if is not prime
Function Overloading
- The function has the same name but differing arguments
- Functions of different number or type of parameters,
- Example:
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
- The program calls the
display()
function three times - Function overloading uses different types or numbers of arguments
- The return type are that same, but that is not necessary
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.