Function:
- A function is a set of instructions that are used to perform specified tasks that repeatedly occur in the main program.
- It helps to avoid duplication of work and also breaks down a large program into a number of small functions.
- It is used to divide the complex task into a manageable task.
- Functions are classified into two types as shown below,
* User-defined functions
* Pre-defined functions
* Pre-defined functions
1.User-defined function:
- The users have full scope to implement their own ideas in the function.
- It defined by the users according to their requirements are called user-defined functions.
- Elements of user-defined function:
* Function definition
* Function call
Syntax of user-defined function:
<Header_files>
Main_function()
{
variable_declaration;
//function declaration part
return_type or data_type function_name(parameters list);
// function calling part
function_name();//no parameter passed
or
function_name(parameter);//parameter passed
or
return value=function_name(parameter);
//printing statements
}
//function definition part
Data_type function_name(parameters list)
{
Local variable declaration;
......
Body of the function;
return (expression);
}
<Header_files>
Main_function()
{
variable_declaration;
//function declaration part
return_type or data_type function_name(parameters list);
// function calling part
function_name();//no parameter passed
or
function_name(parameter);//parameter passed
or
return value=function_name(parameter);
//printing statements
}
//function definition part
Data_type function_name(parameters list)
{
Local variable declaration;
......
Body of the function;
return (expression);
}
Parameters:
- It provides data communication between the calling function and the called function.
- There are two types of parameters,
(i). Actual parameters:
- Used in data transferred from the main function to the user-defined function.
(ii). Formal parameters:
- Used in data transferred from a user-defined function into the main function.
For example:
Where
a, b are the actual parameters.
x,y is the formal parameters.
Local and global variables:
Local variable:
- The local variables are defined within the body of the function.
- This variable used within the function only, other functions can not access these variables.
Example:
fun1(int a, int b)
{
int c,d;
}
fun1(int a, int b)
{
int c,d;
}
Global variable:
- The global variables are defined outside the main () function, multiple functions can use these variables.
Example:
int x,y=10;
main()
{
int a,b;
}
int x,y=10;
main()
{
int a,b;
}
Example program for local and global variables.
#include<iostream.h>
#include<conio.h>
int i; //Global variable
void main()
{
int j; //Local variable
void fun1();//Function declaration
i=0;
clrscr();
cout<<"\n Value of i in main function: "<<i;
fun1();//Function calling
cout<<"\n Value of i in after call: "<<i;
getch();
}
//Function definition
void fun1()
{
int k; //Local variable for fun1()
i=50;
}
#include<iostream.h>
#include<conio.h>
int i; //Global variable
void main()
{
int j; //Local variable
void fun1();//Function declaration
i=0;
clrscr();
cout<<"\n Value of i in main function: "<<i;
fun1();//Function calling
cout<<"\n Value of i in after call: "<<i;
getch();
}
//Function definition
void fun1()
{
int k; //Local variable for fun1()
i=50;
}
Output:
The return statement:
- A return statement is an optional part of the function.
- It is used to populate the results without any printing function.
Syntax:
return; or return(expression);
return; or return(expression);
Example:
if(x>0)
return(1);
else
return(0);
if(x>0)
return(1);
else
return(0);
Function calling:
- A function can be called by specifying the function name in the source program with parameters if presence within parentheses.
- If a function is not declaring the variable to return, we must be specified the data type at the return value.
For example:
- It is divided into two types,
1.Function call without a parameter:
#include<iostream.h>
#include<conio.h>
void main()
{
msg_func();//Function definition without parameter and
called the function
cout<<"\n Message from main program or function");
getch();
}
//Function definition
msg_func()
{
clrscr();
cout<<"\n Message from msg_func() user defined function";
}
#include<iostream.h>
#include<conio.h>
void main()
{
msg_func();//Function definition without parameter and
called the function
cout<<"\n Message from main program or function");
getch();
}
//Function definition
msg_func()
{
clrscr();
cout<<"\n Message from msg_func() user defined function";
}
Output:
2.Function call with parameter:
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=msg_func(10,20);//Function definition with parameter and
called the function
cout<<"\n Message from main program or function";
cout<<"\n The value of a is : "<<a;
getch();
}
//Function definition
int msg_func(int x,int y)
{
int z;
z=x+y;
cout<<"\n Message from msg_func() user defined function";
return(z);
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=msg_func(10,20);//Function definition with parameter and
called the function
cout<<"\n Message from main program or function";
cout<<"\n The value of a is : "<<a;
getch();
}
//Function definition
int msg_func(int x,int y)
{
int z;
z=x+y;
cout<<"\n Message from msg_func() user defined function";
return(z);
}
Output:
No comments:
Post a Comment