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

Functions

Uploaded by

priyankaroy8638
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Functions

Uploaded by

priyankaroy8638
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

// program to print a text

#include <iostream>
using namespace std;

// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}

int main() {

int num1 = 5;
double num2 = 5.5;

// calling the function


displayNum(num1, num2);

return 0;
}
// program to add two numbers using a function

#include <iostream>

using namespace std;

// declaring a function
int add(int a, int b) {
return (a + b);
}

int main() {

int sum;

// calling the function and storing


// the returned value in sum
sum = add(100, 78);

cout << "100 + 78 = " << sum << endl;

return 0;
}
Default Arguments
A default argument is a value provided in a function
declaration that is automatically assigned by the compiler if
the calling function doesn’t provide a value for the
argument. In case any value is passed, the default value is
overridden.
// CPP Program to demonstrate Default Arguments
#include <iostream>
using namespace std;

// A function with default arguments,


// it can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z = 0, int w = 0) //assigning default values to z,w as 0
{
return (x + y + z + w);
}

// Driver Code
int main()
{
// Statement 1
cout << sum(10, 15) << endl;

// Statement 2
cout << sum(10, 15, 25) << endl;

// Statement 3
cout << sum(10, 15, 25, 30) << endl;
return 0;}
C++ program allow a user to input principal amount, time, and interest
rate, and then calculate and display the simple interest using a
customizable interest rate with a default value of 5.75%?
#include <iostream>

using namespace std;

// Function to calculate simple interest with default interest rate


double calculateSimpleInterest(double principal, double time, double rate = 5.75) {
return (principal * rate * time) / 100;
}
By default the rate
of interest is set to
5.75
Call by value and Call by reference
There are two ways to pass the data to a function in the C++ language:
1.call by value
2.call by reference
These parameters are of two types.
• Formal parameters: These are those parameters that are passed
during the declaration/ definition of a function.
• Actual parameters: These are those parameters that are passed while
calling the function.
Formal parameter

Actual parameter
Call by Value Method
Call by Reference Method
Call by value Call by reference

While calling a function, we pass the values of While calling a function, we pass the values of
variables to it. Such functions are known as “Call variables to it. Such functions are known as “Call
By Values”. While calling a function, instead By Values”. While calling a function, instead
of passing the values of variables, we pass the of passing the values of variables, we pass the
address of variables(location of variables) to the address of variables(location of variables) to the
function known as “Call By References. function known as “Call By References.

Original values remain unchanged outside the Original values can be modified inside the function.
function.

Requires additional memory for storing copies of More memory-efficient as it directly manipulates
parameters. original data.

This method is preferred when we have to pass some This method is preferred when we have to pass a large
small values that should not change. amount of data to the function.

Actual arguments remain safe as they cannot be Actual arguments are not Safe. They can be
modified accidentally. accidentally modified, so you need to handle
arguments operations carefully.
Write a C++ program to swap 2 values by writing
a function that uses call by reference technique.
#include <iostream>
using namespace std;
// Function prototype for swapping using call by reference
void swapNumbers(int &a, int &b);

int main() {
int num1 = 5;
int num2 = 10;

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

// Calling the function to swap numbers


swapNumbers(num1, num2);
Function overloading
• Function overloading in C++ is a feature that allows you to define
multiple functions with the same name in the same scope but with
different parameters.
• The compiler determines which version of the function to call based
on the number and types of arguments provided during the function
call.
• Function overloading makes code more readable and flexible by
providing a way to use the same function name for operations that
conceptually do the same thing but may vary in terms of data types or
the number of parameters.
Write a C++ program to overload
function for computing the area
triangle, circle and square
#include <iostream>
#include <cmath>
const double PI = 3.14159265358979323846;
using namespace std;
// Function to compute the area of a triangle
double area(double base, double height) {
return 0.5 * base * height;
}
// Function to compute the area of a circle
double area(double radius) {
return PI * pow(radius, 2);
}
// Function to compute the area of a square
double area(int side) {
return pow(side, 2);
}
write a C++ program to find the largest of the three numbers using control statements.
#include <iostream>
using namespace std;

int main() {
// Input three numbers
double num1, num2, num3;

cout << "Enter three numbers: ";


cin >> num1 >> num2 >> num3;

// Using if-else statements to find the largest number


if (num1 >= num2 && num1 >= num3) {
cout << "The largest number is: " << num1 << endl;
} else if (num2 >= num1 && num2 >= num3) {
cout << "The largest number is: " << num2 << endl;
} else {
cout << "The largest number is: " << num3 << endl;
}

return 0;
}

You might also like