Fed 2 CH 6 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Q1. What is the difference between pass-by-value and pass-by-reference in C++ function arguments?

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.

Q2. How do you pass an array to a function in C++?

A2.You can pass an array to a function in C++ using either of the following methods:

Pass the array as a pointer to the first element of the array.

Pass the array as a reference.

Q3. What is the return type of a C++ function?

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.

Q5. What is function overloading in C++?

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:

void myFunction(int a, ...) {

// 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:

void myFunction(int (*func)(int)) {

// function body

int myOtherFunction(int x) {

// function body

int main() {

myFunction(myOtherFunction);

return 0;

Q9. What is a default argument in C++ function?

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.

Q10. Can you define a C++ function inside another function?

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>

using namespace std;

int sum(int a, int b) {

return a + b;

int main() {

int x, y;

cout << "Enter two integers: ";

cin >> x >> y;

cout << "Sum of " << x << " and " << y << " is " << sum(x, y) << endl;

return 0;

To find the maximum of two numbers using a function:

#include <iostream>

using namespace std;

int max(int a, int b) {

if (a > b) {

return a;

} else {

return b;

int main() {

int x, y;

cout << "Enter two integers: ";

cin >> 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>

using namespace std;

bool isPrime(int n) {

if (n <= 1) {

return false;

for (int i = 2; i*i <= n; i++) {

if (n % i == 0) {

return false;

return true;

int main() {

int n;

cout << "Enter a positive integer: ";

cin >> n;

if (isPrime(n)) {

cout << n << " is a prime number" << endl;

} else {

cout << n << " is not a prime number" << endl;

return 0;

}
To find the factorial of a number using a function:

#include <iostream>

using namespace std;

int factorial(int n) {

if (n == 0) {

return 1;

} else {

return n * factorial(n-1);

int main() {

int n;

cout << "Enter a positive integer: ";

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>

using namespace std;

int sum(int n) {

if (n == 1) {

return 1;

} else {

return n + sum(n-1);

int main() {

int n;

cout << "Enter a positive integer: ";

cin >> n;

cout << "Sum of first " << n << " natural numbers is " << sum(n) << endl;

return 0;

Program to find the roots of a quadratic equation using a function:

#include <iostream>

#include <cmath>

using namespace std;

void findRoots(double a, double b, double c) {

double discriminant = b*b - 4*a*c;

double root1, root2;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2*a);

root2 = (-b - sqrt(discriminant)) / (2*a);

cout << "Root 1: " << root1 << endl;

cout << "Root 2: " << root2 << endl;


} else if (discriminant == 0) {

root1 = -b / (2*a);

cout << "Root 1 and Root 2: " << root1 << endl;

} else {

double realPart = -b / (2*a);

double imaginaryPart = sqrt(-discriminant) / (2*a);

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;

cin >> a >> b >> c;

findRoots(a, b, c);

return 0;

Program to sort an array in ascending order using a function:

#include <iostream>

using namespace std;

void sortArray(int arr[], int n) {

int temp;

for (int i = 0; i < n; i++) {

for (int j = i+1; j < n; j++) {

if (arr[i] > arr[j]) {

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;

for (int i = 0; i < n; i++) {

cin >> arr[i];

sortArray(arr, n);

cout << "Sorted array in ascending order: " << endl;

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

return 0;

C++ to check whether a given year is a leap year or not using a function

#include <iostream>

using namespace std;

bool isLeapYear(int year) {

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

return true;

} else {

return false;

}
int main() {

int year;

cout << "Enter the year: ";

cin >> year;

if (isLeapYear(year)) {

cout << year << " is a leap year." << endl;

} else {

cout << year << " is not a leap year." << endl;

return 0;

Program to calculate the average of an array of numbers using a function:

#include <iostream>

using namespace std;

double avgArray(int arr[], int size) {

int sum = 0;

for (int i = 0; i < size; i++) {

sum += arr[i];

return (double)sum / size;

int main() {

int arr[] = { 5, 10, 15, 20, 25 };

int size = sizeof(arr) / sizeof(arr[0]);

double avg = avgArray(arr, size);

cout << "Average of array elements is " << avg << endl;

return 0;

}
Program to swap two numbers using a function:

#include <iostream>

using namespace std;

void swap(int& num1, int& num2) {

int temp = num1;

num1 = num2;

num2 = temp;

int main() {

int num1 = 10;

int num2 = 20;

cout << "Before swapping: " << num1 << " " << num2 << endl;

swap(num1, num2);

cout << "After swapping: " << num1 << " " << num2 << endl;

return 0;

Program to find the GCD of two numbers using a function:

#include <iostream>

using namespace std;

int gcd(int num1, int num2) {

if (num2 == 0)

return num1;

else

return gcd(num2, num1 % num2);

}
int main() {

int num1, num2;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

int result = gcd(num1, num2);

cout << "GCD of " << num1 << " and " << num2 << " is " << result << endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy