KEMBAR78
Default Arguments in C | PDF | Parameter (Computer Programming) | Pointer (Computer Programming)
0% found this document useful (0 votes)
33 views15 pages

Default Arguments in C

The document discusses default arguments, inline functions, recursion, function pointers, and dynamic memory allocation in C++. It explains how default arguments are assigned by the compiler, the benefits and drawbacks of inline functions, and provides examples of recursion and function pointers. Additionally, it covers dynamic memory allocation using the 'new' operator and the importance of managing memory during program execution.

Uploaded by

sridhar2879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views15 pages

Default Arguments in C

The document discusses default arguments, inline functions, recursion, function pointers, and dynamic memory allocation in C++. It explains how default arguments are assigned by the compiler, the benefits and drawbacks of inline functions, and provides examples of recursion and function pointers. Additionally, it covers dynamic memory allocation using the 'new' operator and the importance of managing memory during program execution.

Uploaded by

sridhar2879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Default arguments in C++

A default argument is a value in the function declaration automatically assigned by the compiler if the
calling function does not pass any value to that argument.

Following are the rules of declaring default arguments -

o The values passed in the default arguments are not constant. These values can be overwritten if
the value is passed to the function. If not, the previously declared value retains.

o During the calling of function, the values are copied from left to right.

o All the values that will be given default value will be on the right.

Example

o void function(int x, int y, int z = 0)


Explanation - The above function is valid. Here z is the value that is predefined as a part of the
default argument.

o Void function(int x, int z = 0, int y)


Explanation - The above function is invalid. Here z is the value defined in between, and it is not
accepted.

1. #include<iostream>

2. using namespace std;

3. int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default arguments

4. { // Both z and w are initialised to zero

5. return (x + y + z + w); // return sum of all parameter values

6. }

7. int main()

8. {

9. cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0

10. cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w = 0

11. cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w = 30

12. return 0;

13. }
Inline function in C++

If make a function as inline, then the compiler replaces the function calling location with the definition
of the inline function at compile time.

Any changes made to an inline function will require the inline function to be recompiled again because
the compiler would need to replace all the code with a new code; otherwise, it will execute the old
functionality.

Syntax for an inline function:

inline return_type function_name(parameters)

// function code?

Let's understand the difference between the normal function and the
inline function.

Inside the main() method, when the function fun1() is called, the control is transferred to the definition
of the called function. The addresses from where the function is called and the definition of the function
are different. This control transfer takes a lot of time and increases the overhead.

When the inline function is encountered, then the definition of the function is copied to it. In this case,
there is no control transfer which saves a lot of time and also decreases the overhead.

Let's understand through an example.

1. #include <iostream>

2. using namespace std;

3. inline int add(int a, int b)

4. {

5. return(a+b);

6. }

7. int main()

8. {

9. cout<<"Addition of 'a' and 'b' is:"<<add(2,3);A


10. return 0;

11.

12. }

Once the compilation is done, the code would be like as shown as below:

1. #include<iostream>

2. using namespace std;

3. inline int add(int a, int b)

4. {

5. return(a+b);

6. }

7. int main()

8. {

9. cout<<"Addition of 'a' and 'b' is:"<<return(2+3);

10. return 0;

11. }

Why do we need an inline function in C++?

The main use of the inline function in C++ is to save memory space. Whenever the function is called,
then it takes a lot of time to execute the tasks, such as moving to the calling function. If the length of the
function is small, then the substantial amount of execution time is spent in such overheads, and
sometimes time taken required for moving to the calling function will be greater than the time taken
required to execute that function.

The solution to this problem is to use macro definitions known as macros. The preprocessor macros are
widely used in C, but the major drawback with the macros is that these are not normal functions which
means the error checking process will not be done during the compilation.

C++ has provided one solution to this problem. In the case of function calling, the time for calling such
small functions is huge, so to overcome such a problem, a new concept was introduced known as an
inline function. When the function is encountered inside the main() method, it is expanded with its
definition thus saving time.

We cannot provide the inlining to the functions in the following circumstances:


o If a function is recursive.

o If a function contains a loop like for, while, do-while loop.

o If a function contains static variables.

o If a function contains a switch or go to statement

When do we require an inline function?

An inline function can be used in the following scenarios:

o An inline function can be used when the performance is required.

o It can be used over the macros.

o We can use the inline function outside the class so that we can hide the internal implementation
of the function.

Advantages of inline function

o In the inline function, we do not need to call a function, so it does not cause any overhead.

o It also saves the overhead of the return statement from a function.

o It does not require any stack on which we can push or pop the variables as it does not perform
any function calling.

o An inline function is mainly beneficial for the embedded systems as it yields less code than a
normal function.

Disadvantages of inline function

The following are the disadvantages of an inline function:

