29

Functions in C++

Embed Size (px)

Citation preview

Objectives

In this chapter you will learn about

•Strict type checking

•Function Overloading

•Default Arguments

•Inline Function

• In C, Prototype is not required if the function returns integer.

• printf can be used without including stdio.h

In C++, prototype is compulsory even if functions return int.

Inclusion of stdio.h is compulsory if you are using printf function.

C PROGRAM C PROGRAM

• Can we pass argument for function given the following Prototype

• int func();

• In C –Yes , says that a function can take zero or more number of arguments. (K & R Style of Prototyping)

• In C++ - No, says that a function takes zero arguments

A feature of C++ which allows multiple functions to have same function name

Calls to an overloaded function are resolved using function signatures.

Allows multiple functions which perform similar tasks to be grouped under a single name “single interface and multiple implementations”

How does C++ compiler distinguishes between different functions when it generates object code

It changes names by adding information about arguments. 

This technique of adding additional information to function names is called Name Mangling

What is the difference between strcpy and strncpy?

Write two functions by name mystrcpy. • First functions takes two arguments (dest ,

src)• Second takes three arguments(dest,src,n);

“Single interface multiple implementations” is technically termed as “Polymorphism”.

The binding takes place during compile time, so its called “compile time polymorphism” or “static polymorphism”.

Ensure that function overloading in unambiguous

int main( ){

char str[50];cout <<"Enter a name : ";cin >> str;cout <<"Entered name is " << str;

}

.getline(str , 50 );; .getline(str , 50 , ‘.’ );

• A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn't provide a value for the argument.

• Allows a function to be called without providing one or more trailing arguments.

• Default arguments should be provided only in function prototype and shouldn’t be repeated in function definition. The compiler uses the prototype to build a function call and not the function definition.

• Only trailing arguments should be given default values. i.e., you cant have a default argument followed by a non default argument.

void printBinary(int num,int bits = 8);

int main( ){

printBinary(23);printBinary(500 , 16);printBinary(28 );

}

void printBinary(int num,int bits){

for(int i = bits - 1 ; i >= 0 ; i--)cout << ((num & 1 << i )?1:0) ;

}

If a function is declared as inline, at the time of compilation, the body of the function is expanded at the point at which it is invoked.

For small functions, the inline function removes all the overheads of function calls.

To make a function inline prefix the keyword inline before the function name Syntax is

inline <return_Type> <Func_Name>(arguments)

Not all requests to make a function inline are honored. Generally C++ examines the• complexity of the function• should request for inline replacement only if the functions are short.

The compiler may ignore the function as inline when:• If there are loops, switch statements or goto’s in the function• If there are static variables in a function• If the function is recursive• If the function call and function definition are not in a single

translation unit

Inline functions are functionally similar to #define macros. In both the cases, the body of the macro or the function is expanded. But inline functions are preferred over macros because of three main reasons:

• Argument Evaluation - Arguments are first evaluated and then expanded unlike in macros where arguments are expanded and then evaluated.

• Type Checking - The types of the arguments are checked against the parameter list in the declaration for the function. As a result, any mismatch in the parameters can be detected at the time of compilation.

• Overloading - inline functions can be overloaded, which is not possible in the case of macros.

#define square(x) x * x OR

inline int square(int x){ return x * x; }

int main( ) {

int x = 5;cout << square( x + 2) << endl;return 0;

}

• Prototypes are a must in C++• Every function should have a explicit return-type.• Multiple functions can have same name with

different signature.• Default parameters allows us to pass lesser

arguments to functions.• Default parameters will help in combining

multiple functions into one.• Inline functions to be used as alternative to

macros• Reference is an alternate name given to existing

variable