Sunday 8 March 2015

Function Definition in C


The definition of a function is the actual body of the function. It starts with function header, which is the same as the function prototype but does not end with a semicolon. The function prototype and the definition must have exactly the same return type, name and parameter list. If they do not match the compiler will issue error messages during compilation. Function definition takes the form:

Return-type function-name(type argement-1, type argument-2, ….. )
{
Statement(s);
}

Consider the following function, which accepts an integer as the input and returns its square.

int square(int x)
{
int y;
y = x*x;
return y;
}

Whenever you want to call this function you can include a statement like the following in the program.

b = square(a);

Here, the value of variable “a” is passed to the function, where it is received by the function as the value of variable “x”. The function returns the square as the value of “y”. The variable “b” in the calling function receives the return value.

Example 7.1 – Write a C program to calculate the circumference and area of a circle given its radius. Implement calculation of circumference and areas as separate functions.

/*Program-7.1 */

#include <stdio.h>

const float pi = 3.141;
float area(float r); //function prototype
float circumference(float r); //function prototype

int main()
{
float radius;
printf("Enter radius: ");
scanf("%f",&radius); //read radius

printf("\nArea : %.2f", area(radius));
printf("\nCircumference : %.2f", circumference(radius));
return 0;
}
/* Function computes the area of a circle given its radius*/
float area(float r)
{
return (pi*r*r);
}
/* Function computes the circumference of a circle given radius*/
float circumference(float r)
{
return (2*pi*r);
}
It is also possible to define several functions with the same name but with different parameters. As
given below:

float area(int r);
float area(float r);

Both these functions calculate the area of a circle given the radius. However the first function accepts an integer as the input while the second function accepts a floating point number. Based on the type of the given input the program dynamically calls the correct function. Consider the following program:

/*Program-7.2 */
#include <stdio.h>
const float pi = 3.141; //define pi
void area(float r); // function prototype
void area(int r); // function prototype
int main()
{
int radius;
printf("Enter radius: ");
scanf("%d",&radius);
area(radius);
area((float)radius); //convert to float
return 0;
}
void area(float r)
{
printf("\nFloating point input");
printf("\nArea is: %.2f", (pi*r*r));
}
void area(int r)
{
printf("\nInteger input");
printf("\nArea is: %.2f", (pi*r*r));
}
Execution of Program-7.2 displays:
Enter radius: 1
Integer input
Area is: 3.14
Floating point input
Area is: 3.14


Program-7.2 executes each function based on the given input. The program accepts an integer from the keyboard and when the expression area(radius); is executed it calls the function which accepts and integer as the input parameter and it display the message “Integer input” and the area. The expression area((float)radius); converts the integer value to a floating point number (this process is called casting) before calling the function. When it is called since the input is a floating point value it executes the function which displays the message “Floating point input”. Although you can have multiple functions with the same name but with different input parameters, you cannot have functions with the same name with a different output parameter. It is also possible to have the same name for functions with different behavior with different input parameters, however best practices suggest that no two functions should have the same name unless they perform identical tasks.


No comments:

Post a Comment