Function receive argument

Syntax:
return-type function-name(data-type arg1, data-type arge2, data-type arg3,…..data-type argN)
Here arg stands for arguments.arg1, arge2 are as like as variable name
Example: 
function name is: example
return-type is: void
2 arguments: first argument is int type and second argument is float type.
So this function can be written as
void example(int a, float b)
{

}
Problem: Write a C program to do the following

  • Determine the area of a circle while user will input the value of radius from keyboard
  • Use a user defined function named circle. This function will receive two values while it will be called. One is the radius and another is the value of PI(3.1416)
  • circle function will return area of circle

Solution:
Let radius is always an integer number. So fist argument is int type. value of PI is fractional. So second argument is float.Area is the product of fraction and integer number. So return type should be float.Complete solution is:

#include<stdio.h>
float circle(int r, float PI)
{
    return PI*r*r;
}
int main()
{
     int radius;
     printf("Enter value of radius : ");
     scanf("%d",&radius);
     float area;
     area=circle(radius,3.1416);
     printf("Area  = %f",area);
     return 0;
}

NOTE

area=circle(radius,3.1416);
Here radius is int type data and it will be assigned to the first argument ‘int r’. 3.1416 is fraction type data and it will be assigned to ‘float PI’. So if you call circle function as follows:
area= circle(3.1416,radius); it will show error. So sequence of argument is a fact to be considered.

#include<stdio.h>
void test(int x, float y, char z) // argument sequent is int,float,char
 {
     // statement goes here
 }
int main()
{
  int a=10;
  char b='A';
  float c=25.6;
  test(a,b,c); // here sequence of passing variable for arguments
  //is: int,char,float. Wrong
   
  test(a,c,b); // right. Sequence matched here
  return 0;
}

Next Previous