Functions improves a program by modularizing it and allowing code to be reused.
3 steps:
- Declare the function - The functions should be declared at the top or it should be defined at the top
- Define the function - The declared function should be defined. Please note the the arguments and the return datatype should match.
- Call the function.
#include <iostream>
using namespace std;
void findSum(double, double); //1. Declare the function
int main() {
double num1, num2;
cout << "Enter first number : ";
cin >> num1;
cout << "Enter second number : ";
cin >> num2;
findSum(num1, num2); //3. Call the function
return 0;
}
void findSum(double i, double j) { //2. Define the function
double result;
result = i + j;
cout << "The sum is : " << result << endl;
}
The following program gives an example of two functions. Both functions return a value.
#include <iostream>
using namespace std;
double findSum(double, double); //1. Declare the function
double findProduct(double, double);
int main() {
double num1, num2, result;
cout << "Enter first number : ";
cin >> num1;
cout << "Enter second number : ";
cin >> num2;
result = findSum(num1, num2); //3. Call the function
cout << "The sum is : " << result << endl;
cout << "The product is : " << findProduct(num1, num2) << endl;
return 0;
}
double findSum(double i, double j) { //2. Define the function
double r;
r = i + j;
return r;
}
double findProduct(double i, double j) { //2. Define the function
double r;
r = i * j;
return r;
}
The following example shows default argument for the power function. Note that the power function declaration specifies a default value for the second argument. The power function will work with one or two arguments.
#include <iostream>
using namespace std;
double findSum(double, double); //1. Declare the function
double findProduct(double, double);
int power(int, int = 2);
int main() {
double num1, num2, result;
cout << "Enter first number : ";
cin >> num1;
cout << "Enter second number : ";
cin >> num2;
result = findSum(num1, num2); //3. Call the function
cout << "The sum is : " << result << endl;
cout << "The product is : " << findProduct(num1, num2) << endl;
cout << num1 << " raised to the power " << num2 << " is : " << power(num1, num2) << endl;
cout << num1 << " raised to the power of 2 is : " << power(num1) << endl;
return 0;
}
double findSum(double i, double j) { //2. Define the function
double r;
r = i + j;
return r;
}
double findProduct(double i, double j) { //2. Define the function
double r;
r = i * j;
return r;
}
int power(int b, int p) { //2. Define the function
return pow(b, p);
}
Another example:
#include <iostream>
using namespace std;
void max(double, double); //Declare Function
double min(double, double); //Declare Function
double sum(double, double);
double average(double, double);
int main(){
double num1, num2, result;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
max(num1, num2); //call the function
result = min(num1, num2);
cout << "Min: " << result << endl;
cout << "Sum: " << sum(num1, num2) << endl;
cout << "Average: " << average(num1, num2) << endl;
return 0;
}
void max(double a, double b) { //Define
if (a > b)
cout << "Max: " << a << endl;
else
cout << "Max: " << b << endl;
}
double min(double a, double b) { //Define
if (a < b)
return a;
else
return b;
}
double sum(double a, double b) { //Define
return a + b;
}
double average(double a, double b) { //Define
return (a + b)/2;
}
Comments
Post a Comment