o The variables that are created inside the inline function will consume additional registers. If the
variables increase, then the use of registers also increases, which may increase the overhead on
register variable resource utilization. It means that when the function call is replaced with an
inline function body, then the number of variables also increases, leading to an increase in the
number of registers. This causes an overhead on resource utilization.

o If we use many inline functions, then the binary executable file also becomes large.

o The use of so many inline functions can reduce the instruction cache hit rate, reducing the speed
of instruction fetch from the cache memory to that of the primary memory.
o It also increases the compile-time overhead because whenever the changes are made inside the
inline function, then the code needs to be recompiled again to reflect the changes; otherwise, it
will execute the old functionality.

o Sometimes inline functions are not useful for many embedded systems because, in some cases,
the size of the embedded is considered more important than the speed.

o It can also cause thrashing due to the increase in the size of the binary executable file. If the
thrashing occurs in the memory, then it leads to the degradation in the performance of the
computer.

Recursive Functions

When function is called within the same function, it is known as recursion in C++. The function which
calls the same function, is known as recursive function.

A function that calls itself, and doesn't perform any task after function call, is known as tail recursion. In
tail recursion, we generally call the same function with return statement.

Let's see a simple example of recursion.

1. recursionfunction(){

2. recursionfunction(); //calling self function

3. }

C++ Recursion Example

Let's see an example to print factorial number using recursion in C++ language.

1. #include<iostream>

2. using namespace std;

3. int main()

4. {

5. int factorial(int);

6. int fact,value;

7. cout<<"Enter any number: ";

8. cin>>value;

9. fact=factorial(value);
10. cout<<"Factorial of a number is: "<<fact<<endl;

11. return 0;

12. }

13. int factorial(int n)

14. {

15. if(n<0)

16. return(-1); /*Wrong value*/

17. if(n==0)

18. return(1); /*Terminating condition*/

19. else

20. {

21. return(n*factorial(n-1));

22. }

23. }

Output:

Enter any number: 5

Factorial of a number is: 120

Function Pointer in C++

As we know that pointers are used to point some variables; similarly, the function pointer is a pointer
used to point functions. It is basically used to store the address of a function. We can call the function by
using the function pointer, or we can also pass the pointer to another function as a parameter.

They are mainly useful for event-driven applications, callbacks, and even for storing the functions in
arrays.

Syntax for Declaration

The following is the syntax for the declaration of a function pointer:


1. int (*FuncPtr) (int,int);

The above syntax is the function declaration. As functions are not simple as variables, but C++ is a type
safe, so function pointers have return type and parameter list. In the above syntax, we first supply the
return type, and then the name of the pointer, i.e., FuncPtr which is surrounded by the brackets and
preceded by the pointer symbol, i.e., (*). After this, we have supplied the parameter list (int,int). The
above function pointer can point to any function which takes two integer parameters and returns
integer type value.

Address of a function

We can get the address of a function very easily. We just need to mention the name of the function, we
do not need to call the function.

Let's illustrate through an example.

1. #include <iostream>

2. using namespace std;

3. int main()

4. {

5. std::cout << "Address of a main() function is : " <<&main<< std::endl;

6. return 0;

7. }

In the above program, we are displaying the address of a main() function. To print the address of a
main() function, we have just mentioned the name of the function, there is no bracket not parameters.
Therefore, the name of the function by itself without any brackets or parameters means the address of a
function.

We can use the alternate way to print the address of a function, i.e., &main.

Calling a function indirectly

We can call the function with the help of a function pointer by simply using the name of the function
pointer. The syntax of calling the function through the function pointer would be similar as we do the
calling of the function normally.

Let's understand this scenario through an example.


1. #include <iostream>

2. using namespace std;

3. int add(int a , int b)

4. {

5. return a+b;

6. }

7. int main()

8. {

9. int (*funcptr)(int,int); // function pointer declaration

10. funcptr=add; // funcptr is pointing to the add function

11. int sum=funcptr(5,5);

12. std::cout << "value of sum is :" <<sum<< std::endl;

13. return 0;

14. }

In the above program, we declare the function pointer, i.e., int (*funcptr)(int,int) and then we store the
address of add() function in funcptr. This implies that funcptr contains the address of add() function.
Now, we can call the add() function by using funcptr. The statement funcptr(5,5) calls the add() function,
and the result of add() function gets stored in sum variable.

Let's look at another example of function pointer.

1. #include <iostream>

2. using namespace std;

3. void printname(char *name)

4. {

5. std::cout << "Name is :" <<name<< std::endl;


6. }

7.

8. int main()

9. {

10. char s[20]; // array declaration

11. void (*ptr)(char*); // function pointer declaration

12. ptr=printname; // storing the address of printname in ptr.

13. std::cout << "Enter the name of the person: " << std::endl;

14. cin>>s;

15. cout<<s;

16. ptr(s); // calling printname() function

17. return 0;

18. }

