User-Defined Functions I Puc
User-Defined Functions I Puc
User-Defined Functions I Puc
DEFINITION:
User defined function are defined by user and this function can be called or
invoked from anywhere and any number of times in program.
The main purpose is to make the program design process easy, understandable and
avoid confusion.
PAGE 1
COMPUTER SCIENCE NOTES FOR FIRST PUC
NOTE:
Calling function is a function that transfers control from it to another function by
specifying the name of that function and passing arguments.
PAGE 2
COMPUTER SCIENCE NOTES FOR FIRST PUC
Called function is a function that receives the call and arguments from another
calling function.
Function call is the statement that is used to call or make another function execute.
When a function call is made, the control jumps from calling function to the called
function.
5. RETURNING A VALUE.
When a function is called, the statements in the called function are executed.
After executing the statements, the function returns a value to the calling function.
The return statement is used to return a value and a function can return only one
value or none to the calling function, for every function call .
SYNTAX: return; OR return (expression);
The first return statement does not return any value. When return is encountered,
the control is immediately passed back to the calling function.
The second form of return with an expression returns the value of the expression.
The default return type is int. if you don’t want to return any value from a
function, use the data type void. It means nothing.
Return statement(s) is included in the called function. When return statement is
encountered the control is transferred back to the calling program. It returns a
constant or the value of a variable or expression. The returned value can be of any
data type.
If a function does not return any value, the return type should be mentioned as
void.
7. ARGUMENTS OR PRAMETERS.
A variable in a function header is called an argument. The arguments are used to pass
information from the calling function to the called function.
TYPES OF ARGUMENTS
ACTUAL PARAMETERS: The arguments found in the function call is called actual
parameters. Example: result= sum (a, b) a & b are called as actual arguments.
FORMAL PARAMETERS: The arguments found in the function header or function
definition of a function call is called formal parameters.
Example: int sum (int x, int y) x & y are called as actual arguments.
PAGE 3
COMPUTER SCIENCE NOTES FOR FIRST PUC
The name of actual arguments and formal argument can be same or different.
SCOPE OF VARIABLE.
It can be defined as the region over which the variable is visible or valid.
LOCAL VARIABLES:
Variable that are declared inside a function or a block are called as local variable.
They must be declared before they have used in program and used only inside the
function or block.
GLOBAL VARIABLES:
Variable declared outside the function are called global variable.
They can be accessed from any part of a program.
#include<iostream.h>
#include<conio.h>
int a=15; //GLOBAL VARIABLE
void main()
{
int b=10; // LOCAL VARIABLE
clrscr();
cout<<"Value of a outside block is"<<a;
cout<<"Value of b inside block is"<<b;
}
PAGE 4
COMPUTER SCIENCE NOTES FOR FIRST PUC
When a function has no arguments, it does not receive any data from the calling
function. Similarly, when it does not return a value, the calling function does not
receive any data from the called function.
So, there is no data transfer between the calling function and the called function.
#include<iostream.h>
void main()
{
void Average(); // Function prototype
Average(); // Function call statement
getch();
}
void Average()
{
int a,b,result;
cout << “Enter 2 numbers”;
cin >> a >> b;
result=(a+b)/2.0;
cout << “\n Average = ” << result;
}
PAGE 5
COMPUTER SCIENCE NOTES FOR FIRST PUC
Example: Program to find the largest of 2 numbers with arguments and no return
values.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
void Large(int, int); // Function prototype
cout<<"Enter 2 number”;
cin>>a>>b;
Large(a,b);
getch();
}
void Large(int x, int y) // Function header
{
if(x>y)
cout<<x<<”is largest”;
else
cout<<y<<”is largest”;
}
PAGE 6
COMPUTER SCIENCE NOTES FOR FIRST PUC
#include<iostream.h>
#include<conio.h>
void main()
{
int s;
clrscr();
int Square(void);
s=Square();
cout<<”Sum of Square of natural number=”<<s;
getch();
}
int Square() // Function header
{
int total=0,i,n;
cout<<”Enter the value of N”;
cin>>n;
for(i=1;i<=n;i++)
total=total+i*i;
return(total);
}
PAGE 7
COMPUTER SCIENCE NOTES FOR FIRST PUC
Example:
Program
to find the
sum of 2
number
using
function
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,result;
clrscr();
int Add(int,int);
cout<<”Enter 2 numbers”;
cin>>a>>b;
result=Add(a,b);
cout<<”Sum=”<<result;
getch();
}
int Add(int x,int y) // Function header
{
return(x+y);
}
RECURSIONS.
“Recursion is a process by which a function calls itself repeatedly, until some
specified condition has been satisfied.”
The process is used for repetitive computations in which each action is stated in
terms of previous result.
In order to solve a problem recursively, two conditions must be satisfied.
Base case: The problem must be written in recursive form.
Recursive case: The problem statement must include a stopping condition.
CALL BY VALUE.
The call by value method of passing arguments to a function copies the actual value
of an argument into the formal parameter of the function.
While passing parameter using call by value Xerox copy of original parameter is
created and passed to the function.
Any changes made to argument inside the function the function have no efforts on
the arguments and if the value of formal parameters changes in the function, value
of actual parameters are not changed, this way of passing parameter is called as
pass by value.
Example: Program to Swap 2 numbers using Call by value method.
#include<iostream.h>
#include<conio.h>
void main()
{
void Swap(int,int);
inta,b;
After execution of this program, the first cout() function prints the value
cout<<”Enter two number”;
a=5,b=10,then it calls the function Swap(a,b),In the function swap
cin>>a>>b;
Swap(a,b),the values of a and b are interchanged. So in the Swap(a,b),the
cout<<”Before Calling function a=”<<a<<” and b=”<<b;
cout() function prints a=10,b=5,then the control will transferred to main()
Swap(a,b);
functionCalling
cout<<”After and the cout a=”<<a<<”
function function prints
and the value a=5 and b=10
b=”<<b;
This indicates that, in call by value, whatever modifications done to the
getch();
}
CALL BY REFERENCES.
Sometimes we need to change the values of the actual arguments in the calling
function. This is not possible in the pass by value method.
C++ allows us to pass parameters to the function by using reference variables. When
we pass arguments by reference, the formal arguments in the called function
become the aliases to the actual arguments of the calling function. i.e., the called
function is actually uses the original data with a different name.
When a function invokes formal argument became reference to the actual
arguments.
In this method called function does not create a copy of original vale instead it refer
to the original value and any changes made are applicable in original data.
#include<iostream.h>
#include<conio.h>
void main()
{
void Swap(int &,int &);
int a,b;
cout<<”Enter two number”;
cin>>a>>b;
cout<<”Before Calling function a=”<<a<<” and b=”<<b;
Swap(a,b);
cout<<”After Calling function a=”<<a<<” and b=”<<b;
getch();
}
PAGE 11