OOPs Unit 2
OOPs Unit 2
C++ Function :
A function is a block of code which only runs when it is called.
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);
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.
The parameters received by the function are called formal parameters. For example, in
the above program x and y are formal parameters.
{
cout << fname << " Refsnes\n";
}
int main()
{
myFunction("Liam");
myFunction("Jenny"); ( Actual Parameter)
myFunction("Anja");
return 0;
}
There are two ways to pass parameters:
Example.
#include <iostream>
using namespace std;
{
x += 1;
return x;
}
{
int x = 3;
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;
}
Actual and formal arguments will be Actual and formal arguments will be
created at created at
different memory location same memory location.
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.
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";
}
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:
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:
Example:
#include <iostream>
Syntax of Macro:
#define MACRO_NAME Macro_definition
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;
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.
#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()
{