In the above program, we define the function printname() which contains the char pointer as a
parameter. We declare the function pointer, i.e., void (*ptr)(char*). The statement ptr=printname
means that we are assigning the address of printname() function to ptr. Now, we can call the
printname() function by using the statement ptr(s).

Passing a function pointer as a parameter

The function pointer can be passed as a parameter to another function.

Let's understand through an example.

1. #include <iostream>

2. using namespace std;

3. void func1()

4. {

5. cout<<"func1 is called";

6. }

7. void func2(void (*funcptr)())


8. {

9. funcptr();

10. }

11. int main()

12. {

13. func2(func1);

14. return 0;

15. }

In the above code, the func2() function takes the function pointer as a parameter. The main() method
calls the func2() function in which the address of func1() is passed. In this way, the func2() function is
calling the func1() indirectly
Dynamic memory allocation in C++

There are times where the data to be entered is allocated at the time of execution. For example, a list of
employees increases as the new employees are hired in the organization and similarly reduces when a
person leaves the organization. This is called managing the memory. So now, let us discuss the concept
of dynamic memory allocation.

Memory allocation

Reserving or providing space to a variable is called memory allocation. For storing the data, memory
allocation can be done in two ways -

o Static allocation or compile-time allocation - Static memory allocation means providing space
for the variable. The size and data type of the variable is known, and it remains constant
throughout the program.

o Dynamic allocation or run-time allocation - The allocation in which memory is allocated


dynamically. In this type of allocation, the exact size of the variable is not known in advance.
Pointers play a major role in dynamic memory allocation

Why Dynamic memory allocation?

Dynamically we can allocate storage while the program is in a running state, but variables cannot be
created "on the fly". Thus, there are two criteria for dynamic memory allocation -

o A dynamic space in the memory is needed.

o Storing the address to access the variable from the memory

Similarly, we do memory de-allocation for the variables in the memory.

In C++, memory is divided into two parts -

o Stack - All the variables that are declared inside any function take memory from the stack.

o Heap - It is unused memory in the program that is generally used for dynamic memory
allocation.

Dynamic memory allocation using the new operator

To allocate the space dynamically, the operator new is used. It means creating a request for memory
allocation on the free store. If memory is available, memory is initialized, and the address of that space
is returned to a pointer variable.
Syntax

Pointer_variable = new data_type;

The pointer_varible is of pointer data_type. The data type can be int, float, string, char, etc.

Example

int *m = NULL // Initially we have a NULL pointer

m = new int // memory is requested to the variable

It can be directly declared by putting the following statement in a line -

int *m = new int

Initialize memory

We can also initialize memory using new operator.

For example

int *m = new int(20);

Float *d = new float(21.01);

Allocate a block of memory

We can also use a new operator to allocate a block(array) of a particular data type.

For example

int *arr = new int[10]

Here we have dynamically allocated memory for ten integers which also returns a pointer to the first
element of the array. Hence, arr[0] is the first element and so on.

Note

o The difference between creating a normal array and allocating a block using new normal arrays
is deallocated by the compiler. Whereas the block is created dynamically until the programmer
deletes it or if the program terminates.

o If there is no space in the heap memory, the new request results in a failure throwing an
exception(std::bad_alloc) until we use nonthrow with the new operator. Thus, the best practice
is to first check for the pointer variable.

Code
1. int *m = new(nonthrow) int;

2. if(!m) // check if memory is available

3. {

4. cout<< "No memory allocated";

5. }

Now as we have allocated the memory dynamically. Let us learn how to delete it.

Delete operator

We delete the allocated space in C++ using the delete operator.

Syntax

delete pointer_variable_name

Example

delete m; // free m that is a variable

delete [] arr; // Release a block of memory

1. // The program will show the use of new and delete

2. #include <iostream>

3. using namespace std;

4. int main ()

5. {

6. // Pointer initialization to null

7. int* m = NULL;
8.

9. // Request memory for the variable

10. // using new operator

11. m = new(nothrow) int;

12. if (!m)

13. cout<< "allocation of memory failed\n";

14. else

15. {

16. // Store value at allocated address

17. *m=29;

18. cout<< "Value of m: " << *m <<endl;

19. }

20. // Request block of memory

21. // using new operator

22. float *f = new float(75.25);

23. cout<< "Value of f: " << *f <<endl;

24. // Request block of memory of size

25. int size = 5;

26. int *arr = new(nothrow) int[size];

27. if (!arr)

28. cout<< "allocation of memory failed\n";

29. else

30. {

31. for (int i = 0; i< size; i++)

32. arr[i] = i+1;


33.

34. cout<< "Value store in block of memory: ";

35. for (int i = 0; i< size; i++)

36. cout<<arr[i] << " ";

37. }

38.

39. // freed the allocated memory

40. delete m;

41. delete f;

42. // freed the block of allocated memory

43. delete[] arr;

44.

45. return 0;

46. }

You might also like