C++ Functions
◇ Presented by Kathryne Tarrayo and Jester Tiu
What is a Function?
Function
In programming, function refers to a segment
that groups code to perform a specific task.
Depending on whether a function is
predefined or created by programmer; there
are two types of function:
1. Library Function / Built-in Function
2. User-defined Function
User-Defined Function
C++ allows programmer to define their own function.
A user defined function groups code to perform a
specific task and that group of code is given a
name(identifier).
When the function is invoked from any part of program,
it all executes the codes defined in the body of function.
“
int main() function1();
{
statement;
function1();
return;
function2(); function3();
statement
function2(); statement
statement; function3();
function4(); return; return;
statement;
function4();
return 0;
} return;
User-defined Function
It consist of the following:
Function Names,
Function Body,
Return Values,
Parameters,
Arguments
return_type function_name ( parameter list )
Function Name − This is the actual
name of the function. The function
“
name and the parameter list together
constitute the function signature.
Parameters − A parameter is like a
placeholder. When a function is
invoked, you pass a value to the
parameter. This value is referred to as
actual parameter or argument. The
parameter list refers to the type, order,
and number of the parameters of a
function. Parameters are optional; that
is, a function may contain no
parameters.
“ Function Body − The
function body contains a
collection of statements that
define what the function
does.
function_name variable_name
Calling function − To use a function, you
“
will have to call or invoke that function.
When a program calls a function,
program control is transferred to the
called function.
variable_name
Return Statement − A
“
function may return a value.
Some functions perform the
desired operations without
returning a value.
“
“
Library function
Library function
• Library functions are the built-in function in C++
programming.
• Programmer can use library function by invoking
function directly; they don't need to write it
themselves.
• Completely debugged, efficient and always
produce a precise output.
• We need not to declare and define these functions
as they are already written in the C++ libraries
such as iostream, cmath etc.
• Can directly call when needed.
“
Examples of built-in functions
TITLE DESCRIPTION
pow() Computes Power a Number
remainder() Returns remainder of x/y
sqrt() Computes Square Root of A Number
cbrt() Computes Cube Root of a Number
pow()
remainder()
sqrt()
cbrt()