Podcast
Questions and Answers
Why are functions commonly used in programming?
Why are functions commonly used in programming?
Functions are commonly used to break a problem down into smaller, manageable pieces.
What are the two main types of functions in C++?
What are the two main types of functions in C++?
The two main types of functions in C++ are library functions and user-defined functions.
What is needed to use a library function in a C++ program?
What is needed to use a library function in a C++ program?
To use a library function, you need to include the proper header file that contains the function declaration.
If you wanted to calculate the power of a number, which header file would you need to include?
If you wanted to calculate the power of a number, which header file would you need to include?
What is the purpose of the isalpha()
function, and which header file is needed to use it?
What is the purpose of the isalpha()
function, and which header file is needed to use it?
Describe the general purpose of functions in character manipulation.
Describe the general purpose of functions in character manipulation.
How can you convert a character from lowercase to uppercase in C++?
How can you convert a character from lowercase to uppercase in C++?
What does a user-defined function consist of?
What does a user-defined function consist of?
How does declaring a function as void
impact its behavior?
How does declaring a function as void
impact its behavior?
Explain the purpose of function prototypes.
Explain the purpose of function prototypes.
Where should function prototypes typically be placed in a C++ program?
Where should function prototypes typically be placed in a C++ program?
What must the compiler know about a function before it is called?
What must the compiler know about a function before it is called?
What is the difference between arguments and parameters in the context of functions?
What is the difference between arguments and parameters in the context of functions?
What happens to the value of an argument when it is passed by value to a function?
What happens to the value of an argument when it is passed by value to a function?
Explain why a change to a parameter inside a function does not necessarily affect the original argument passed to it.
Explain why a change to a parameter inside a function does not necessarily affect the original argument passed to it.
How do reference variables differ from regular variables in C++ regarding memory usage?
How do reference variables differ from regular variables in C++ regarding memory usage?
When should reference variables be used as function parameters?
When should reference variables be used as function parameters?
How does passing arguments by reference affect the function's ability to modify the original variables?
How does passing arguments by reference affect the function's ability to modify the original variables?
Describe the difference between passing data by value and passing data by reference.
Describe the difference between passing data by value and passing data by reference.
What is the significance of the ampersand (&
) symbol when declaring a function parameter in C++?
What is the significance of the ampersand (&
) symbol when declaring a function parameter in C++?
You are writing a program to swap the values of two variables. What is the best way to accomplish this with a function?
You are writing a program to swap the values of two variables. What is the best way to accomplish this with a function?
How can you ensure that a function does not modify the value of a variable passed to it?
How can you ensure that a function does not modify the value of a variable passed to it?
You are writing a C++ program, and you want to use the square root function. Which header file must you include?
You are writing a C++ program, and you want to use the square root function. Which header file must you include?
What is the purpose of the #include
directive in C++?
What is the purpose of the #include
directive in C++?
Explain the difference between a function definition and a function call.
Explain the difference between a function definition and a function call.
What happens if you try to use a library function without including its header file?
What happens if you try to use a library function without including its header file?
What does the term “function header” refer to?
What does the term “function header” refer to?
Explain why the main()
function is essential in a C++ program.
Explain why the main()
function is essential in a C++ program.
If a function needs to return a floating-point number, what return type should be used in its definition?
If a function needs to return a floating-point number, what return type should be used in its definition?
Describe how main()
functions should be written according to best practices?
Describe how main()
functions should be written according to best practices?
What is the purpose of the return
statement in a function?
What is the purpose of the return
statement in a function?
Explain the purpose of a 'parameter list' in a function definition.
Explain the purpose of a 'parameter list' in a function definition.
In C++, regarding function calls, what are function prototypes and why are they useful?
In C++, regarding function calls, what are function prototypes and why are they useful?
How can you use the pow()
function in C++ to calculate $5^3$?
How can you use the pow()
function in C++ to calculate $5^3$?
If the function calculateArea(int length, int width)
is defined to calculate the area of a rectangle, provide an example of a function call with length = 10 and width = 5.
If the function calculateArea(int length, int width)
is defined to calculate the area of a rectangle, provide an example of a function call with length = 10 and width = 5.
Define what a function call is in programming.
Define what a function call is in programming.
How should the toupper()
function be used to convert the char variable myChar
to uppercase?
How should the toupper()
function be used to convert the char variable myChar
to uppercase?
Briefly compare and contrast library and user-defined functions.
Briefly compare and contrast library and user-defined functions.
You want to determine if a character that a user inputs is an uppercase letter. Show the code needed to preform this action.
You want to determine if a character that a user inputs is an uppercase letter. Show the code needed to preform this action.
Explain the operation and significance of the code cout << tolower('H');
in C++.
Explain the operation and significance of the code cout << tolower('H');
in C++.
Flashcards
Function
Function
A collection of statements that perform a specific task.
Library Functions
Library Functions
Built-in functions that come with the compiler.
#include
#include
A compiler directive to include header files.
Math Functions
Math Functions
Signup and view all the flashcards
abs(x)
abs(x)
Signup and view all the flashcards
pow(x, y)
pow(x, y)
Signup and view all the flashcards
pow10(x)
pow10(x)
Signup and view all the flashcards
sqrt(x)
sqrt(x)
Signup and view all the flashcards
Character Manipulation Functions
Character Manipulation Functions
Signup and view all the flashcards
isalpha
isalpha
Signup and view all the flashcards
isalnum
isalnum
Signup and view all the flashcards
isdigit
isdigit
Signup and view all the flashcards
islower
islower
Signup and view all the flashcards
isprint
isprint
Signup and view all the flashcards
ispunct
ispunct
Signup and view all the flashcards
isupper
isupper
Signup and view all the flashcards
isspace
isspace
Signup and view all the flashcards
toupper
toupper
Signup and view all the flashcards
tolower
tolower
Signup and view all the flashcards
User-Defined Function
User-Defined Function
Signup and view all the flashcards
int main()
int main()
Signup and view all the flashcards
Function Call
Function Call
Signup and view all the flashcards
Function Definition
Function Definition
Signup and view all the flashcards
Return Type
Return Type
Signup and view all the flashcards
Name
Name
Signup and view all the flashcards
Parameter List
Parameter List
Signup and view all the flashcards
Body
Body
Signup and view all the flashcards
Function Prototypes
Function Prototypes
Signup and view all the flashcards
Function Declaration
Function Declaration
Signup and view all the flashcards
Sending Data to a Function
Sending Data to a Function
Signup and view all the flashcards
Arguments
Arguments
Signup and view all the flashcards
Parameters
Parameters
Signup and view all the flashcards
Pass by Value
Pass by Value
Signup and view all the flashcards
Reference Variables as Parameters
Reference Variables as Parameters
Signup and view all the flashcards
Reference Variable
Reference Variable
Signup and view all the flashcards
Pass by Reference
Pass by Reference
Signup and view all the flashcards
Study Notes
- Functions are a collection of statements performing a specific task.
- Functions are commonly used to break a problem down into manageable pieces.
- There are 2 types of functions in C++: library functions and user-defined functions.
Benefits of Using Functions
- A program divided into smaller problems, each handled by a separate function, is more organized.
- A program with one long, complex function has all the necessary statements to solve a problem.
Library Functions
- Library functions are built-in functions that come with the compiler.
- The source code for library functions does not appear.
- To use a library function, the proper header file is included, and the function name must be known.
- Use the
#include
compiler directive.
Common Library Functions
- Character classification and conversions use the
<cctype>
compiler directive. - Math functions use the
<cmath>
compiler directive. - Data conversions use the
<cstdlib>
compiler directive. - Time functions use the
<ctime>
compiler directive.
Math Functions
- You must include
<cmath>
to use math functions. abs(x)
returns the absolute value of an integer.pow(x,y)
calculates x to the power of y; if x is negative, y must be an integer; if x is zero, y must be a positive integer.pow10(x)
calculates 10 to the power of x.sqrt(x)
calculates the positive square root of x, where x is >=0.
Character Manipulation
- The C++ library provides functions for testing characters.
- To use these functions, include the
cctype
header file. isalpha
will return true if the argument is a letter; otherwise, it returns false.isalnum
will return true if the argument is a letter or a digit; otherwise, false.isdigit
returns true if the argument is a digit 0-9; otherwise, false.islower
returns true if the argument is a lowercase letter; otherwise, false.isprint
returns true if the argument is a printable character; otherwise, false.ispunct
returns true if the argument is a punctuation character; otherwise, false.isupper
returns true if the argument is an uppercase letter; otherwise, false.isspace
returns true if the argument is a whitespace character; otherwise, false.
Character Case Conversion
toupper
converts achar
argument; if it's a lowercase letter, it returns the uppercase equivalent; otherwise, the input is returned unchanged.tolower
converts a char argument; if it's an uppercase letter, it returns the lowercase equivalent; otherwise, the input is returned unchanged.- The
cctype
header file is required for case conversion functions.
User-Defined Functions
- User-defined functions are created by the programmer.
- User-defined functions are commonly used to break a problem down into small, manageable pieces.
int main()
is a function every C++ program possesses.- Ideally, the
main()
function should be short and consist primarily of function calls.
Defining and Calling Functions
- Every function has a function call.
- A function call is a statement that causes a function to execute.
- Every function has a function definition.
- A function definition is the statements that make up a function
Function Definition
- A function definition includes the return type which is the data type of the value that function returns to the part of the program that calls it.
- It includes the name, which is the function name that follows the same rules as variables.
- It includes the parameter list. The parameter list consists of variables containing values passed to the function.
- The body consists of statements that perform the function's task, enclosed in
{}
. function-returntype function-name( parameter-list)
is the general form of a function definition in C++.int main()
is the function header.
Function Return Types
- If a function returns a value, the type of the value must be indicated; for example,
int main()
. - If a function does not return a value, its return type is
void
; for example,void printHeading()
.
Calling a Function
- To call a function, the function name followed by
()
and;
is be used; for example,printHeading();
- When called, the program executes the body of the called function.
- After the function terminates, execution resumes in the calling function at the point of call.
User-Defined Functions - Example
- Main can call any number of functions.
- Functions can call other functions.
- The compiler must know name, return type, number of parameters, and data type of each parameter before a function is called.
Function Prototypes
- A function prototype is a way to notify the compiler about a function before a call to the function.
- Place the function definition before calling the function's definition, or use a function prototype (function declaration).
- A function prototype looks like a function definition without the body.
- Place prototypes near the top of the program.
- If using prototypes, function definitions can be placed in any order in the source file.
Pass By Value
- Passing values into a function occurs at the time of call.
- For example,
c = pow(a, b);
passesa
andb
as function arguments. - Values passed to a function are arguments.
- Variables in a function that hold the values passed as arguments are parameters.
- With pass by value, when an argument os passed to a function its valued is copied into the parameter..
- Changes to the parameter in the function do not affect the value of the argument
Reference Variables as Parameters
- A reference occurs when a function works with the original argument from the function call, not a copy of the argument.
- A reference allows the function to modify values stored in the calling environment.
- A reference provides a way for the function to return more than one value.
- A reference variable is an alias for another variable.
- Reference Variables are defined with an ampersand (
&
); for example,void getDimensions (int&, int&);
- Changes to a reference variable are made to the variable it refers to.
- Reference variables implement passing parameters by reference.
Reference Variables Notes
- Each reference parameter must contain
&
. - There is no significance of the space between type and
&
. &
must be used in both the prototype and header.- The argument passed to a reference parameter must be a variable, not an expression or constant.
- Use when appropriate; do not use when the argument should not be changed by the function or if the function needs to return only one (1) value.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.