0% found this document useful (0 votes)
3 views

FUNCTION Examples @cppfocus

Uploaded by

ifaatemesgen
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)
3 views

FUNCTION Examples @cppfocus

Uploaded by

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

@cppfocus

FUNCTION CONCEPTS:

1. Write a C++ program with separate functions for add, subtract, multiply, and divide. Call
them from main() using user input.
#include <iostream>
using namespace std;
float add(float a, float b) {
return a + b;
}
float subtract(float a, float b) {
return a - b;
}
float multiply(float a, float b) {
return a * b;
}
float divide(float a, float b) {
if (b != 0)
return a / b;
else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
int main() {
float num1, num2;
char op;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
switch(op) {
case '+':
cout << "Result: " << add(num1, num2) << endl;
break;
case '-':
cout << "Result: " << subtract(num1, num2) << endl;
break;
case '*':
cout << "Result: " << multiply(num1, num2) << endl;
@cppfocus

break;
case '/':
cout << "Result: " << divide(num1, num2) << endl;
break;
default:
cout << "Invalid operator!" << endl;
}
return 0;
}
2. Create a modular calculator using function overloading to handle both integers and
doubles.
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int divide(int a, int b) {
if (b != 0)
return a / b;
else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
double add(double a, double b) {
return a + b;
}
double subtract(double a, double b) {
return a - b;
}
double multiply(double a, double b) {
return a * b;
}
double divide(double a, double b) {
if (b != 0)
@cppfocus

return a / b;
else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
int main() {
int int1, int2;
double double1, double2;
char op;
cout << "Enter first integer: ";
cin >> int1;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter second integer: ";
cin >> int2;
switch(op) {
case '+':
cout << "Result: " << add(int1, int2) << endl;
break;
case '-':
cout << "Result: " << subtract(int1, int2) << endl;
break;
case '*':
cout << "Result: " << multiply(int1, int2) << endl;
break;
case '/':
cout << "Result: " << divide(int1, int2) << endl;
break;
default:
cout << "Invalid operator!" << endl;
}
cout << "Enter first double: ";
cin >> double1;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter second double: ";
cin >> double2;
switch(op) {
case '+':
cout << "Result: " << add(double1, double2) << endl;
@cppfocus

break;
case '-':
cout << "Result: " << subtract(double1, double2) << endl;
break;
case '*':
cout << "Result: " << multiply(double1, double2) << endl;
break;
case '/':
cout << "Result: " << divide(double1, double2) << endl;
break;
default:
cout << "Invalid operator!" << endl;
}
return 0;
}
3. Create a program that swaps two integers using both call-by-value and call-by-
reference. Show the difference.
#include <iostream>
using namespace std;
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
cout << "Before swap (call by value): x = " << x << ", y = " << y << endl;
swapByValue(x, y);
cout << "After swap (call by value): x = " << x << ", y = " << y << endl;
cout << "Before swap (call by reference): x = " << x << ", y = " << y << endl;
swapByReference(x, y);
cout << "After swap (call by reference): x = " << x << ", y = " << y << endl;
return 0;
}
@cppfocus

4. Pass two strings to a function: one by value and one by reference. Modify both inside
the function and show the output.
#include <iostream>
using namespace std;
void modifyStrings(string str1, string &str2) {
str1 = "Modified by value";
str2 = "Modified by reference";
}
int main() {
string str1 = "Original by value";
string str2 = "Original by reference";
cout << "Before modification: str1 = " << str1 << ", str2 = " << str2 << endl;
modifyStrings(str1, str2);
cout << "After modification: str1 = " << str1 << ", str2 = " << str2 << endl;
return 0;
}
5. Implement function overloading for calculating the area of a circle, square, and
rectangle.
#include <iostream>
using namespace std;
float area(float radius) {
return 3.14f * radius * radius;
}
int area(int side) {
return side * side;
}
int area(int length, int width) {
return length * width;
}
int main() {
float r;
int s, l, w;
cout << "Enter radius of circle: ";
cin >> r;
cout << "Area of circle: " << area(r) << endl;
cout << "Enter side of square: ";
cin >> s;
cout << "Area of square: " << area(s) << endl;
@cppfocus

cout << "Enter length and width of rectangle: ";


cin >> l >> w;
cout << "Area of rectangle: " << area(l, w) << endl;
return 0;
}

6. Write a C++ function factorial() that computes the factorial of a non-negative integer n
using recursion. The function should handle edge cases (e.g., 0! = 1) and return the
result.
#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 non-negative integer: ";
cin >> n;
if (n < 0)
cout << "Invalid input! Please enter a non-negative integer." << endl;
else
cout << "Factorial of " << n << " is " << factorial(n) << endl;
return 0;
}
7. Create an inline function for squaring a number and call it multiple times inside a loop.
#include <iostream>
using namespace std;
inline int square(int x) {
return x * x;
}
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Square of " << i << " is " << square(i) << endl;
}
@cppfocus

return 0;
}
8. Write a recursive function to calculate the factorial of a number.
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
cout << "Enter a non-negative integer: ";
cin >> num;
if (num < 0)
cout << "Invalid input!" << endl;
else
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}

You might also like