Sunteți pe pagina 1din 19

Functions in C++

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-data-type function-name(parameter list)


{
constant declarations
variable declarations

other C++ statements

return value
Function Definition (contd.)
• Non value-returning functions

void function-name(parameter list)


{
constant declarations
variable declarations

other C++ statements


}

• The argument names in the function header are referred to as formal


parameters.
Function Prototype
• Every function should have a function prototype.
• The function prototype specifies the type of the value that the function
returns (if any) and the type, number, and order of the function's
arguments.
return-data-type function-name(argument data types);
or
void function-name(argument data types);

• 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);

• Value returning functions:


results = function-name (data passed to function);

• The argument names in the function call are referred to as actual


parameters.
Calling a function by value
• In call by value, original value is not modified.
• In call by value, value being passed to the function is locally stored by the
function parameter in stack memory location.
• If we change the value of function parameter, it is changed for the
current function only. It will not change the value of variable inside the
caller method such as main().
• Example:
#include <iostream>
using namespace std;
void swap(int x, int y);
int main () {
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}

void swap(int x, int y) //Call by Value


{
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
Calling a function by reference
• In call by reference, original value is modified because we pass reference
(address).
• In call by reference, address of the value is passed in the function, so
actual and formal arguments share the same address space. Hence,
value changed inside the function, is reflected inside as well as outside the
function.
• Example:
#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main () {
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}

void swap(int &x, int &y) //Call by Reference


{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
Return by reference
• A function can also return a reference.
• Unlike return by value, here return statement doesn't return value, instead it
returns the variable itself (address).
• In the example used, the return type of function test() is int&. Hence, this
function returns a reference of the variable num.
• Example:
#include <iostream>
using namespace std;

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.