Function is a group of statement that perform a particular task. C enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function in the program is supposed to perform a well-defined task. Therefore, the program code of one function is completely insulated from the other functions.
Every
function interfaces to the outside world in terms of how information is
transferred to it and how
results generated by the function are
transmitted back from it. This interface is basically specified by the function
name.
Function Components
Function Declaration / Prototype
Before
using a function, the compiler must know the number of parameters and the type
of parameters that the function expects to receive and the data type of value
that it will return to the calling program. Placing the function declaration
statement prior to its use enables the compiler to make a check on the
arguments used while calling that function. A function declaration statement
has the following syntax:
Example
- return_data_type
function_name( argument_type);
void display();
void
max(int,int);
Function Calling
The
function call statement invokes the function. When a function is invoked, the
compiler jumps to the called function to execute the statements that are a part
of that function. Once the called function is executed, the program control
passes back to the calling function. A function call statement has the
following syntax:
Example - function_name(argument); // variable , value , address
display();
max(i,j);
Function Defination
When a
function is defined, space is allocated for that function in the memory. Note
that the number of arguments and the order of arguments in the function header
must be the same as that given in the function declaration statement. The
syntax of a function definition can be given as:
Example - return_data_type
function_name(argument);
{
……….............
statements
….…...............
return(variable);
}
Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
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.
Type of Function
1. Pre-define function
2. User-defined function
Pre-define function
C standard pre-define function contain file the
standard function that can be used some function are already defined. Which are
used by programmers these function are called pre-define function. Function are
divided into 3 part mathematical, string, character function.
Example
– printf() , scanf()
Example of Pre-define function
#include<iostream.h>
#include<conio.h>
void display(){
cout<<"Hello";
}
void main(){
clrscr();
display();
getch();
}
Output
Hello
User-defined function
In c user can also define own according to their need
when a function is called control is transfer to the first in the function body
are the executed in the main program when the closing braces is uncounted.
Example
– Myfun() , Add()
#include<iostream.h>
#include<conio.h>
void display();
void main(){
clrscr();
display();
getch();
}
void display(){
cout<<"Hello";
}
Output
Hello
Function Arguments
#include<iostream.h>
#include<conio.h>
int sum(int a,int b){
int c;
c=a+b;
return c;
}
void main(){
int i,j,k;
clrscr();
cout<<"Enter value of i:";
cin>>i;
cout<<"Enter value of j:";
cin>>j;
k=sum(i,j);cout<<"Sum=" <<k;
getch();
}
Output
Enter value of i: 12
Enter value of j: 12
Sum=24
#include<iostream.h>
#include<conio.h>
float pi(){
return 3.14;
}
void main(){
int r;
float area;
clrscr();
cout<<"Enter radius:";
cin>>r;
area=pi()*r*r;
cout<<"Area=" <<area;
getch();
}
Output
Enter radius:10Area=314.000000
Call by value
The values
of the variables are passed by the calling function to the called function. In
this method, the called function creates new variables to store the value of
the arguments passed to it. Therefore, the called function uses a copy of the
actual arguments to perform its intended task. If the called function is supposed
to modify the value of the parameters passed to it, then the change will be
reflected only in the called function. In the calling function, no change will
be made to the value of the variables. This is because all the changes are made
to the copy of the variables and not to the actual variables.
#include<iostream.h>
#include<conio.h>
void display(int i){
cout<<i;
i=20;
}
void main(){
int i=5;
clrscr();
cout<<i;
display(i);
cout<<i;
getch();
}
Output
5 5 5
Call by address
When the
calling function passes arguments to the called function using the
call-by-value method,the only way to return the modified value of the argument
to the caller is explicitly using the return statement. A better option is to
pass arguments using the call-by-reference technique.In this method, we declare
the function parameters as references rather than normal variables.When this is
done, any changes made by the function to the arguments it received are also
visible in the calling function.To indicate that an argument is passed using
call by reference, an asterisk (*) is placed after the type in the parameter
list.
#include<iostream.h>
#include<conio.h>
void display(int *p){
cout<<*p;
*p=20;
}
void main(){
int i=5;
clrscr();
cout<<i;
display(&i);
cout<<i;
getch();
}
Output
5 5 20
#include<iostream.h>
#include<conio.h>
void swap(int *p,int *q){
int t;
t=*p;
*p=*q;
*q=t;
}
void main(){
int i=5,j=6;
clrscr();
cout<<"Before swap i=” <<i <<” “ <<”j=” <<j;
swap(&i,&j);
cout<<endl <<"After swap i=” <<i <<” “ <<”j=" <<j;
getch();
}
Output
Before swap i=5 j=6
After swap i=6 j=5
Recursion
It is a process in which a function call itself again
and again till the particular condition satisfies.
#include<iostream.h>
#include<conio.h>
void display(){
cout<<endl <<"display";
display();
}
void main(){
clrscr();
cout<<"main";
display();
getch();
}
Output
Main
Display
Display
Function Scope
There is 4 types of scope in c …
1. local scope
When ever a variable is
declared in a block of statement then variable and function will be called as
local variable and function.
2. Function scope
if a variable is declared
then the range of that variable is limited to in that function only that
variable can be used only in that function.
3. File scope
If a variable is declared
outside the function and block then can be used anywhere in the block or
function.
4. Class scope
0 Comments