Function Untill Overload
Function Untill Overload
Types of Functions
1. Library Functions: are the functions which are declared in the C++ header files such
as ceil(x), cos(x), exp(x), etc.
2. User-defined 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.
Declaration of a function
The syntax of creating function in C++ language is given below:
practical
Function Example
Let's see the simple example of C++ function.
#include <iostream>
using namespace std;
void func() {
int i=0;
int j=0;
i++,j++;
cout<<"i=" << i<<" and j=" <<j<<endl;
}
int main()
{ func();
func();
func();
}
Call by value and call by reference in C++
There are two ways to pass value or data to function
1. call by value
2. call by reference.
Call by Value
The call-by-value method allows you to copy the actual parameter to a formal
parameter. In this case, if we change the formal parameter then the actual parameter
doesn’t change. C++ uses call by value method by default.
practical
Call by refrence
#include<iostream>
using namespace std;
void swap (int &num1, int &num2) //&num1 and &bnum2 are Reference
variables
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
int main()
{
int a=5,b=10;
cout<<"\n Before swapping"<<"\n A = "<<a<<"\n B = "<<b<<endl;
swap(a,b);
cout<<"\n After swapping"<<"\n A = "<<a<<"\n B = "<<b<<endl;
return 0;
}
2 Changes made inside the function is not reflected on Changes made inside the
other functions function is reflected outside
the function also
3 Actual and formal arguments will be created in Actual and formal arguments
different memory location will be created in same
memory location
Function Overloading
Two functions can have the same name if the number and/or type of arguments
passed is different.
These functions having the same name but different arguments are
known as overloaded functions.
For example:
int test() { }
int test(int a) { }
float test(double a) { }
Notice that the return types of all these 4 functions are not the same.
Overloaded functions may or may not have different return types but they
must have different arguments.
For example
// Error code
int test(int a) { }
both functions have the same name, the same type, and the same number of
arguments. Hence, the compiler will throw an error.
practical
Write Program to compute absolute value Works for both int and float
#include <iostream>
var = -var;
return var;
if (var < 0)
var = -var;
return var;
int main() {
cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;}
Function Template
We can create a single function to work with different data types by using a
template.
Defining a Function Template
starts with the keyword template followed by template parameter(s) inside <> which is followed by the
function definition.
// code
T is a template argument that accepts different data types (int, float, etc.), and typename is a keyword.
When an argument of a data type is passed to functionName(), the compiler generates a new version
functionName() for the given data type.
practical
int main() {
int result1;
double result2;
return 0;
Inline Functions:
This copies the function to the location of the function call in compile-time and may make the
program execution faster.
we use the inline keyword. For syntax,
#include <iostream>
using namespace std;
C++ Recursion
A function that calls itself is known as a recursive function. And, this technique is known as
recursion.
int main()
{
... .. ...
recurse();
... .. ...
}
note
To prevent infinite recursion, if...else statement (or similar approach) can be used where one
branch makes the recursive call and the other doesn't.
It makes our code shorter and cleaner. It takes a lot of stack space compared to an
Recursion reduce the length of code. iterative program.
It is very useful in solving the data It uses more processor time.
structure problem.
Reduce unnecessary calling of function. It can be more difficult to debug compared to
an equivalent iterative program.
#include <iostream>
using namespace std;
int factorial(int);
int main() {
int n, result;
result = factorial(n);
cout << "Factorial of " << n << " = " << result;
return 0;
}
int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}
C++ Math
C++ has many functions that allows you to perform mathematical tasks on
numbers.
C++ <cmath> Header
Other functions, such as sqrt (square root), round (rounds a number) and log (natural
logarithm), can be found in the <cmath> header file:
example
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << sqrt(64) << "\n";
cout << round(2.6) << "\n";
cout << log(2) << "\n";
return 0;
}