0% found this document useful (0 votes)
42 views10 pages

BPLCK105D - Module 2 - Functions in C++

best notes

Uploaded by

Varun S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views10 pages

BPLCK105D - Module 2 - Functions in C++

best notes

Uploaded by

Varun S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C++ Tokens

Each word and punctuation is referred to as a token in C++. Tokens are the smallest
building block or smallest unit of a C++ program.

The following tokens are available in C++:


• Identifiers
• Keywords
• Constants
• Operators
• Strings

Identifiers: Identifiers are names given to various entries, such as variables, structures, and
functions. Also, identifier names must be unique as these entities are used in program
execution.

Some rules must be followed when choosing an identifier:


• The first character must be a letter or an underscore.

• Identifiers cannot be the same as a keyword.

• Identifiers cannot contain any spaces or special characters except for the

underscore.

• Identifiers are case-sensitive, meaning that a variable named "myVariable" is

different from a variable named "myvariable".

Keywords: Keywords are reserved words with fixed meanings that cannot be changed.

The meaning and working of these keywords are already known to the compiler. Eg:

namespace, public, inline, bool.

Constants: Constants are like a variable, except that their value never changes during

execution once defined. Constants often represent fixed values used frequently in the

code. Eg: const double PI = 3.14159;

Operators: In C++, operators are special symbols that are used to perform operations on

one or more operands. An operand is a value on which an operator acts. C++ has a wide
range of operators, including arithmetic operators, relational operators, logical operators,

and others.

Strings: Strings are objects that signify sequences of characters which is enclosed inside the

double quotes.

Char string-name[size];

Char city[9]=”New York”;

Scope Resolution Operator

The scope resolution operator is used to reference the global variable or member

function that is out of scope. Therefore, we use the scope resolution operator to access

the hidden variable or function of a program. The operator is represented as the

double colon (::) symbol.

Example 1:

#include <iostream>

using namespace std;

int num = 50;

int main ()

int num = 100;

cout << " The value of the local variable num: " << num; O/P:100
// use scope resolution operator (::) to access the global variable

cout << "\n The value of the global variable num: " <<: :num; O/P:50

return 0;

To access the hidden variable /function of a program

#include <iostream>

using namespace std;

class Operate

public:

void fun();

};

// define the member function outside the class.

void Operate::fun() /* return_type Class_Name::function_name */

cout << " It is the member function of the class. ";

int main ()

// create an object of the class Operate

Operate op;

op.fun();

return 0;

Expression and their types


An expression can be a combination of other expressions; while computation, first inner

expressions are calculated, then the overall expression. A function call that returns some

value could be part of an expression.


• Integral expressions: The expressions that produce an integer value as output after
performing all types of conversions are called integral expressions. For example, x, 6*x-y
and 10 +int (5.0) are integral expressions. Here, x and yare variables of type into

• Float expressions: The expressions that produce floating-point value as output after
performing all types of conversions are called float expressions. For example, 9.25, x-y
and 9+ float (7) are float expressions. Here, x ‘and yare variables of type float.

• Relational or Boolean expressions: The expressions that produce a bool type value, that
is, either true or false are called relational or Boolean expressions. For example, x + y<100,
m + n==a-b and a>=b + c. are relational expressions.

• Logical expressions: The expressions that produce a bool type value after combining
two or more relational expressions are called logical expressions. For example, x==5
&&m==5 and y>x I I m<=n are logical expressions.

• Bitwise expressions: The expressions which manipulate data at bit level are called bitwise
expressions. For example, a >> 4 and b<< 2 are bitwise expressions.

• Pointer expressions: The expressions that give address values as output are
called pointer expressions. For example, &x, ptr and -ptr are pointer expressions. Here, x
is a variable of any type and ptr is a pointer.

• Special assignment expressions: An expression can be categorized further depending


upon the way the values are assigned to the variables.

• Chained assignment: Chained assignment is an assignment expression in which


the same value is assigned to more than one variable, using a single statement.
For example, consider these statements.

