Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

User-Defined Functions I Puc

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

COMPUTER SCIENCE NOTES FOR FIRST PUC

CHAPTER 13: USER DEFINED FUNCTION


1. INTRODUCTION.
 A program size is depending on type of problem to be solved if a given problem is
larger and complex more number of lines of code are need and occupies more space
and require more time to execute.
 Even testing and debugging also becomes difficult to overcome all these difficulties
there is a need of fragmenting a program into smaller program so larger program are
divided into subprogram to reduce the complexity for maintenance and
understanding so user defined function are defined to fulfill the specific needs.

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.

2. ADVANTAGES OF USER DEFINED FUNCTION.


 Makes program more readable and easy to understand.
 Avoids the repletion of program segment because it make code reusable.
 Maintenance is easier due to their smaller size.
 If there is a set of statement to be replaced several times in the program the
statement can be replaced as function whenever and wherever required.
 If function are developed in a program in a program the number of lines decreases.
 Debugging and testing is easier.

3. FUNCTION DEFINITION OR STRUCTURE OF USER-DEFINED


FUNCTION.

 Any user-defined function has the following structure.

 General structure of a function has 2 parts namely


A. HEADER OF A FUNCTION/ FUNCTION HEADER: It contain
 RETURN TYPE: It is a data type of the value return by the function to another
function when it is called return type can be char, int, float or void. When void is
used when the function return no value to calling function.
 FUNCTION NAME: It is the name of the function. It is an identifier to identify the
particular function in a program.
 ARGUMENT LIST WITH DECLARATION: It is the list of arguments or
parameters or variables with their declaration. Each argument should be

PAGE 1
COMPUTER SCIENCE NOTES FOR FIRST PUC

declared separately. Each declaration should be separated by comma. The list


should be enclosed within parenthesis.
B. BODY OF FUNCTION: It contain
 LOCAL VARIABLE DECLARATION: It is the declaration of the variables that are
used within the function. Since these variables are local to the function, these
variables are called as local variables.
 EXECUTABLE-STATEMENTS: This are the statements that perform the
necessary operations to solve the problem. If the function is returning a value, a
return statement should be included. Otherwise, return statement is not
necessary.
 RETURN (): Return is a resultant value to a calling function.

SAMPLE PROGRAM TO FIND SUM OF TWO NUMBER USING FUNCTION

4. CALLING A FUNCTION OR FUNCTION CALL.


 The definition of the program can come either before the main () function or after the
main () function.
 DEFINITION: A function can be called by specifying its name, followed by list of
arguments enclosed within the parenthesis. The arguments should be separated by
commas. If the function does not pass any arguments, then an empty pair of
parenthesis should follow the name of the function.
 SYNTAX: variable= functionname(Argument list);
OR
variable= functionname();
 EXAMPLE: result= Sum(a,b)

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.

6. FUNCTION PROTOTYPE OR DECLARATION.


 The declaration of function header before calling a function is called function
prototype.
 A function prototype is a declaration of the function that tells the program about the
type of the value return by the function and the number and type of arguments.
 SYNTAX: return type functionname(Argument list);
 EXAMPLE: int Sum(int, int)
 The function prototype describes the function interface to the compiler.
 The function prototype gives details such as name of the function to be called,
number of arguments, data type of arguments and data type of the returning value.
 With function prototyping, a template is created when declaring and defining a
function. When a function is called, the compiler checks the template to ensure that
proper arguments are passed and correct type of value is returned.
 Any violation in matching the arguments or the return type will be caught by the
compiler at the time of compilation.

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.

 Number of actual parameter will be equivalent to number of formal parameters.

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;
}

8. TYPES OF USER DEFINED FUNCTIONS:


 Different user defined function are as follows
 Function with no arguments and no return value.
 Function with argument and no return value.
 Function with no argument and return value.
 Function with argument and return value
 Recursions.

FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUE.

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.

Example: Program to find the average of 2 numbers with no arguments and no


return values.

#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;
}

FUNCTION WITH ARGUMENT AND NO RETURN VALUE.


 In this category, there is data transfer from the calling function to the called function
using the arguments.
 But, there is no data transfer from the called function to the calling function. When
arguments are passed, the function can receive values from the calling function.
 When the function does not return a value, the calling function cannot receive any
value from the called function.

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”;
}

FUNCTION WITH NO ARGUMENT AND RETURN VALUE.


 In this category, there is data transfer between the calling function and called
function. When parameters are passed, the called function can receive values from
the calling function.
 When the function returns a value, the calling function can receive a value from the
called function.

PAGE 6
COMPUTER SCIENCE NOTES FOR FIRST PUC

Example: Program to find the sum of square of natural number upto N

#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);
}

FUNCTION WITH ARGUMENT AND RETURN VALUE


 In this category, there is data transfer between the calling function and called
function. When parameters are passed, the called function can receive values from
the calling function.
 When the function returns a value, the calling function can receive a value from the
called function.

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.

Example: Program to find the factorial of given number


#include<iostream.h>
#include<conio.h>
int Fact(int); //FUNCTION PROTOTYPE
void main()
{
int n;
clrscr();
PAGE 8
cout<<”Enter a number”;
cin>>n;
COMPUTER SCIENCE NOTES FOR FIRST PUC

9. PASSING ARGUMENT IN FUNCTIONS.


 In C++ programming we have different ways of passing parameter such as
 Call by value OR Pass by value.
 Call by references OR Pass by references.

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);
inta,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();
}

void Swap(int p,int q)


{
int temp;
temp=p; PAGE 9
p=q;
q=temp;
COMPUTER SCIENCE NOTES FOR FIRST PUC

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();
}

void Swap(int &p,int &q)


{
int temp;
temp=p;
p=q;
q=temp; PAGE 10
}
COMPUTER SCIENCE NOTES FOR FIRST PUC

DIFFERENCES BETWEEN CALL BY VALUE AND CALL BY


REFERENCES.
CALL BY VALUE CALL BY REFERENCES.
When a function is called the values of variables are When a function is called the address of variables
passed. are passed.
The types of formal parameter should be same as The types of formal parameter should be same as
type of actual parameter. type of actual parameter, but they have to be
declared as a pointers.
Formal parameter contains the values of actual Formal parameter contains the address of actual
parameters. parameters.
Execution is slower since all the values are copied Execution is faster since only the address are
into formal parameters. copied.

PAGE 11

You might also like