Saturday 11 February 2012

functions in C and C++

Function


Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C
Generally functioms are classified into standard functions and user defined functions. the standard  functions are also called library functions or built in functions.all standard functions,such as sqrt(), abs(), log(), sin(), pow() etc. are provided in the library of the fuctions.these are selective.but, most of the applications need other functions than those available in software.these functions must be written by the user(programmer),hence,the name user definedfunctions.


Definition of function
A function is a group of statements that is executed when it is called from some point of the program. The following is its format:
Defining a function means writing a actual code of a function which does a specific and identifiable task suppose you are defining a function which  compute the square of a given number. Then you have to write a set of instructions to do this there are different methods to define functions. The following two ways are are used in defining functions:-
NON ANSI style
ANSI style
The general form of function declaration in non ANSI style
Type_of_return_value name_of_function();
Example:


#include<stdio.h>
main()
{
Char ch, ct;
Char char_is();
Printf(“enter a character\n”);
Ch=getchar();
Ct=char_is(ch);
If(ct==’a’)
Printf(“it is an alphabet\n”);
else if(ct==’d’)
printf(“it is a digit\n”);
else if(ct==’x’)
printf(“it is a special character\n”);
}
Char char_is(cyar)
Char cyar;
{
If((cyar>=’a’ &&cyar<=’z’)||(cyar>=’a’ && cyar<=’z’))
Return(‘a’);
Else if(cyar>=’0’ &&cyar<=’9’)
Return(‘d’);
Else
Return(‘x’);
}


Output of the program
Enter the character
f
It is an alphabet

The ANSI style of function declaration differs with the non ANSI style only in the comments on the type of the variable received from the calling function.that is the pair of the parenthesis is empty in non-ANSI style, while the ANSI style contains the variables and their datatypes with in the parenthesis.
The general form of ANSI function  declaration  is as follows:
Type_of_return_value name_of_function( dt1 p1, dt2 p2 , dt3 p3………)
Where
dt1,dt2,dt3,……..dtn   are data types of (formal parameters)
p1,p2,p3,…..pn  are formal parameters.
Example:

#include<stdio.h>
#include<math.h>
main()
{
Int m,n,temp,I,rem;
Printf(“enter the values of m and n\n”)
Scanf(“%d %d”,&m, &n);
If(m>n)
{
temp=m;
m=n;
n=temp;
}
rem=(m%2);
if(rem==0)
m++;
printf(“prime numbers between %d and %d are …\n”,m,n);
for(i=m;i<=n;i+=2)
{
If(isprime(i))
Printf(“%d\n”,i);


}
}
Int isprime(int k)
{
int limit, td, remain;
remain=k%2;
if(remain)
{
Td=3;
Limit=sqrt(k);
While(remain &&(td<=limit))
{
Remainder=k%td;
td+ =2;
}
return(remain);
}


 Output of the program will be:
Enter the values of m and n
15
45
The prime numbers between m and n are……………
17
19
23
29
31
37
41
43

Parameter passing method:
There are two methods of parameters passing
1)call by value
2)call by reference


Call by value:
When the values of arguments are passed from the calling function to be called function, the values are copied into the called function, there is no change in the original values with in the calling function.

Call by reference:
In this method, the actual value are not passed, instead their addresses are passed. There is no copying of values since their memory locations are referenced.if any modification is made to the values in the called function, the original value get changed with in the calling function. Passing of addresses  require the knowledge of pointers

No comments:

Post a Comment