3. Functions C++

A function is a set of statements that take inputs, do some specific computation and produces output.

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.

Syntax:-
return_type function_name([ arg1_type arg1_name, ... ])
 { code
 }

Types of function:-

C allows us to define functions according to our need. These functions are known as user-defined functions. 

Example:-
#include <iostream.h> using namespace std; int max(int x, int y) { if (x > y) return x; else return y; } int main() { int a = 10, b = 20; int m = max(a, b); // Calling above function to find max of 'a' and 'b' cout << "m is " << m; return 0; }
Output:- m is 20


Function prototype

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body.

A function prototype gives information to the compiler that the function may later be used in the program.

Syntax:-

returnType functionName(type1 argument1, type2 argument2, ...);

Compiler may not perform inlining in such circumstances like:-
1) If a function contains a loop. (for, while, 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 function body.
5) If a function contains switch or goto statement.

Inline function 

C++ provides an inline functions to reduce the function call overhead. 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 inline function call. This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it is small.

Syntax:-
inline return-type function-name(parameters)
{
    // function code
}  

Example:-
#include <iostream>
using namespace std;
inline int Max(int x, int y) {
   return (x > y)? x : y;
}
int main() {
   cout << "Max (20,10): " << Max(20,10) << endl;
   cout << "Max (0,200): " << Max(0,200) << endl;
   cout << "Max (100,1010): " << Max(100,1010) << endl;
   return 0;
}
Output:-
            Max (20,10): 20
           Max (0,200): 200
           Max (100,1010): 1010

Inline functions provide following advantages:
1) Function call overhead doesn’t occur.
2) It also saves the overhead of push/pop variables on the stack when function is called.
3) It also saves overhead of a return call from a function.
4) Inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function call preamble and return.

Inline function disadvantages:
1) 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.

2) 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.

3) Inline function may increase compile time 

4) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed.

5) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.

Function Overloading in C++

C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.

Example:-

#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(15); 

  print(10.10); 

  print("ten"); 

  return 0; 

}

Output:-

         Here is int 15 

        Here is float 10.10

        Here is char* ten 

Library functions

Library functions which are also called as “built-in” functions are the functions that are already available and implemented in C++.
We can directly call these functions in our program as per our requirements. Library functions in C++ are declared and defined in special files called “Header Files” which we can reference in our C++ programs using the “include” directive.
Syntax:-
#include <cmath>

Some of the standard library header files that are used in C++ are tabularized as below. These headers replace their respective counterparts with “.h” extension.




Post a Comment

0 Comments