0% found this document useful (0 votes)
19 views16 pages

Module2 C++

C++
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
19 views16 pages

Module2 C++

C++
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

Module-2 (8 hours)

Functions in C++: Tokens – Keywords – Identifiers and constants – Operators in C++ –


Scope resolution operator – Expressions and their types – Special assignment expressions –
Function prototyping – Call by reference – Return by reference – Inline functions -Default
arguments – Function overloading.

Textbook 2: Chapter 3(3.2,3.3,3.4,3.13,3.14,3.19, 3.20) , chapter 4(4.3,4.4,4.5,4.6,4.7,4.9)

Tokens:
https://www.geeksforgeeks.org/cpp-tokens/

Keywords :
Ref tokens

Identifiers:

What is the identifier?

Identifiers refer to the name of variables, functions, arrays, classes,


etc. created by the programmer. They are the fundamental
requirement of any language.
Rules for naming identifiers:
 Identifier names can not start with a digit or any special
character.
 A keyword cannot be used as an identifier name.
 Only alphabetic characters, digits, and underscores are
permitted.
 The upper case and lower case letters are distinct. i.e., A and a
are different in C++.
 The valid identifiers are GFG, gfg, and geeks_for_geeks.
Examples of good and bad identifiers

Invalid Identifier Bad Identifier Good Identifier

Cash prize C_prize cashprize

catch catch_1 catch1

1list list_1 list1

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;

cout << "Identifier result is: "


<< geeks_for_geeks;

return 0;
}

Output
Identifier result is: 1

How keywords are different from identifiers?

So there are some main properties of keywords that distinguish


keywords from identifiers:

Keywords Identifiers

Keywords are identifiers are the values used to define


predefined/reserved different programming items like a
words variable, integers, structures, and unions.

Keywords always start in identifiers can start with an uppercase


lowercase letter as well as a lowercase letter.

It defines the type of


It classifies the name of the entity.
entity.

A keyword contains only an identifier can consist of alphabetical


alphabetical characters, characters, digits, and underscores.

It should be lowercase. It can be both upper and lowercase.

No special symbols or No special symbols or punctuations are


punctuations are used in used in keywords and identifiers. The only
keywords and identifiers. underscore can be used in an identifier.

Keywords: int, char,


Identifiers: Geeks_for_Geeks, GFG, Gfg1.
while, do.

Example: Below is the program for how to use different keywords in


the program:
 C++
// C++ Program to demonstrate keywords
#include <iostream>
using namespace std;

// Driver Code
int main()
{
// Variable declaration and
// initialization
int n = 2;

// Switch Case Statement


switch (n) {
case 1:
cout << "Computer Network"
<< endl;
break;
case 2:
cout << "C++" << endl;
break;
case 3:
cout << "DBMS" << endl;
break;
case 4:
cout << "Data Structure"
<< endl;
break;
case 5:
cout << "Operating System"
<< endl;
break;
default:
cout << "Enter Valid number"
<< endl;
}

// Return keyword returns an object


// to a function's caller
return 0;
}
Output
C++
Constants:
https://www.geeksforgeeks.org/cpp-constants/

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.

Refer the following link for explaination of each operator.

Write definition and 1 example program for each operator.

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

Special assignment expressions:


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>

using namespace std;

// Following function that takes two parameters 'x' and 'y'

// as input and returns max of two input numbers

int max(int x, int y)

if (x > y)

return x;

else

return y;

// main function that doesn't receive any parameter and

// returns integer

int main()

int a = 10, b = 20;


// Calling above function to find max of 'a' and 'b'

int m = max(a, b);

cout << "m is " << m;

return 0;

Output:
m is 20

Function Declaration/Function Prototyping:


A function declaration tells the compiler about the number of
parameters, data types of parameters, and returns type of function.
This allows the compiler to know about the function before it's used, enabling
proper compilation and error checking.
#include <iostream>

// Function prototype
int add(int a, int b);

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

// Function call
int sum = add(num1, num2);

std::cout << "Sum: " << sum << std::endl;

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;

void swap(int x, int y) {


int temp = x;
x = y;
y = temp;
}

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;

// Function to return a reference to the object's value member


int& getValueRef() {
return value; // Return reference to value member (must be valid)
}
};

int main() {
Number num;
num.value = 10;

cout << "Before modification: " << num.value <<endl;

// Get a reference to num.value using the member function


int& refToValue = num.getValueRef();
// Modify the value through the reference
refToValue = 20;

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>

// Inline function to calculate the square of a number

inline int square(int x) {

return x * x;

int main() {

int num = 5;

// Call to the inline function

int result = square(num);


std::cout << "Square of " << num << " is: " << result << std::endl;

return 0;

The inline keyword before the function declaration suggests to the



compiler that it should attempt to expand the function in place at each
call site, rather than generating a separate function call.
Advantages:

Performance Improvement:

Optimization Opportunities:

Avoidance of Function Call Overhead

Disadvantages:

Compilation Time Increase

Debugging Complexity

Default Arguments in C++


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;

Output
25
50
80

You might also like