BPLCK105D - Module 2 - Functions in C++
BPLCK105D - Module 2 - Functions in C++
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.
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.
• Identifiers cannot contain any spaces or special characters except for the
underscore.
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:
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
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];
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
Example 1:
#include <iostream>
int main ()
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;
#include <iostream>
class Operate
public:
void fun();
};
int main ()
Operate op;
op.fun();
return 0;
expressions are calculated, then the overall expression. A function call that returns some
• 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.
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.
Example:
Int main()
Int a=10,b=20,c;
c = add(a,b);
cout<<”Add”<<c;
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
#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; }
Int &test()
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:
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) { }
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;
}