Вы находитесь на странице: 1из 17

Functions:

Definition: A function is a set of instruction to carry out a specific task. It is a complete and independent logical component which is used in a program.

Function is widely classified into two types:

standard function is a built in functions. They are also called as a library functions. C++ provides a large number of functions which is defined in a particular header file. Ex:iostream.h this file provides a stream oriented i/p & o/p functions, main( ),conio.h,etc.

function which is defined by a programmer to solve the real-world programs which is beyond the standard functions. A user defined function is structured into 1. Function Prototype. 2. Call of Function. 3.Function definition.

Before

using a user defined function programmer have to define the type of the function to the compiler. A function can be defined before or after the main function. SYNTAX:
return_type function_name(arglist/parameter list) ;

Once

a function is defined a function has to be call either in the main function or in any other function. Thus they are two function, calling function and called function. Whenever a function is called control is transferred from the calling function to the called function.

main manipulation task is done in the function definition. Syntax:


return_type function name(arg list/parameters)

{
statement 1; Statement 2; | Statement n; return(expression); }

1.function

with no argument and no return

value 2.function with argument and no return values 3.function without argument and with return value 4.function with argument and with return value

In

this type of function the first function calls the second function. Since no arguments are passed to the second function ,the second function performs all the task with no return value to the first function. Calling Function No Called Function argument Syntax : Function1( ) Function2( )
{ ________ ; ________ ; function2( ); { No return value ________ ; ________ ; }

//Example program for with no return value and no argument. #include<iostream.h> #include<conio.h> void add( ); void add( ) { int a,b,sum; cout<<enter two nos\n; cin>>a>>b; sum=a+b; cout<<sum=<<sum<<endl; } void main( ) { add( ); getch( ); }

In

this type of function the first function sends the argument to the second function and the second function will not return value. Syntax: Calling Function Passes Called Function
argument Function1( arg) { ___________ ; ___________ ; Function2( arg) } Function2( arg) { ___________ ; ___________ ; } No return value

//program to demonstrate function with argument with no return value. #include<iostream.h> #include<conio.h> void add(int a,int b); void main( ) { cout<<enter a value for a & b ; cin>>a>>b; add(a,b); getch(); } void add(int a,int b) { int s; s=a+b; cout<<sum=<<s<<endl; }

In

this type of function the first function will not pass any argument to the second function but the second function returns a value to first function. Syntax:
Calling function
Function1(no arg) { ___________ ; ___________ ; Function2(no arg); } No arg

Called function
Function2(no arg) { ___________ ; ___________ ; ____________ ; return(expression); }

With return Value.

//program to demonstrate without arg and with return value. #include<iostream.h> #include<conio.h> int add( ); void main( ) { int sum; sum=add( ); cout<<sum=<<sum; getch( ); } int add( ) { int a,b,sum; cout<<enter a value for a and b \n; cin>>a>>b; sum=a+b; return sum; }

In

this type of function the first function passes argument to second function, and second function returns a value. Syntax:
Calling Function
Function1(arg) { _____________ ; _____________ ; Function2(arg); } Passes argument

Called function
Function2(arg) { _____________ ; _____________ ; return(expression); }

Returns a value

//demo program for with return type and with argument function. #include<iostream.h> #include<conio.h> int add(int a,int b); main( ) { int a,b,s; cout<<enter a value for a & b \n; cin>>a>>b; s=add(a,b); cout<<sum=<<s<<endl; getch( ); return 0; } int add(int a,int b) { int s; s=a+b; return s; }

Вам также может понравиться