The document discusses functions in C++. It defines a function as a self-contained program that performs a specific task. Functions help break large programs into smaller, modular pieces. The key parts of a function include the prototype, definition, arguments, return statement, and calling a function. Functions make programs easier to understand, maintain and debug.
2. Introduction
Function is a self contained program. In case the size of
the program is large, it becomes difficult to maintain it
and it is also hard to identify the flow of data. It is
preferred to divide the large program into small
modules called functions. Each function takes the data
that are provided by the main() function and carries out
operations as per the requirement, and results can be
written to the calling function.
2
3. Advantages of the functions are as follows:
• Reusability: A function once written can be invoked again and
again, thus helping us to reuse the code and removing data
redundancy.
• Modularity: Functions can help us in breaking a large, hard to
manage problem into smaller manageable sub-problems. It is easier
to understand the logic of sub-programs.
• Reduced Program Size: Functions can reduce the size of the
program by removing data redundancy.
• Easy Debugging: Using functions, debugging of a program becomes
very easy, as it is easier to locate and rectify the bug in the program
if functions are used.
• Easy Updating: If we need to update some code in the program,
then it is much more easier in case we have used functions.
3
4. #include<iostream.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here
int main( ) {
float m, n ;
cout<<"Enter some number for finding square n”;
cin>>m ;
// function call
n = square ( m ) ;
cout<<“nSquare of the given number “<<m<<“ is “<<n }
float square ( float x ) {
float p ; Function Definition
p = x * x ;
return ( p ) ; } 4
6. Parts of the function
Parts of a function are as follows.
• Function prototype declaration
• Function call
• Definition of a function
• Actual and formal arguments
• Return statement
6
7. Function prototype declaration
A prototype statement helps the compiler to check the return and
argument types of the function.
A function prototype declaration consists of function’s return type,
name, and arguments list. It tells the compiler
Name of the function
Type of value returned
The type and number of arguments
The general Syntax: <Data type> <function name> (with/without
arguments list with/without data types)
Example : float sum (float, int);
float sum (float x, int y);
When the programmer defines the function, the definition of
function must be same like its prototype declaration.
7
8. Function Call
A function is a latent(hidden) body. It gets activated only when a call
to a function is invoked. A function must be called by its name
followed by argument, or without argument, list enclosed in
parenthesis and terminated by semicolon.
The general Syntax of function call is as follows:
function-name(with/without argument list);
Example : sum (x,y);
product (x,y);
Function Definition
The first line is called function definition and function body follows
it. The function definition and function prototype should match
with each other. The function body is enclosed within curly
braces. The function can be defined anywhere. If the function is
defined before its caller, then its prototype declaration is
optional. 8
9. Syntax of function call is as follows:
return_data_type function-name (argument/parameter list);
{
variable declarations
function statements
}
Actual and Formal Argument
The arguments declared in caller function and given in the function call are called
actual arguments. The arguments declared in the function definition are
known as formal arguments.
9
10. variables y and z are actual arguments and variables j and
k are formal arguments. The values of y and z are stored
in j and k, respectively. The values of actual arguments
are assigned to formal arguments. The function uses
formal arguments for computing.
The return Statement
The return statement is used to return value to the caller
function. The return statement returns only one value
at a time. When a return statement is encountered,
complier transfers the control of the program to caller
function.
The syntax of return statement is as follows:
return (variable name); or return variable name;
10
11. Factorial of a Number
#include <iostream.h>
long factorial(int);
int main() {
int number;
cout<<“Enter a number to calculate it's factorialn“<<;
cin>>number;
cout<< number<< factorial(number);
return 0; }
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result; } 11
12. PASSING ARGUMENTS
The main objective of passing argument to function is
message passing. The message passing is also known as
communication between two functions, that is between
caller and called functions. There are three methods by
which we can pass values to the function. These
methods are as follows:
Call by value (pass by value)
Call by address (pass by address)
Call by Reference
The arguments used to send values to function are known
as input arguments. The arguments used to return
result are known as output arguments.
12
13. While passing values to the function, the following
conditions should be fulfilled.
1. The data type and number of actual and formal
arguments should be same both in caller and callee
functions.
2. Extra arguments are discarded if they are declared.
3. If the formal arguments are more than the actual
arguments, then the extra arguments appear as
garbage.
4. Any mismatch in the data type will produce the
unexpected result.
13
14. Call by Value
In this type, values of actual arguments are passed to the
formal arguments and operation is done on the formal
arguments. Any change in the formal arguments does
not effect to the actual arguments because formal
arguments are photocopy of actual arguments. Hence,
when function is called by call by value method, it does
not affect the actual contents of actual arguments. The
advantage of this call by value is that actual parameters
are fully protected because their values are not
changed when control is returned to the calling
function.
14
15. Call by Value
#include <iostream.h>
void swap(int x, int y);
int main () {
int a = 100, b = 200;
cout<<“Before swap, value of a : %dn“<< a ;
cout<<"Before swap, value of b : %dn",<<b;
swap(a, b);
cout<<"After swap, value of a : %dn “<< a ;
cout<<"After swap, value of b : %dn “<< b ;
return 0;
}
void swap(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
}
15
16. Call by Address
In this type, instead of passing values, addresses of actual
parameters are passed to the function by using
pointers. Function operates on addresses rather than
values. Here the formal arguments are pointers to the
actual arguments. Because of this, when the values of
formal arguments are changed, the values of actual
parameters also change. Hence changes made in the
argument are permanent.
16
17. #include <iostream.h>
void swap(int *x, int *y);
int main () {
int a = 100, b= 200;
cout<<Before swap, value of a : %dn“<< a;
cout<<Before swap, value of b : %dn“<< b;
/* calling a function to swap the values.* &a indicates pointer to a ie. address
of variable a and * &b indicates pointer to b ie. address of variable b. */
swap(&a, &b);
cout<<"After swap, value of a : %dn“<<a;
cout<<"After swap, value of b : %dn“<< b;
return 0; }
void swap(int *x, int *y) {
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
} 17
18. • Call by Reference
C passes arguments by value and address. In C++ it is
possible to pass arguments by value, address, and
reference. C++ reference types, declared with ‘&’
operator, are nearly identical but not exactly the same
as pointer types. They declare aliases for objects
variables and allow the programmer to pass arguments
by reference to functions. The reference
decelerator (&) can be used to declare references
outside functions.
18
19. int k = 0;
int &kk = k; // kk is an alias for k
kk = 2; // same effect as k = 2
This creates the lvalue kk as an alias (assumed name) for k,
provided that the initializer is the same type as the reference.
Any operations on kk have exactly the same result as operations
on k.
Example,
kk = 5 // assigns 5 to kk and
&kk // returns the address of kk.
The reference decelerator can also be used to declare reference type parameters
within a function:
void funcA (int i);
void funcB (int &kk); // kk is type “reference to int”
int s=4;
funcA(s); // s passed by value
funcB(s); // s passed by reference 19