PROBLEM SOLVING
USING- C++
24CS203ES103
DR. SHITENDU SOME
ASSISTANT PROFESSOR
MECHANICAL ENGINEERING DEPARTMENT,
SR UNIVERSITY, WARANGAL, TELANGANA, INDIA.
1
UNIT - 4
2
SESSION: 5
INLINE FUNCTIONS
Suggests the compiler to replace the function call with its
definition.
#include <iostream>
using namespace std; The 'inline' keyword
inline int square(int x) suggests replacing
'square(5)' directly with '5 *
{ return x * x; } 5' to improve performance
int main() { for small functions.
cout << square(5) <<
endl;
return 0; 3
}
SESSION: 5
FUNCTION TEMPLATES
Generic functions that work with any data type.
#include <iostream>
using namespace std;
template <typename T>
T maxVal(T a, T b) { return (a > b) ? a : b; }
The template function
int main() { 'maxVal' works for both
integers and floating-point
cout << maxVal(10, 20) << endl; numbers, avoiding repetitive
code.
cout << maxVal(5.5, 2.2) << endl;
return 0;
4
}
SESSION: 5
RECURSION
A function calling itself to solve a problem.
#include <iostream>
using namespace std;
int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}
int main() { The function calls itself to
calculate the factorial of a
cout << factorial(5) << endl; number, reducing problem
size in each step
return 0;
} 5
SESSION: 5
LAMBDA FUNCTIONS
Anonymous functions defined inline.
#include <iostream>
using namespace std;
int main() {
auto square = [](int x) { return x * x; };
cout << square(5) << endl;
return 0;
The lambda function is
} defined inline using '[]' and
used like a regular function
6
SESSION: 5
MUTABLE LAMBDA
EXPRESSIONS
Allows modifying captured variables inside a lambda.
#include <iostream>
using namespace std;
int main() {
int count = 0;
auto increment = [count]() mutable { return ++count; };
cout << increment() << endl;
The 'mutable' keyword
cout << increment() << endl; allows the lambda to modify
return 0; the captured variable
'count'.
} 7
SESSION: 6
FUNCTION OBJECTS
(FUNCTORS)
Objects that can be called like functions.
#include <iostream>
using namespace std;
struct Multiply {
int operator()(int a, int b) { return a * b; }
};
int main() {
Multiply multiply;
cout << multiply(5, 3) << endl; The struct 'Multiply'
overloads 'operator()' to
return 0;
8
make instances callable like
functions.
}
SESSION: 6
STD::FUNCTION AND
STD::BIND
std::function stores callable objects, std::bind creates
partial functions.
#include <iostream>
#include <functional> 'std::function' allows storing
using namespace std; functions, and 'std::bind'
creates a function where
int add(int a, int b) { return a + b; } one parameter is pre-set.
int main() {
function<int(int, int)> func = add;
auto boundFunc = bind(add, 10, placeholders::_1);
cout << func(5, 10) << endl;
cout << boundFunc(20) << endl; 9
return 0;
SESSION: 6
VARIADIC TEMPLATES
Functions accepting variable number of arguments.
#include <iostream>
using namespace std;
template<typename... Args>
void print(Args... args) { (cout << ... << args) << endl; }
The variadic template 'print'
int main() { can take any number of
print(1, 2, "Hello", 3.5); arguments, using '...' to
unpack them
return 0;
}
10
SESSION: 6
“THIS” POINTER REVISIT
• this-pointer is an implicit pointer available in non-
static member functions of a class.
• It holds the memory address of the calling object and
is useful for distinguishing between class attributes
and function parameters when they have the same
name.
11
SESSION: 6
#include <iostream> int main() {
using namespace std; Example obj;
obj.setX(10);
class Example { obj.print();
private: return 0;
int x; }
public:
void setX(int x) { setX function has a
this->x = x; parameter x, which has the
same name as the class
} member x.
void print() { This-> refers to the class
member, while x refers to
cout << "Value of x: " the function parameter. 12
<< x << endl;
}
SESSION: 6
FUNCTION PROTOTYPE
#include <iostream>
• A function prototype is a using namespace std;
declaration of a function // Function Prototype
int add(int, int);
that informs the compiler
about the function's int main() {
name, return type, and int result = add(5, 10); //
parameters before its Function call
actual definition. cout << "Sum: " << result;
return 0;
• It ensures that a function }
can be called before it is
defined. // Function Definition
int add(int a, int b) { 13
return a + b;
}
SESSION: 6
FUNCTION PROTOTYPE
#include <iostream>
• A function prototype is a using namespace std;
declaration of a function // Function Prototype
int add(int, int);
that informs the compiler
about the function's int main() {
name, return type, and int result = add(5, 10); //
parameters before its Function call
actual definition. cout << "Sum: " << result;
return 0;
• It ensures that a function }
can be called before it is
defined. // Function Definition
int add(int a, int b) { 14
return a + b;
}
SESSION: 6
REVISIT- FUNCTION WITH
STRUCTURE
Passing Structure by Value
#include <iostream>
using namespace std;
struct Student { Structure s is passed to the
string name; function
int age;
};
void display(Student s) { // Passed by value
cout << "Name: " << s.name << ", Age: " << s.age << endl;
}
int main() {
Student s1 = {"Rahul", 20};
display(s1); // Function call 15
return 0;
}
SESSION: 6
REVISIT- FUNCTION WITH
STRUCTURE
Passing Structure by Reference
#include <iostream>
using namespace std;
struct Student { Structure s is passed to the
string name; function by reference
int age;
};
void update(Student &s) { // Passed by reference
s.age += 1; // Modify original structure
} Remember here, Original
structure will change after
int main() { calling by reference
Student s1 = {"Rahul", 20};
cout << "Before: " << s1.age << endl; 16
update(s1); // Pass by reference
cout << "After: " << s1.age << endl;