C++ User-Defined Functions and Parameters

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Listen to an AI-generated conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

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++?

  • 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?

  • 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++?

<p>They are part of the C++ language and available through libraries. (B)</p>
Signup and view all the answers

What happens to variables declared inside a function when the function finishes executing?

<p>They are destroyed and their memory is deallocated. (A)</p>
Signup and view all the answers

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;
}

<p>It seeds the random number generator with the current time. (D)</p>
Signup and view all the answers

Which of the following is a correct example of calling a user-defined function named calculateSum that takes two integer arguments?

<p>int result = calculateSum(5, 10); (B)</p>
Signup and view all the answers

Which statement accurately describes the difference between arguments and parameters in a C++ function?

<p>Parameters are variables in the function definition, while arguments are the actual values passed to the function. (B)</p>
Signup and view all the answers

Given the following function declaration, what is the correct way to call this function?

void printMessage(std::string message = "Hello");

<p>printMessage(); (D)</p>
Signup and view all the answers

In C++, what is function overloading?

<p>Defining multiple functions with the same name but different parameters. (A)</p>
Signup and view all the answers

Which of the following describes a function with no arguments and no return value?

<p>It can perform an operation but cannot pass data in or out. (A)</p>
Signup and view all the answers

What is the purpose of the return statement in a C++ function?

<p>To send a value back to the calling function. (A)</p>
Signup and view all the answers

When a function is called in C++, how does the program know where to return after the function is executed?

<p>The return address is stored on the call stack before the function is called. (C)</p>
Signup and view all the answers

What happens if you declare a variable with the same name inside a function as a global variable?

<p>The local variable shadows the global variable within the function's scope. (A)</p>
Signup and view all the answers

In C++, which of the following is NOT a valid reason for using functions?

<p>To increase program execution speed. (B)</p>
Signup and view all the answers

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?

<p>The result of <code>add(x, y)</code> is not stored or used. (B)</p>
Signup and view all the answers

What is a key benefit of using user-defined functions in C++?

<p>They make code easier to understand and maintain. (A)</p>
Signup and view all the answers

In terms of function execution flow, what does it mean for a function to 'call' another function?

<p>It means the current function temporarily pauses and executes another function before resuming. (B)</p>
Signup and view all the answers

Which of the following best describes the concept of 'code modularity' achieved through the use of functions?

<p>Organizing code into independent, reusable blocks. (A)</p>
Signup and view all the answers

Consider the following C++ function definition:

int calculate(int x, int y = 10) {
    return x * y;
}

What will be the output of calculate(5)?

<p>50 (D)</p>
Signup and view all the answers

In C++, what happens if you define two functions with the same name and the exact same parameter list?

<p>The compiler will issue an error due to function redefinition. (C)</p>
Signup and view all the answers

What is the significance of the cmath library in C++ programming?

<p>It provides functions for performing common mathematical operations. (B)</p>
Signup and view all the answers

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;
}

<p>15\n5\n (D)</p>
Signup and view all the answers

Which of the following is a reason to use a function with arguments?

<p>To allow the function to operate on different data each time it is called. (C)</p>
Signup and view all the answers

In C++, what is the purpose of the cstdlib library?

<p>It contains functions for general utilities, such as random number generation, memory allocation, and environment control. (A)</p>
Signup and view all the answers

How does the use of function prototypes contribute to the compilation process in C++?

<p>It helps the compiler check for correct function usage before the function is defined. (C)</p>
Signup and view all the answers

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;
}

<p>Returns the absolute value of a long integer. (D)</p>
Signup and view all the answers

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?

<p>float calculateArea(float radius); (B)</p>
Signup and view all the answers

What is the potential issue with the following C++ function?

int myFunction() {
    int result;
    return result;
}

<p>The variable <code>result</code> is not initialized before being returned. (A)</p>
Signup and view all the answers

In C++, what is the purpose of the exit() function?

<p>To terminate the entire program execution. (C)</p>
Signup and view all the answers

How do default arguments in C++ functions affect function overloading?

<p>They can create ambiguity if overloaded functions can be called with the same arguments. (D)</p>
Signup and view all the answers

What is the primary difference between passing an argument 'by value' versus 'by reference' to a function?

<p>Passing by value creates a copy of the argument, while passing by reference allows the function to modify the original argument. (D)</p>
Signup and view all the answers

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?

<p>50 (C)</p>
Signup and view all the answers

What is the main purpose of the floor() function in C++?

<p>To round a number down to the nearest integer. (A)</p>
Signup and view all the answers

What does the term 'function signature' refer to in C++?

<p>The combination of the function name, return type, and parameter list. (A)</p>
Signup and view all the answers

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?

<p><code>int</code> (D)</p>
Signup and view all the answers

Which header file is required to use the sqrt() function in C++?

<p><code>cmath</code> (D)</p>
Signup and view all the answers

When should a function's return type be declared as void?

<p>When the function does not need to return any value. (B)</p>
Signup and view all the answers

Flashcards

What is a function?

A block of code that runs when called.

What are functions?

Functions that may return a value, or perform actions, void functions don't return value.

What are built-in functions?

A function provided within C++ libraries.

What does sqrt() do?

Square root function in C++.

Signup and view all the flashcards

What does abs() do in C++?

Integer absolute value function.

Signup and view all the flashcards

What does fabs() do in C++?

Absolute value of double function.

Signup and view all the flashcards

What does ceil() do?

Rounds a number UP to the nearest integer.

Signup and view all the flashcards

What does floor() do?

Rounds a number DOWN to the nearest integer.

Signup and view all the flashcards

What does exit() do?

Terminates the program.

Signup and view all the flashcards

What does rand() do?

Generates a random number.

Signup and view all the flashcards

What does srand() do?

Sets the seed for random number generation.

Signup and view all the flashcards

What is user-defined function?

Function defined by the programmer.

Signup and view all the flashcards

What is main() function?

Beginning of a C++ program execution.

Signup and view all the flashcards

What is a function prototype?

Declaration of a function's return type, name, and parameters.

Signup and view all the flashcards

What does it mean to call a function?

To start a user-defined function to run the function body code, the function needs to be called.

Signup and view all the flashcards

What is a function definition?

The function's actual implementation.

Signup and view all the flashcards

What is a function with no arguments?

Has no arguments and dont return a value.

Signup and view all the flashcards

What are Void Functions?

Void functions are nonvalue-returning functions.

Signup and view all the flashcards

What is a function with no arguments but a return value?

Has no arguments but do return a value.

Signup and view all the flashcards

What is a local variable?

A variable existing only inside a function.

Signup and view all the flashcards

What are parameters?

Values function receives when its called.

Signup and view all the flashcards

What are arguments?

When a parameter is passed to the function.

Signup and view all the flashcards

Passing arguments to function means..?

Refers to the data passed to the function when its called

Signup and view all the flashcards

Return statement means..?

Returns a single value to the calling program.

Signup and view all the flashcards

What are default arguments?

Arguments that have a default value specified in the function declaration.

Signup and view all the flashcards

What is a function overloading?

Functions with the same name but different parameters

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 void
  • rand(): Generates a random number; takes None and returns an int
  • srand(): 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) inside main() 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 from main() 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 an int, the main() 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.

Quiz Team

Related Documents

More Like This

User-Defined Functions in Programming
5 questions
User-Defined Functions in Programming
10 questions
User Defined Functions and Pointers
30 questions
User Defined Functions in C
13 questions

User Defined Functions in C

HardWorkingSandDune avatar
HardWorkingSandDune
Use Quizgecko on...
Browser
Browser