0% found this document useful (0 votes)
3 views11 pages

OOPs Unit 2

This document provides an overview of functions in C++, including their definition, types (user-defined and library functions), and parameter passing methods (by value and by reference). It also covers function prototypes, inline functions, macros, function overloading, and recursion, detailing their syntax and usage with examples. The main function's significance as the entry point of a C++ program is also highlighted.
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)
3 views11 pages

OOPs Unit 2

This document provides an overview of functions in C++, including their definition, types (user-defined and library functions), and parameter passing methods (by value and by reference). It also covers function prototypes, inline functions, macros, function overloading, and recursion, detailing their syntax and usage with examples. The main function's significance as the entry point of a C++ program is also highlighted.
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/ 11

Unit 2

C++ Function :
A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

Functions are used to perform certain actions, and they are important for reusing code:
Define the code once, and use it many times.

C++ provides some pre-defined functions, such as main(), which is used to execute code. But
you can also create your own functions to perform certain actions.

To create (often referred to as declare) a function, specify the name of the function,
followed by parentheses ():

Syntax
void myFunction()

{
// code to be executed
}

Function Declaration :

A function declaration tells the compiler about the number of parameters, data types
of parameters, and returns type of function. Writing parameter names in the function
declaration is optional but it is necessary to put them in the definition. Below is an
example of function declarations. (parameter names are not present in the below
declarations)

Example
1. int max(int, int);

2. char* call(char b);

3. int fun(char, int);


Types of Functions

1. User-defined functions: User Define Functions are the functions which are created by the C+
+ programmer, so that he/she can use it many times. It reduces complexity of a big program
and optimizes the code.

2. Library Functions : Library Functions are the functions which are declared in the C++ header
files such as sqrt(), setw(), strcat(), etc.

Parameter Passing to Functions


The parameters passed to the function are called actual parameters. For example, in
the program below, 5 and 10 are actual parameters.

The parameters received by the function are called formal parameters. For example, in
the above program x and y are formal parameters.

void myFunction(string fname) (Formal Parameter)

{
cout << fname << " Refsnes\n";
}

int main()

{
myFunction("Liam");
myFunction("Jenny"); ( Actual Parameter)
myFunction("Anja");
return 0;
}
There are two ways to pass parameters:

1. Pass by Value: In this parameter passing method, values of actual parameters


are copied to the function’s formal parameters. The actual and formal
parameters are stored in different memory locations so any changes made in the
functions are not reflected in the actual parameters of the caller.

Example.

#include <iostream>
using namespace std;

int fun(int x) // Function to change value of x

{
x += 1;
return x;
}

int main() // Driver Code

{
int x = 3;

cout << "Value of X before modifying: " << x << endl;

x = fun(x);
cout << "Value of X after modifying: " << x << endl;

return 0;
}

2. Pass by Reference: Both actual and formal parameters refer to the same
locations, so any changes made inside the function are reflected in the actual
parameters of the caller.

Example:

#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int swap;
swap=*x;
*x=*y;
*y=swap;
}
int main()
{
int x=500, y=100;
swap(&x, &y); // passing value to function
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
return 0;
}

Difference between call by value and call by reference:


Call by value Call by reference

A copy of the value is passed to the An address of value is passed to the


function function

Changes made inside the function are


Changes made inside the function are not reflected
reflected on other functions outside the function as well

Actual and formal arguments will be Actual and formal arguments will be
created at created at
different memory location same memory location.

The main() function :

W hen a program begins running, the system calls the function main, which marks the entry
point of the program. By default, main has the storage class extern. Every program must
have one function named main, and the following constraints apply:
No other function in the program can be called main.
main cannot be defined as inline or static.

main cannot be called from within a program.

The address of main cannot be taken.

The main function cannot be overloaded.


The main function cannot be declared with the constexpr specifier
Syntax :
void main()
{
............
............
}

In above syntax;

void: is a keyword in C++ language, void means nothing, whenever we use void
as a function return type then that function nothing return. here main() function
no return any value.
In place of void we can also use int return type of main() function, at that time
main() return integer type value.
main: is a name of function which is predefined function in C++ library.
Example:
#include<stdio.h>

void main()
{
cout<<"This is main function";
}

Function Prototype in C++


A function prototype is a declaration of the function that informs the program about the
number and kind of parameters, as well as the type of value the function will return. One
incredibly helpful aspect of C++ functions is function prototyping. A function prototype
provides information, such as the number and type of parameters and the type of return
values, to explain the function interface to the compiler.
The prototype declaration resembles a function definition exactly, with the exception that it lacks
a body, or its code. At this point, you were aware of the distinction between a statement and a
definition.

