Programming with C language and using Functions is done in our previous discussions. Now we are going to deal with advanced concepts of using functions in c programming.
Function Declaration and Prototypes :
Any C function by default returns an int value. Whenever a call is made to a function, the compiler assumes that this function would return a value of the type int. If a function should return a value other than an int, then it is necessary to explicitly mention so in the calling function as well as in the called function.
The following program segment illustrates how to make square( ) capable of returning a float value.
main( )
{
float square ( float ) ;
float a, b ;
printf ( "\nEnter any number " ) ;
scanf ( "%f", &a ) ;
b = square ( a ) ;
printf ( "\nSquare of %f is %f", a, b ) ;
}
float square ( float x )
{
float y ;
y = x * x ;
return ( y ) ;
}
And here is the output
Enter any number 1.5 Square of 1.5 is 2.250000
Enter any number 2.5 Square of 2.5 is 6.250000
The function square( ) must be declared in main( ) as float square ( float ) ;
This statement is often called the prototype declaration of the square( ) function. What it means is square( ) is a function that receives a float and returns a float.
Call by Value and Call by Reference
Whenever a function is called and passed something to it we have always passed the ‘values’ of variables to the called function. Such function calls are called ‘calls by value’. On calling a function we are passing values of variables to it.
Call by reference is done with pointers in C programming.
Pointer Notation Consider the declaration, int i = 3 ;
This declaration tells the C compiler to
1. Reserve space in memory to hold the integer value.
2.Associate the name i with this memory location.
3. Store the value 3 at this location.
The computer has selected memory location 65524 as the place to store the value 3. The location number 65524 is not a number to be relied upon, because some other time the computer may choose a different location for storing the value 3. The important point is, i’s address in memory is a number.TO print this address number
main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
}
The output of the above program would be: Address of i = 65524 Value of i = 3
‘&’ used in this statement is C’s ‘address of’ operator. The expression &i returns the address of the variable i, which in this case happens to be 65524. Since 65524 represents an address, there is no question of a sign being associated with it.
Hence it is printed out using %u, which is a format specifier for printing an unsigned integer.
OTHER PROGRAMMING COURSES:
Dot Net Complete Course Part one and two
ASP.NET part one and two
Programming with C and C Sharp
Interview Questions in dot net
asp.net part one part two
Software Testing Complete course part one two and Interview Questions
Thank you for visiting Programing and Testing. To get the updates as and when published, please subscribe to my blog via email or RSS FEED.You can mail me at d_vsuresh[at the rate of ]yahoo[dot]co[in] for specific feed backs and suggestions.