a = (b=20); or a=b=20;
int a=b=30; // illegal
int a=30, int b=30; //valid
• Embedded assignment: Embedded assignment is an assignment expression,
which is enclosed within another assignment expression. For example, consider
this statement
a=20+(b=30); //equivalent to b=30; a=20+30;
• Compound Assignment: Compound Assignment is an assignment expression,
which uses a compound assignment operator that is a combination of the
assignment operator with a binary arithmetic operator. For example, consider this
statement.
a + =20; //equivalent to a=a+20;
Function prototype
A function prototype describes the function interface to the compiler by giving details
such as the number and type of arguments and the type of return values.

The prototype declaration looks just like a function definition except that it has no body
i.e., its code is missing. This is the time you knew the difference between a declaration
and a definition.

Syntax: return_type name of the function (argument list);

Void add(int x,int y);

Example:

Int add(int x,int y); //function declaration

Int main()

Int a=10,b=20,c;

c = add(a,b);

cout<<”Add”<<c;

Int add(int x,int y)

Int z;

z=x+y;

returnz;

Call by reference
• Call by Reference is a method in which it passes the reference or address of the
actual parameter to the function's formal parameters, which means if there is any
change in the values inside the function, it reflects that change in the actual values.

• If there is any change in the values inside the function, it reflects that change in
the actual values.

Example:
void increment(int s)
{
s=s+5000;
cout<<”After increment\n”;
}
Int main()
{
Int sal;
sal=50000;
increment(sal);
cout<<”Before incerement”<<sal;
return 0;
}
Return by reference
Return a value by reference

Int& add(int a,int b);

This function returns a reference of the variable

#include <iostream>
using namespace std;
int num;
int& test();
int main()
{
test() = 5;
cout << num;
return 0;
}
int& test() {
return num;
}
Note : test() =”return type of function” is int&
We cannot return a constant from a function

Int& test()

{ return 2; }

We cannot return a local variable from this function

Int &test()

{ int n=2;return n;}


Inline Functions
When an instruction of a function call is encountered during the compilation of a program, its
memory address is stored by the compiler. The function arguments are copied on the stack and
after the execution of the code, the control is transferred to the calling instruction.

After the execution of the code , the controlled transferred to the calling function.

In case of inline functions, the compiler does not go through the above process of switching
between the stack and calling function. Instead, it copies the definition of the inline function and
the calling instruction with that definition.

When the function is small, execution time is lesser than the switching time.

Example:

inline void printSum(int num1,int num2) {


cout << num1 + num2 << "\n";
}
int main() {
// call the inline function
// first call
printSum(10, 20);
// second call
printSum(2, 5);
// third call
printSum(100, 400);
return 0;
}
Default Arguments

If a function with default arguments is called without passing arguments, then the default
parameters are used.

We can understand the working of default arguments from the image above:
• When temp() is called, both the default parameters are used by the function.

• When temp(6) is called, the first argument becomes 6 while the default value is
used for the second parameter.

• When temp(6, -2.3) is called, both the default parameters are overridden, resulting
in i = 6 and f = -2.3.

• When temp(3.4) is passed, the function behaves in an undesired way because the
second argument cannot be passed without passing the first argument.

Therefore, 3.4 is passed as the first argument. Since the first argument has been defined
as int, the value that is actually passed is 3.

Function Overloading
These functions having the same name but different arguments are known as overloaded
functions.

Examples
int test() { }

int test(int a) { }

float test(double a) { }

int test(int a, double b) { }

Class Moverlaod
{
Int add (int a , int b)
{
Int sum=a+b;
return sum;
}
Int add (int a , int b,int c)
{
Int sum=a+b+c;
return sum;
}
float add(float a,float b)
{
Float a+b;
return f;
}
}
Int main()
{
Moverload m1,m2,m3;
int s1,s2;
float s3;
s1=m1.add(20,30);
s2=m2.add(10,20,30);
s3=m3.add(1.5,2.5);
cout<<s1<<s2<<s3;
return 0;
}

You might also like