Fed 2 CH 6 Notes
Fed 2 CH 6 Notes
Fed 2 CH 6 Notes
A1. In pass-by-value, a copy of the argument is made and passed to the function. Any changes made to
the argument inside the function are not reflected outside the function. In pass-by-reference, a
reference to the argument is passed to the function. Any changes made to the argument inside the
function are reflected outside the function.
A2.You can pass an array to a function in C++ using either of the following methods:
A3. The return type of a C++ function is the data type of the value that the function returns. If the
function does not return any value, then the return type is void.
Q4. Can a C++ function return more than one value? If so, how?
A4. No, a C++ function can only return one value. However, you can return a tuple or a struct that
contains multiple values.
A5. Function overloading is a feature in C++ that allows multiple functions to have the same name but
different parameter lists. The compiler determines which function to call based on the number, types,
and order of the arguments passed to the function.
Q6. Can you overload C++ functions based on the return type?
A6. No, you cannot overload C++ functions based on the return type. The return type of the function is
not considered by the compiler when determining which function to call.
Q7. How do you declare a C++ function that takes a variable number of arguments?
A7. You can declare a C++ function that takes a variable number of arguments using the ellipsis (...)
syntax. For example:
// function body
}
Q8. Can you pass a function as an argument to another function in C++? If so, how?
A8. Yes, you can pass a function as an argument to another function in C++ using function pointers. For
example:
// function body
int myOtherFunction(int x) {
// function body
int main() {
myFunction(myOtherFunction);
return 0;
A9. A default argument is a value that is specified for a function parameter in the function declaration. If
no value is provided for that parameter when the function is called, the default value is used.
A10. No, you cannot define a C++ function inside another function. However, you can declare a function
inside another function and define it outside the enclosing function.
To find the sum of two numbers using a function:
#include <iostream>
return a + b;
int main() {
int x, y;
cout << "Sum of " << x << " and " << y << " is " << sum(x, y) << endl;
return 0;
#include <iostream>
if (a > b) {
return a;
} else {
return b;
int main() {
int x, y;
cout << "Maximum of " << x << " and " << y << " is " << max(x, y) << endl;
return 0;}
To check whether a number is prime or not using a function:
#include <iostream>
bool isPrime(int n) {
if (n <= 1) {
return false;
if (n % i == 0) {
return false;
return true;
int main() {
int n;
cin >> n;
if (isPrime(n)) {
} else {
return 0;
}
To find the factorial of a number using a function:
#include <iostream>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n-1);
int main() {
int n;
cin >> n;
cout << "Factorial of " << n << " is " << factorial(n) << endl;
return 0;
}
To find the sum of n natural numbers using a function:
#include <iostream>
int sum(int n) {
if (n == 1) {
return 1;
} else {
return n + sum(n-1);
int main() {
int n;
cin >> n;
cout << "Sum of first " << n << " natural numbers is " << sum(n) << endl;
return 0;
#include <iostream>
#include <cmath>
if (discriminant > 0) {
root1 = -b / (2*a);
cout << "Root 1 and Root 2: " << root1 << endl;
} else {
cout << "Root 1: " << realPart << " + " << imaginaryPart << "i" << endl;
cout << "Root 2: " << realPart << " - " << imaginaryPart << "i" << endl;
int main() {
double a, b, c;
cout << "Enter the coefficients of the quadratic equation: " << endl;
findRoots(a, b, c);
return 0;
#include <iostream>
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main() {
int n;
cout << "Enter the number of elements in the array: " << endl;
cin >> n;
int arr[n];
cout << "Enter the elements of the array: " << endl;
sortArray(arr, n);
return 0;
C++ to check whether a given year is a leap year or not using a function
#include <iostream>
return true;
} else {
return false;
}
int main() {
int year;
if (isLeapYear(year)) {
} else {
cout << year << " is not a leap year." << endl;
return 0;
#include <iostream>
int sum = 0;
sum += arr[i];
int main() {
cout << "Average of array elements is " << avg << endl;
return 0;
}
Program to swap two numbers using a function:
#include <iostream>
num1 = num2;
num2 = temp;
int main() {
cout << "Before swapping: " << num1 << " " << num2 << endl;
swap(num1, num2);
cout << "After swapping: " << num1 << " " << num2 << endl;
return 0;
#include <iostream>
if (num2 == 0)
return num1;
else
}
int main() {
cout << "GCD of " << num1 << " and " << num2 << " is " << result << endl;
return 0;