Chapter 6 Functions Exercise
Chapter 6 Functions Exercise
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.
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 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. Function Definition:
The actual body where the task is performed.
Example:
3. Function Call:
Invoking the function to execute.
Example: sum(4, 5);
---
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 main() {
cout << "Result: " << add(3) << endl; // b = 5
cout << "Result: " << add(3, 7) << endl; // b = 7
return 0;
}
Advantages:
Disadvantages:
---
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 main() {
cout << "Area of square: " << area(5) << endl;
cout << "Area of rectangle: " << area(4, 6) << endl;
return 0;
}
---
A function signature is the combination of the function's name and its parameter list. It
uniquely identifies a function.
---
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;
}
---
Types:
Example:
#include <iostream>
using namespace std;
int main() {
greet("Alice"); // Actual Parameter
return 0;
}
---
Lab Activities
---
#include <iostream>
using namespace std;
int main() {
cout << "Sum: " << add(4, 5);
return 0;
}
---
#include <iostream>
using namespace std;
int main() {
int x, y, z;
cout << "Enter three numbers: ";
cin >> x >> y >> z;
cout << "Mean: " << mean(x, y, z);
return 0;
}
---
#include <iostream>
using namespace std;
int main() {
int l, w;
cout << "Enter length and width: ";
cin >> l >> w;
cout << "Area: " << rectangle(l, w);
return 0;
}
---
#include <iostream>
using namespace std;
int main() {
int s;
cout << "Enter side length: ";
cin >> s;
cout << "Area: " << area(s) << endl;
cout << "Perimeter: " << perimeter(s) << endl;
return 0;
}
---
#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;
}
---
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
table(num);
return 0;
}