You are on page 1of 5

Lab Session 09

Objective
C++ User defined Functions

Theory
Functions (called methods or procedures in other programming languages) allow you to
modularize a program by separating its tasks into self-contained units. Functions you write are
referred to as user-defined functions or programmer-defined functions. The statements in
function bodies are written only once, are reused from perhaps several locations in a program and
are hidden from other functions.

There are several motivations for modularizing a program with functions.


 A fragment of code that appears in your program in multiple places can be placed into a
function definition and replaced by a function call instead, resulting in a smaller, more
maintainable program. It is smaller because there are fewer lines of code. It is more
maintainable because if you decide to change the code, you only have to do it in one place,
instead of searching through the entire program for all occurrences of that code fragment.

 By creating a function and putting a code fragment into it, the program becomes easier to
read and more modular.

 Functions can be reused more easily in other programs. You may find that you need the
same code fragment in many programs. By creating a function that encapsulates it, you can
put that code into other programs without having to rename variables and reorganize the
program code.

Defining a Function:

return_type function_name( parameter list )


{
body of the function
}
A C++ function definition consists of a function header and a function body.
Here are all the parts of a function:

 Return Type: A function may or may not return a value. The return_type is the data type
of the value the function returns.
 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.
A function is invoked by a function call, and when the called function completes its task, it either
returns a result or simply returns control to the caller.
Example:
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Declarations:
A function declaration tells the compiler about a function name and how to call the function. The
actual body of the function can be defined separately.
A function declaration has the following parts:

return_type function_name( parameter list );

For the above defined function max, following is the function declaration:

int max(int num1, int num2);

Parameter names are not importan in function declaration only their type is required, so following
is also valid declaration:

int max(int, int);

Function declaration is required when you define a function in one source file and you call that
function in another file. In such case, you should declare the function at the top of the file calling
the function.

Calling a 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. A called function performs defined task and
when its return statement is executed or when its function ending closing brace is reached, it returns
program control back to the main program. To call a function, you simply need to pass the required
parameters along with function name, and if function returns a value, then you can store returned
value.

Example 1:
#include <iostream>
using namespace std;

void max(int num1, int num2)


{
int result;
if (num1 > num2)
cout<<"maximum value is ="<<num1<<endl;
else
cout<<"maximum value is ="<<num2<<endl;
}

int main ()
{
int a, b;
cin>>a>>b;
max(a, b);

return 0;
}
Example 2:
#include <iostream>
using namespace std;

int max(int num1, int num2);

int main ()
{
int a, b, ret;
cin>>a>>b;
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}

int max(int num1, int num2)


{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Example 3:
#include<iostream>
using namespace std;

int factorial(int );
int power(int, int);
int main()
{
int x, n, sign=-1;
float sum=0;
cout<<"Enter the value of x=";
cin>>x;

cout<<"Enter the value of n=";


cin>>n;

for(int i=1; i<=n; i++)


{
sign=sign*-1;
sum=sum+(power(x, 2*i-1)/(float)factorial(2*i-1))*sign;
}

cout<<"sum of the series ="<<sum<<endl;

return 0;
}

int factorial(int x)
{
int fact=1;
for(int i=1; i<=x; i++)
{
fact=fact*i;
}
return fact;
}

int power(int base, int exp)


{
int pwr=1;
for(int i=1; i<=exp; i++)
{
pwr=pwr*base;
}
return pwr;
}

Call 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 you 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().

Call by reference in C++


In call by reference, original value is modified because we pass reference (address).
Here, 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 swap;
swap=x;
x=y;
y=swap;
}

int main()
{
int x=500, y=100;
swap(x, y);
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
return 0;
}

Tasks:
1. Write a function to calculate the factorial value of any integer as an argument. Call this
function from main( ) and print the results in main( )
2. Write a function that receives two numbers as an argument and display all prime numbers
between these two numbers. Call this function from main( ).
3. Write a function called zero_small() that has two integer arguments being passed by
reference and sets the smaller of the two numbers to 0. Write the main program to access
the function.

You might also like