Oriented Programming
Oriented Programming
Syntax of Function
Example:
C++
// C++ Program to demonstrate working of a function
#include <iostream>
using namespace std;
Output
m is 20
Time complexity: O(1)
Space complexity: O(1)
Why Do We Need Functions?
Functions help us in reducing code redundancy. If
functionality is performed at multiple places in software,
then rather than writing the same code, again and again,
we create a function and call it everywhere. This also
helps in maintenance as we have to make changes in only
one place if we make changes to the functionality in
future.
Functions make code modular. Consider a big file having
many lines of code. It becomes really simple to read and
use the code, if the code is divided into functions.
Functions provide abstraction. For example, we can use
library functions without worrying about their internal
work.
Function Declaration
A function declaration tells the compiler about the number of
parameters, data types of parameters, and returns type of
function. Writing parameter names in the function declaration is
optional but it is necessary to put them in the definition. Below is
an example of function declarations. (parameter names are not
present in the below declarations)
Function Declaration
Example:
C++
// C++ Program to show function that takes
// two integers as parameters and returns
// an integer
int max(int, int);
Types of Functions
Types of Function in C++
void fun(int x)
{
// definition of
// function
x = 30;
}
int main()
{
int x = 20;
fun(x);
cout << "x = " << x;
return 0;
}
Output
x = 20
Time complexity: O(1)
Space complexity: O(1)
Functions Using Pointers
The function fun() expects a pointer ptr to an integer (or an
address of an integer). It modifies the value at the address ptr.
The dereference operator * is used to access the value at an
address. In the statement ‘*ptr = 30’, the value at address ptr is
changed to 30. The address operator & is used to get the address
of a variable of any data type. In the function call statement
‘fun(&x)’, the address of x is passed so that x can be modified
using its address.
C++
// C++ Program to demonstrate working of
// function using pointers
#include <iostream>
using namespace std;
int main()
{
int x = 20;
fun(&x);
cout << "x = " << x;
return 0;
}
Output
x = 30
Time complexity: O(1)
Space complexity: O(1)
passing string as an argument:
In c++ we can pass string as as argument in various way
1. Pass by Value
2. Pass by Reference
3. Pass by Pointer
1. Pass by Value
In this when you are passing a values as string it will make a copy
of a string values.
C++
#include <iostream>
#include <string>
int main() {
std::string myString = "Hello, GFG!";
printString(myString);
return 0;
}
Output
Hello, GFG!
2. Pass by Reference
This can be done by using ‘&’ operator
C++
#include <iostream>
#include <string>
int main() {
std::string myString = "welcome to gfg";
printString(myString);
return 0;
}
Output
welcome to gfg
3. Pass by Pointer
This can be done by using * operator
C++
#include <iostream>
#include <string>
void printString(const std::string* str) { // Note the 'const' to
prevent modification
std::cout << *str << std::endl;
}
int main() {
std::string myString = "This is Pss by pointer";
printString(&myString);
return 0;
}
Output
This is Pss by pointer
std::string getGreeting() {
return "This is C++";
}
int main() {
std::string greeting = getGreeting();
std::cout << greeting << std::endl;
return 0;
}
Output
This is C++
int main() {
int size = 10;
int* myArray = createArray(size); // Function returns a pointer to
the array
Output
0 10 20 30 40 50 60 70 80 90
int main() {
// Pass the callback function to performAction
performAction(myCallback);
return 0;
}
Output
Performing some action...
Callback function
// Function declarations
int add(int a, int b) {
return a + b;
}
int main() {
// Declare and initialize an array of function pointers
int (*funcArray[3])(int, int) = { add, subtract, multiply };
return 0;
}
Output
Add: 5
Subtract: -1
Multiply: 6
Difference between call by value and call by
reference in C++
Call by value Call by reference
Actual and formal arguments will be Actual and formal arguments will be
created at created at
different memory location same memory location.
Main Function
The main function is a special function. Every C++ program must
contain a function named main. It serves as the entry point for
the program. The computer will start running the code from the
beginning of the main function.
Types of Main Functions
1. Without parameters:
CPP
// Without Parameters
int main() { ... return 0; }
2. With parameters:
CPP
// With Parameters
int main(int argc, char* const argv[]) { ... return 0; }
The reason for having the parameter option for the main function
is to allow input from the command line. When you use the main
function with parameters, it saves every group of characters
(separated by a space) after the program name as elements in an
array named argv.
Since the main function has the return type of int, the
programmer must always have a return statement in the code.
The number that is returned is used to inform the calling program
what the result of the program’s execution was. Returning 0
signals that there were no problems.
C++ Recursion
When function is called within the same function, it is known as
recursion in C++. The function which calls the same function, is
known as recursive function.
A function that calls itself, and doesn’t perform any task after
function call, is known as tail recursion. In tail recursion, we
generally call the same function with return statement.
Syntax:
We have Direct recursion and in direct recursion
1. Direct Recursion:
it can be done when the function calls itself
C++
#include <iostream>
using namespace std;
void directRecursion(int n) {
if (n > 0) {
cout << n << " ";
directRecursion(n - 1); // Function calls itself
}
}
int main() {
directRecursion(10);
return 0;
}
Output
5 4 3 2 1
2. Indirect Recursion
In this One function calls another function .
C++
#include <iostream>
using namespace std;
void indirectRecursionA(int n) {
if (n > 0) {
cout << n << " ";
indirectRecursionB(n - 1); // Function A calls Function B
}
}
void indirectRecursionB(int n) {
if (n > 1) {
cout << n << " ";
indirectRecursionA(n / 2); // Function B calls Function A
}
}
int main() {
indirectRecursionA(10);
return 0;
}
Output
10 9 4 3 1
void tailRecursion(int n) {
if (n > 0) {
cout << n << " ";
tailRecursion(n - 1); // Recursive call is the last operation
}
}
int main() {
tailRecursion(15);
return 0;
}
Output
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
2. Non-Tail Recursion
Non-tail recursion occurs when a function performs some
operations after the recursive call.
C++
#include <iostream>
using namespace std;
int nonTailRecursion(int n) {
if (n > 0) {
return n + nonTailRecursion(n - 1); // Recursive call is not
the last operation
} else {
return 0;
}
}
int main() {
int result = nonTailRecursion(20);
cout << "Result: " << result << endl;
return 0;
}
Output
Result: 210
To know more see this article.
Output
Minimum element is: 10
Time complexity: O(n) where n is the size of the array
Space complexity: O(n) where n is the size of the array.
C++ Overloading (Function)
If we create two or more members having the same name but
different in number or type of parameters, it is known as C++
overloading. In C++, we can overload:
methods,
constructors and
indexed properties
Types of overloading in C++ are:
Function overloading
Operator overloading
C++ Function Overloading
Function Overloading is defined as the process of having two or
more functions with the same name, but different parameters. In
function overloading, the function is redefined by using either
different types or number of arguments. It is only through these
differences a compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the
readability of the program because you don’t need to use
different names for the same action.
Example: changing number of arguments of add() method
C++
// program of function overloading when number of arguments
// vary
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void)
{
Cal C; // class object declaration.
cout << C.add(10, 20) << endl;
cout << C.add(12, 20, 23);
return 0;
}
Output
30
55
Time complexity: O(1)
Space complexity: O(1)
Example: when the type of the arguments vary.
C++
// Program of function overloading with different types of
// arguments.
#include <iostream>
using namespace std;
int mul(int, int);
float mul(float, int);
Output
r1 is : 42
r2 is : 0.6
Time Complexity: O(1)
Space Complexity: O(1)
Function Overloading and Ambiguity
When the compiler is unable to decide which function is to be
invoked among the overloaded function, this situation is known
as function overloading ambiguity.
When the compiler shows the ambiguity error, the compiler does
not run the program.
Causes of Ambiguity:
Type Conversion.
Function with default arguments.
Function with pass-by-reference.
Type Conversion:-
C++
#include <iostream>
using namespace std;
void fun(int);
void fun(float);
void fun(int i) { cout << "Value of i is : " << i << endl; }
void fun(float j)
{
cout << "Value of j is : " << j << endl;
}
int main()
{
fun(12);
fun(1.2);
return 0;
}
return 0;
}
public:
void set_data();
friend void find_max(Largest);
};
void Largest::set_data()
{
cout << "Enter the first number : ";
cin >> a;
cout << "\nEnter the second number : ";
cin >> b;
}
void find_max(Largest t)
{
if (t.a > t.b)
t.m = t.a;
else
t.m = t.b;
int main()
{
Largest l;
l.set_data();
find_max(l);
return 0;
}
Output
Enter the first number : 789
Enter the second number : 982
Largest number is 982