Function
A function is a named unit of a group of program statements.
This unit can be invoked from other parts of the program as and when required.
Parts of user defined function
1. Function prototype(declaration)
2. Function definition
3. Function call
User Defined Function
1.Function prototype
A function prototype is a declaration of function that tells
the program about return type, name and type of arguments.
return type name argument type
int sum (int ,int )
(list of arguments)
2.Function Definition
In C++ a function must be defined before it is used anywhere
in the program. A function definition tells the compiler, what function is
doing. The general form of a function definition is as given below-
type
function -name(parameter list)
{
body of function
}
Ex.
int
sum(int x,int y)
{
int z;
z=x+y;
return z;
}
3.Invoking/calling a function
A function is called by providing the function name,
followed by parameters being sent enclosed in parentheses.
int sum
(a, b);
Q. Program to print the sum of two number using function.
#include<iostream.h>
#include<conio.h>
void main()
{
int sum(int ,int);
int a,b,c;
cout<<”Enter
two values”;
cin>>a>>b;
c=sum(a,b);
cout<<”The sum
is:”<<sum;
}
int sum(int x, int
y)
{
int z;
z=x+y;
return z;
}
Note: a,b ->actual parameter
x,y
->formal parameter
Default arguments
C++ allows us to assign default values to a function’s
parameters which are useful in case a matching argument is not passed in the
function call statement. The default value is specified at the time of function
declaration.
float
interest (float principal, int time, float rate=0.10)
Default arguments are useful in a situation where some
arguments always have the same value.
Constant argument
A function whose argument cannot modify is called constant
argument.
For instance-
length (“A
string”);
Cannot modify its argument “A string” as it is a constant
value.
int sum (const int a,
const int b);
The qualifier const in function prototype tells the compiler
that function should not modify the argument.
Possible function styles
1.void function with no arguments
2.void function with some arguments
3.Non -void function with no arguments
4.Non-void function with some arguments
Call by Value
A function can be invoked by two ways-
1. Call by Value
2. Call by Reference
In call by value method copies the actual parameters into
the formal parameters i.e. the value of
the argument remains same after calling a function.
Ex.
#include<iostream.h>
int main()
{
int change(int);
int orig=10;
cout<<”\n The
original value is”<<orig<<"\n";
cout<<”\n
Return value of function change() is over ”<<orig<<"\n";
return 0;
}
int change(int a)
{
a=20;
return a;
}
Output: The original value is 10
Return value of function change() is 20
The value after function change() is over 10
Call by Reference
In call by reference method, different mechanism is used .In
place of passing a value to the function; a reference to the original variable
is passed. Reference is the alias (a different name) for a predefined variable
i.e. the reference remains same and value being changed.
Ex.
#include<iostream.h>
#include<iostream.h>
int main()
{
void swap(int&
,int&);
int a=7,b=4;
cout<<”\n Original values are: \n”;
cout<<”a=”<<a<<”b=”<<b<<”\n”;
swap(a,b);
cout<<”\n The value after swap() are:\n”;
return ();
}
void swap(int &x, int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<”\n The
swapped values are \n”;
}
Output: The original values are:
a=7, b=4;
The swapped values are:
a=4, b=7;
The values after swap()are:
a=4,b=7
Returning values from a function
All functions except those of type void return a value. This
value is explicitly specified by the return statement.
There may be three types of functions in C++
1. Computational functions
These functions return the computed or
calculated value.
Ex. sqrt (), cos ()
2. Manipulative functions
These functions manipulate information and
return a success or failure code. Generally 0 shows success operation and any
other number denotes failure.
3. Procedural function
The function that perform an action and
have no explicit return value.
For eg.
exit ()
Scope Rule
The scope rule of a language decides in which part or parts of
the program, a particular piece of code or data item would known.
For eg. Ticket A is valid only train A not in train B.
There are four kinds of scopes in C++ -
Local, function, file, class
1.Local scope
A name declared in a
block (i.e. {...}) is local to that block and can be used only in it and the
other block contained in it. The name of formal arguments is treated as, if
they were declared in the outermost block of that function.
2.Function scope
The variables declared in the outermost block of a function
have function scope i.e. they can be accessed only in the function that
declares them. Also labels (of goto) have function scope i.e. they can’t be
used outside the function.
Global Variables
If a variable declaration appears outside of all the functions,
it is said to be global variable. A global variable is variable to all the
functions and blocks defined in the file. A global variable comes into
existence when the program execution starts and is destroyed when the program
terminates.
Ex.
#include<iostream.h>
int x,y; -> global variable(file
scope)
char ch; -> ,,
int main()
{
int a,b; ->function scope for
main()
float amt;
-> ,, (local variables of main())
void check(int);
}
void check(int i)<-local variable of check() {function scope}
{
long temp;
}
Ex.
#include<iostream.h>
#include<iostream.h>
int a=20;
int main()
{
int a=50;
cout<<::a<<’\n’<<a<<endl;
return 0;
}
Output:20
50
Calling a function with array
At some point, we may need to pass an array to a function as
a parameter. In C++, it is not possible to pass the entire block of memory
represented by an array represents to a function directly as an argument. But
what can be passed instead is its address.
To accept an array as
parameter for a function, the parameters can be declared as the array type, but
with empty brackets, omitting the actual size of array.
Ex. void procedure
(int arg[])
Ex.
double getAverage( int arr[], int size)
{
int i, sum;
double avg;
for(i=0; i<size; i++)
{
sum+= arr [i];
}
avg=double(sum)/size;
return avg;
}
This post is about User-Defined Function in C++. in the next post we will discuss the topic Array.
0 Comments