A definition is a declaration that also informs the program what the function is doing and how it is
doing, as opposed to a declaration, which simply introduces a (function) name to the program.
Therefore, the examples above are function definitions, and the examples that follow are
declarations, or perhaps a better term would be function prototypes:

int valAbs ( int x ) ;


int greatcd ( int a1 , int a2 ) ;

the components of a function prototype are as follows:


o return type
o name of the function
o argument list
the prototype for the following function:
int add ( int a1 , int a2 ) ;
Here,
o return type - int
o name of the function - add
o argument list - (int a1, int a2)
The Function prototype is necessary to serve the following purposes:
1. Function prototype tells the return type of the data that the function will return.
2. Function prototype tells the number of arguments passed to the function.
3. Function prototype tells the data types of each of the passed arguments.
4. Also, the function prototype tells the order in which the arguments are passed
to the function.

Inline Functions :
C++ provides inline functions to reduce the function call overhead. An inline
function is a function that is expanded in line when it is called. When the inline
function is called whole code of the inline function gets inserted or substituted at the
point of the inline function call. This substitution is performed by the C++ compiler at
compile time. An inline function may increase efficiency if it is small. inlining is only a
request to the compiler, not a command. The compiler can ignore the request for
inlining.

Syntax:
inline return-type function-name(parameters)
{
// function code
}
The compiler may not perform inlining in such circumstances as:

1. If a function contains a loop. (for, while and do-while)


2. If a function contains static variables.
3. If a function is recursive.
4. If a function return type is other than void, and the return statement doesn’t
exist in a function body.
5. If a function contains a switch or goto statement.

Example:

#include <iostream>

using namespace std;

inline int cube(int s)


{
return s * s * s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}

Inline function and classes


It is also possible to define the inline function inside the class. In fact, all the functions
defined inside the class are implicitly inline. Thus, all the restrictions of inline functions
are also applied here. If you need to explicitly declare an inline function in the class
then just declare the function inside the class and define it outside the class using the
inline keyword.
Syntax:
class S
{
public:
inline int square(int s) // redundant use of inline
{
// this function is automatically inline
// function body
}
};
For Example:
class S
{
public:
int square(int s); // declare the function
};

inline int S::square(int s) // use inline prefix


{
}
Macro :

It is called preprocessors directive. The macros are defined by


the #define keyword. Before the program compilation, the preprocessor
examines the program whenever the preprocessor detects the macros then
preprocessor replaces the macro by the macro definition.

Syntax of Macro:
#define MACRO_NAME Macro_definition

Difference between Inline and Macro in C++ :

S.NO Inline Macro


An inline function is defined by Whereas the macros are defined by
1. the inline keyword. the #define keyword.
Through inline function, the class’s Whereas macro can’t access the
2. data members can be accessed. class’s data members.
In the case of inline function, the Whereas in the case of macros, the
3. program can be easily debugged. program can’t be easily debugged.
Whereas in the case of macro, the
In the case of inline, the arguments arguments are evaluated every time
4. whenever macro is used in the
are evaluated only once.
program.
Whereas the macro is all the time
In C++, inline may be defined either defined at the beginning of the
5. inside the class or outside the class. program.
Inline is not used in competitive While the macro is very much used
8. programming. in competitive programming.
While the macro is not terminated
Inline function is terminated by the by any symbol, it is terminated by a
9.
curly brace at the end. new line.

Function Overloading in C++


Function overloading is a feature of object-oriented programming where two or
more functions can have the same name but different parameters. When a
function name is overloaded with different jobs it is called Function Overloading.
In Function Overloading “Function” name should be the same and the
arguments should be different.

If multiple functions having same name but parameters of the functions should
be different is known as Function Overloading.
If we have to perform only one operation and having same name of the
functions increases the readability of the program.

Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)

Consider the following example, which have two functions that add numbers of
different type:

#include <iostream>
using namespace std;

void add(int a, int b)


{
cout << "sum = " << (a + b);
}

void add(double a, double b)


{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(10, 2);
add(5.3, 6.2);

return 0;
}

Recursion
Recursion in C++ is a technique in which a function calls itself repeatedly until a given
condition is satisfied. In other words, recursion is the process of solving a problem by
breaking it down into smaller, simpler sub-problems.

Syntax of Recursion

return_type recursive_func {
....
// Base Condition
// Recursive Case
....
}
Here,

The base condition is the condition that is used to terminate the recursion.
Recursive case is the way in which the recursive call is present in the function.

Example of C++ Recursion


The following C++ program illustrates how to perform recursion.

#include <iostream>
using namespace std;

int sum(int n)
{
if (n == 0) // base condition to terminate the recursion when N = 0
{
return 0;
}
int res = n + nSum(n - 1); // recursive case / recursive call
return res;
}

int main()
{

You might also like