Related Topics
C++ Programing
- Question 9
What is function ?
- Answer
In programming, a function is a block of code that performs a specific task. It is a self-contained unit of code that can be called from other parts of the program.
Functions are used to break down a program into smaller, modular pieces of code. By breaking down a program into smaller functions, it becomes easier to read, understand, and maintain. Functions can also be reused in different parts of a program, which can save time and reduce the amount of code that needs to be written.
A function typically has a name, a set of parameters, and a return type. The name of the function is used to call it from other parts of the program. The parameters are values that are passed to the function when it is called. The return type is the type of value that the function returns to the calling code.
Here’s an example of a simple function in C++:
int add(int a, int b) {
return a + b;
}
In this example, the function is named add
, it takes two integer parameters a
and b
, and it returns the sum of a
and b
. This function can be called from other parts of the program like this:
int result = add(2, 3);
This will call the add
function with the parameters 2
and 3
, and the result will be stored in the variable result
. The result
variable will contain the value 5
.
Functions are an essential part of programming, and they are used extensively in many programming languages to organize code and make it more modular and reusable.
- Question 10
Can we write a function without a return value ?
- Answer
Yes, we can write a function without a return value in C++. Such functions are known as “void functions”. Void functions do not return a value and are used to perform a task or operation without returning any result.
Here is an example of a void function:
void printMessage() {
std::cout << "Hello, World!" << std::endl;
}
In this example, the function printMessage
does not have a return type. Instead, it simply prints the message “Hello, World!” to the console. This function can be called from other parts of the program like this:
printMessage();
This will call the printMessage
function, and the message “Hello, World!” will be printed to the console.
Void functions are commonly used for tasks that do not require a return value, such as printing messages, setting values, or modifying data. They are also used as event handlers or callback functions that are called by other parts of the program to perform a specific task.
- Question 11
What is call by value and call by reference ?
- Answer
In C++, there are two ways to pass arguments to a function: call by value and call by reference.
Call by value:
In call by value, a copy of the argument is passed to the function. The function works with the copy of the argument and any changes made to the copy do not affect the original argument.
Here’s an example of call by value:
void square(int x) {
x = x * x;
std::cout << "Inside square function: " << x << std::endl;
}
int main() {
int num = 5;
square(num);
std::cout << "After calling square function: " << num << std::endl;
return 0;
}
In this example, the square
function takes an integer argument x
and squares it. However, since x
is passed by value, any changes made to x
inside the square
function do not affect the value of num
in the main
function.
The output of this program will be:
Inside square function: 25
After calling square function: 5
Call by reference:
In call by reference, a reference to the argument is passed to the function. The function works with the original argument and any changes made to the argument inside the function affect the original argument.
Here’s an example of call by reference:
void square(int& x) {
x = x * x;
std::cout << "Inside square function: " << x << std::endl;
}
int main() {
int num = 5;
square(num);
std::cout << "After calling square function: " << num << std::endl;
return 0;
}
In this example, the square
function takes a reference to an integer argument x
and squares it. Since x
is passed by reference, any changes made to x
inside the square
function affect the value of num
in the main
function.
The output of this program will be:
Inside square function: 25
After calling square function: 25
Call by reference is useful when you want to modify the original argument inside a function. However, it should be used with caution, as it can make the code harder to read and understand.