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:
Programming with C and C Sharp
INTRODUCTION TO C PROGRAMMING
Programming with C an introduction part two
Data types for C programming
C PROGRAMMING CHARACTER SET
CONSTANTS IN C PROGRAMMING
PROGRAMMING C VARIABLES
C PROGRAM INSTRUCTIONS
COMPILATION AND EXECUTION OF C PROGRAM
C PROGRAMMING RULES PART ONE
C PROGRAMMING RULES PART TWO
COMPILATION AND EXECUTION OF C PROGRAM
INSTRUCTIONS TO WRITE C PROGRAM
ARITHMETIC INSTRUCTIONS TO WRITE C PROGRAM
CONVERSION OF CONSTANTS IN C PROGRAM
PRIORITY OF AR THEMATIC OPERATIONS IN C
OPERATORS ASSOCIATIVITY IN C
IF STATEMENT
MULTIPLE STATEMENTS IN IF
IF AND ELSE
NESTED IF AND ELSE
BREAK
CONTINUE AND DO WHILE IN C LANGUAGE
SWITCH IN C PROGRAMMING
FUNCTIONS IN C PROGRAMMING
Functions and usage in C part two
Coding in C functions