Documente Academic
Documente Profesional
Documente Cultură
Function
• 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.
• The idea is to put some commonly or repeatedly done task together and
make a function, so that instead of writing the same code again and
again for different inputs, we can call the function.
Function Definition
• Includes description of the interface and the function body
• Defines function header and function body
• Value-returning functions
return value
Function Definition (contd.)
• Non value-returning functions
• Example:
float volume(float x, float y, float z) ;
Calling a function
• A function is called by specifying its name followed by its arguments.
• Non-value returning functions:
function-name (data passed to function);
int num;
// Function declaration
int& test();
int main()
{
test() = 5;
cout << num;
return 0;
}
int& test()
{
return num;
}
Inline Functions
• If a function is inline, the compiler places a copy of the code of that
function at each point where the function is called at compile time.
• Any change to an inline function could require all clients of the function to
be recompiled because compiler would need to replace all the code
once again otherwise it will continue with old functionality.
• To inline a function, place the keyword inline before the function name
and define the function before any calls are made to the function.
• The compiler can ignore the inline qualifier in case defined function is
more than a line.
• Syntax:
inline return_type function_name(arguments...)
{
//function_code
return_value;
}
• Example:
#include <iostream>
using namespace std;
inline int Max(int x, int y) //Inline Function
{
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;
}
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.
Constant Arguments
• In C++, an argument to a function can be declared as constant. The
argument with constant value should be initialized during the function
declaration.
• Syntax:
type function_name(const data_type variable_name=value);
• Example:
int max(const int a=3, int b); //function prototype
Function Overloading
• Function overloading is a feature in C++ where two or more functions can
have the same name but different parameters.
• It can be considered as an example of polymorphism feature in C++.
• Basically, It means we 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.
• Example:
#include <iostream>
using namespace
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); //uses int
print(10.10); //uses float
print("ten"); //uses char
return 0;
}
End.