0% found this document useful (0 votes)
7 views8 pages

Chapter 6 Functions Exercise

Chapter 6 discusses functions in C++, including their types such as built-in, user-defined, and library functions, along with their components like declaration, definition, and call. It also covers default arguments, function overloading, function signatures, variable scopes, and parameters. Additionally, it provides examples of various functions including arithmetic operations, area calculations, and prime number checks.

Uploaded by

arbiixiia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Chapter 6 Functions Exercise

Chapter 6 discusses functions in C++, including their types such as built-in, user-defined, and library functions, along with their components like declaration, definition, and call. It also covers default arguments, function overloading, function signatures, variable scopes, and parameters. Additionally, it provides examples of various functions including arithmetic operations, area calculations, and prime number checks.

Uploaded by

arbiixiia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter 6

Functions
Exercise

1. What is a function? Explain different types of functions used in C++ with examples.

A function is a block of reusable code that performs a specific task. Functions are used to
reduce redundancy, improve readability, and facilitate code maintenance.

Types of Functions in C++:

1. Built-in Functions
These are predefined functions in C++ libraries.
Example: sqrt(), pow(), cout.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
double result = sqrt(16);
cout << "Square root of 16 is: " << result;
return 0;
}

2. User-defined Functions
Functions created by the programmer.
Example:

#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

int main() {
cout << "Sum: " << add(5, 7);
return 0;
}

3. Library Functions
Functions provided by libraries, such as string and cmath.
Example: strlen() from <cstring>.
---

2. Explain function components with examples.

The main components of a function are:

1. Function Declaration (Prototype):


It tells the compiler about the function's name, return type, and parameters.
Example: int sum(int a, int b);

2. Function Definition:
The actual body where the task is performed.
Example:

int sum(int a, int b) {


return a + b;
}

3. Function Call:
Invoking the function to execute.
Example: sum(4, 5);

---

3. Define default arguments. Give their advantages and disadvantages.

Default Arguments are values provided in the function declaration that are used if no
corresponding arguments are passed during the function call.

Example:

#include <iostream>
using namespace std;

int add(int a, int b = 5) {


return a + b;
}

int main() {
cout << "Result: " << add(3) << endl; // b = 5
cout << "Result: " << add(3, 7) << endl; // b = 7
return 0;
}

Advantages:

1. Reduces function overloading.

2. Provides flexibility in function calls.

3. Improves code readability.

Disadvantages:

1. Can cause ambiguity in function overloading.

2. Harder to debug in large codebases.

---

4. What is meant by function overloading? How can a function be overloaded in C++?


Explain with an example.

Function Overloading is when two or more functions have the same name but different
parameters (type, number, or order). The compiler decides which function to call based on
the arguments.

Example:

#include <iostream>
using namespace std;

int area(int side) {


return side * side;
}

int area(int length, int breadth) {


return length * breadth;
}

int main() {
cout << "Area of square: " << area(5) << endl;
cout << "Area of rectangle: " << area(4, 6) << endl;
return 0;
}

---

5. What is a function signature? Explain its different parts.

A function signature is the combination of the function's name and its parameter list. It
uniquely identifies a function.

Example: int add(int a, int b)


Parts:

1. Function Name: add.

2. Parameter Types and Order: int, int.

---

6. Explain the scope of different types of variables used in functions.

1. Local Variables: Declared inside a function and only accessible within it.

2. Global Variables: Declared outside any function and accessible to all functions.

3. Static Variables: Retain their value across multiple calls to the same function.

Example:

#include <iostream>
using namespace std;

void demo() {
static int count = 0;
count++;
cout << "Count: " << count << endl;
}
int main() {
demo();
demo();
return 0;
}

---

7. What are parameters? Explain their types with examples.

Parameters are variables passed to a function to provide input.

Types:

1. Formal Parameters: Declared in the function definition.

2. Actual Parameters: Passed during the function call.

Example:

#include <iostream>
using namespace std;

void greet(string name) { // Formal Parameter


cout << "Hello, " << name << endl;
}

int main() {
greet("Alice"); // Actual Parameter
return 0;
}

---

Lab Activities

---

1. Function to add two integers.

#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

int main() {
cout << "Sum: " << add(4, 5);
return 0;
}

---

2. Function to calculate the arithmetic mean.

#include <iostream>
using namespace std;

float mean(int a, int b, int c) {


return (a + b + c) / 3.0;
}

int main() {
int x, y, z;
cout << "Enter three numbers: ";
cin >> x >> y >> z;
cout << "Mean: " << mean(x, y, z);
return 0;
}

---

3. Function to find the area of a rectangle.

#include <iostream>
using namespace std;

int rectangle(int length, int width) {


return length * width;
}

int main() {
int l, w;
cout << "Enter length and width: ";
cin >> l >> w;
cout << "Area: " << rectangle(l, w);
return 0;
}

---

4. Functions to find the area and perimeter of a square.

#include <iostream>
using namespace std;

int area(int side) {


return side * side;
}

int perimeter(int side) {


return 4 * side;
}

int main() {
int s;
cout << "Enter side length: ";
cin >> s;
cout << "Area: " << area(s) << endl;
cout << "Perimeter: " << perimeter(s) << endl;
return 0;
}

---

5. Function to check if a number is prime.

#include <iostream>
using namespace std;

bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) return false;
}
return true;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num)) {
cout << num << " is a prime number.";
} else {
cout << num << " is a composite number.";
}
return 0;
}

---

6. Function to display the multiplication table.

#include <iostream>
using namespace std;

void table(int num) {


for (int i = 1; i <= 10; i++) {
cout << num << " x " << i << " = " << num * i << endl;
}
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
table(num);
return 0;
}

You might also like