Module2 C++
Module2 C++
Tokens:
https://www.geeksforgeeks.org/cpp-tokens/
Keywords :
Ref tokens
Identifiers:
Example:
C++
// C++ program to illustrate the use
// of identifiers
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Use of Underscore (_) symbol
// in variable declaration
int geeks_for_geeks = 1;
return 0;
}
Output
Identifier result is: 1
Keywords Identifiers
// Driver Code
int main()
{
// Variable declaration and
// initialization
int n = 2;
Operators in C++:
Basic operators:
An operator is a symbol that operates on a value to perform
specific mathematical or logical computations.
An operator operates the operands. For example,
int c = a + b;
Here, ‘+’ is the addition operator. ‘a’ and ‘b’ are the operands that
are being ‘added’.
6 types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Ternary or Conditional Operators.
https://www.geeksforgeeks.org/operators-in-cpp/
Scope resolution operator:
https://www.geeksforgeeks.org/scope-resolution-operator-in-c/
Decision Making Statements in C++:
https://www.geeksforgeeks.org/cpp-decision-making/
Expressions and their types:
Link1:
https://www.geeksforgeeks.org/what-is-an-expression-and-
what-are-the-types-of-expressions/
Link2:
https://www.javatpoint.com/cpp-expression
Functions in C++
A function is a set of statements that takes input, does some
specific computation, and produces output. The idea is to put some
commonly or repeatedly done tasks together to make a function so
that instead of writing the same code again and again for different
inputs, we can call this function.
// C++ Program to demonstrate working of a function
#include <iostream>
if (x > y)
return x;
else
return y;
// returns integer
int main()
return 0;
Output:
m is 20
// Function prototype
int add(int a, int b);
int main() {
int num1 = 5, num2 = 10;
// Function call
int sum = add(num1, num2);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Call by Value and call by reference:
Call by Value:
In call by value, a copy of the actual parameter's value is passed to the
function.
The function works with this copy of the value, and any changes made to the
parameter inside the function do not affect the original variable passed as
an argument.
Call by value is simple and straightforward to use.
It is suitable for passing small-sized variables or when the original value
should not be modified.
Changes made to the parameter inside the function do not affect the original
variable.
#include <iostream>
using namespace std;
int main() {
int a = 40;
int b = 50;
cout << "Before swap: a = " << a << " b = " << b << endl;
swap(a, b);
cout << "After swap: a = " << a << " b = " << b << endl;
return 0;
}
Output
Before swap: a = 40 b = 50
After swap: a = 40 b = 50
Call by Reference:
In call by reference, the memory address (reference) of the actual parameter
is passed to the function.
The function operates directly on the original variable through this reference,
allowing modifications to be reflected in the original variable.
Call by reference avoids making copies of data, which can be more efficient,
especially for large data types.
It is suitable for passing large data structures or when modifications to the
parameter need to be reflected in the original variable.
Changes made to the parameter inside the function are reflected in the
original variable.
#include <iostream>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5;
int y = 10;
cout << "Before swap: x = " << x << " , y = " << y << endl;
swap(&x, &y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
Output
Before swap: x = 5, y = 10
After swap: x = 10, y = 5
Return by reference:
Unlike a typical return statement that returns a copy of the value, return by
reference provides a way to return a reference to the actual variable within
the function.
This allows the calling code to modify the original variable through the
returned reference.
#include <iostream>
using namespace std;
class Number {
public:
int value;
int main() {
Number num;
num.value = 10;
cout << "After modification (using reference): " << num.value << endl;
return 0;
}
Output:
Before modification: 10
After modification (using reference): 20
Inline function:
In C++, an inline function is a function that is expanded in place at the point
of each function call, rather than being called like a regular function. This can
potentially improve performance by reducing the overhead of function call
and return.
Syntax:
inline return-type function-name(parameters)
{
// function code
}
#include <iostream>
return x * x;
int main() {
int num = 5;
return 0;
Performance Improvement:
Optimization Opportunities:
Disadvantages:
Debugging Complexity
#include <iostream>
{
return (x + y + z + w);
// Driver Code
int main()
// Statement 1
// Statement 2
// Statement 3
return 0;
Output
25
50
80