Unit 4 Functions (1)
Unit 4 Functions (1)
simple functions
call by reference
return by reference
Inline functions
overloaded functions
default arguments
A function is a block of code that performs a specific task. A function
is a group of statements that together perform a task. Every C++
program has at least one function, which is main(), and all the most
trivial programs can define additional functions.
A function is known with various names like a method or a sub-routine
or a procedure etc.
There are two types of function:
Standard Library Functions: Predefined in C++
User-defined Function: Created by users
Defining a Function
The general form of a C++ function definition is as follows −
Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument. The parameter list refers to the type,
order, and number of the parameters of a function. Parameters are
optional; that is, a function may contain no parameters.
Function Declarations
A function declaration tells the compiler about a function name and
how to call the function. The actual body of the function can be defined
separately.
A function declaration has the following parts −
return_type function_name( parameter list );
Calling a Function
While creating a C++ function, you give a definition of what the
function has to do. To use a function, you will have to call or invoke
that function.
When a program calls a function, program control is transferred to the
called function. A called function performs defined task and when it’s
return statement is executed or when its function-ending closing brace
is reached, it returns program control back to the main program.
To call a function, you simply need to pass the required parameters
along with function name, and if function returns a value, then you can
store returned value.
Example: -
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main () {
// local variable declaration:
int a = 100;
int b = 200;
int ret;
return 0;
}
return result;
}
Function Arguments
If a function is to use arguments, it must declare variables that accept the values
of the arguments. These variables are called the formal parameters of the
function.
The formal parameters behave like other local variables inside the function and
are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways that arguments can be passed to a
function.
Sr.No Call Type & Description
1 Call by Value
This method copies the actual value of an argument into the formal parameter of the
function. In this case, changes made to the parameter inside the function have no effect
on the argument.
2 Call by Reference
This method copies the reference of an argument into the formal parameter. Inside the
function, the reference is used to access the actual argument used in the call. This
means that changes made to the parameter affect the argument.
2 Changes made inside the function is not Changes made inside the function is
reflected on other functions reflected outside the function also
3 Actual and formal arguments will be Actual and formal arguments will be
created in different memory location created in same memory location
1) The added variables from the inlined function consumes additional registers,
After in-lining function if variables number which are going to use register
increases than they may create overhead on register variable resource utilization.
This means that when inline function body is substituted at the point of function
call, total number of variables used by the function also gets inserted. So the
number of register going to be used for the variables will also get increased. So
if after function inlining variable numbers increase drastically then it would
surely cause an overhead on register utilization.
2) If you use too many inline functions then the size of the binary executable file
will be large, because of the duplication of same code.
3) Too much inlining can also reduce your instruction cache hit rate, thus reducing
the speed of instruction fetch from that of cache memory to that of primary
memory.
4) Inline function may increase compile time overhead if someone changes the
code inside the inline function then all the calling location has to be recompiled
because compiler would require to replace all the code once again to reflect the
changes, otherwise it will continue with old functionality.
5) Inline functions may not be useful for many embedded systems. Because in
embedded systems code size is more important than speed.
6) Inline functions might cause thrashing because inlining might increase size of
the binary executable file. Thrashing in memory causes performance of computer
to degrade.
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;
}
Output: The cube of 3 is: 27
Example:
#include <iostream>
int main() {
return 0;
}
Overloaded functions
Function overloading is a feature of object oriented programming where two or
more functions can have the same name but different parameters.
In Function Overloading “Function” name should be the same and the arguments
should be different.
Function overloading can be considered as an example of polymorphism feature
in C++.
Following is a simple C++ example to demonstrate function overloading.
#include <iostream>
using namespace std;
void print(int i)
{
cout << " Here is int " << i << endl;
}
void print(double f)
{
cout << " Here is float " << f << endl;
}
void print(char const *c)
{
cout << " Here is char* " << c << endl;
}
int main()
{
print(10);
print(10.10);
print("ten");
return 0;
}
Output:
Here is int 10
Here is float 10.1
Here is char* ten
Default arguments
A default argument is a value provided in a function declaration that is
automatically assigned by the compiler if the caller of the function doesn’t
provide a value for the argument with a default value. In case any value is passed
the default value is overridden.
// Driver Code
int main()
{
// Statement 1
cout << sum(10, 15) << endl;
// Statement 2
cout << sum(10, 15, 25) << endl;
// Statement 3
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
Output
25
50
80
References
https://www.geeksforgeeks.org/
https://www.programiz.com/
https://www.javatpoint.com/
https://www.w3schools.com/cpp/
https://www.codingninjas.com/