6. • 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
7. 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”
8.
9. 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
10.
11. 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);
12.
13. “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
14.
15. int main( )
{
char str[50];
cout <<"Enter a name : ";
cin >> str;
cout <<"Entered name is " << str;
}
.getline(str , 50 );; .getline(str , 50 , ‘.’ );
16. • 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.
17. • 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.
21. 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)
22. 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
23. 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.
24.
25.
26. #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;
}
27.
28. • 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
Hinweis der Redaktion
(No assumption that a function returns int if no prototype is mentioned as in C)
int main()
{
int result = add(5 , 8);
printf(&quot; %d&quot; , result);
}
int add(int a,int b)
{
return a + b;
}
Change the extension to c++ and compile the program
http://stackoverflow.com/questions/3092006/function-declaration-kr-vs-ansi
In C, A non void function can skip the return statement(No compilation error, returns some garbage)
OVERLOADING A FUNCTION NAME
If you have two or more function definitions for the same function name, that is called overloading.
When you overload a function name, the function definitions must have different numbers of
formal parameters or some formal parameters of different types. When there is a function call, the
compiler uses the function definition whose number of formal parameters and types of formal
parameters match the arguments in the function call.
(Interface always refers to “function names” and implementation always refers to “data and function definition”).
A best match must be unique. Function selection involves the following steps. The compiler first tries to find an exact match in which the types of actual arguments are the same and use that function. If an exact match is not found, the compiler uses the integral promotions to actual arguments, such as, char to int, or float to double to find the match. In case of multiple matches the compiler will generate an error message.
C++ supports function overloading, i.e., there can be more than one functions with same name and differences in parameters. 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