functions

Functions

http://install4install.com

A function is a group of statements that performs some specific task.They can be used in other programs depending on their accessibility and are ended by brackets.In order to define a function inside  a program we need to specify three things:

  • Function Declaration
  • Function Definition
  • Function Calling

Some functions are pre-defined like main(),printf(),scanf().We just need to call these type of functions in our program as their definition and declaration are  already stored in other files.The purpose of main() is to start the execution of program.

Function Declaration

Functions are declared at the top of program in which function call has been placed.It is required to be declared when we define a function in one program and call it another program.It  includes return type,function name and parameters.It tells the compiler how to call the function.

Syntax

return_type function_name(parameter 1, parameter 2,parameter 3,….parameter n);

return_type function_name();

  • return_type is the data type of function.It can be void,int,float or char.
  • function_name is the name of function.
  • parameters are the arguments passed to the function.Parameter has return type and parameter name.Return type is compulsory to mention whereas parameter name is optional in this function declaration.

Example

float area_circle(int r)

or

float area_circle(int)

Function Definition

Function definition defines the task/purpose of the function.The purpose is defined in the body of function.

Syntax

return_type function_name(parameters)

{

body_of_function

}

  •  return_type is the data type of function
  •  function_name is the name of function.
  • parameters are the arguments passed to the function.They are optional   in definition.
  • body_of_function defines the purpose/task of function.

Example

float area_circle(int r)
{
float area;
area =3.14 *r*r;
return area;
}

Function Calling

To use a function and return results function calling is used.When the control in program reaches the function calling,it gets transferred to function definition and the required task is performed.When return statement or function ending close brace is encountered control gets transferred back to the main program.To call a function simply function name along with arguments is required.The arguments are passed in function calling according to the declaration/definition.There is no need to mention return_type in function calling.

Syntax

function_name(arguments);

 Example

area_circle(5);

Program 1

#include <stdio.h>

float area_circle(int r);//Function Declaration
int main() //main function
{
	float result;
	result = area_circle(5);//Function calling
	printf("Area of circle is %f",result);
	return 0;
}
float area_circle(int r)//Function Definition
{
	float area;
	area = 3.14 *r *r;
	return area;
}

Run

 

Program 2

#include <stdio.h>
int largest(int,int,int);
int main() 
{
int big;// int p=2,q=1,r=7;
big = largest(2,1,7);//largest = (p,q,r);
printf("The largest no is %d",big);
return 0;
}
int largest(int a,int b,int c)
{
if((a>b) && (a>c))
	{
	return a;
	}
else if((b>a && b>c))
	{
   return b;
	}
else
	{
	return c;
	}

}
	
	

Run

 

 

Program 3

#include <stdio.h>

float area_rectangle(float,float);
float area_triangle(float,float);
float area_square(float);
int main()
{
	float res1,res2,res3;
	res1 = area_rectangle(3.5,6);
	res2 = area_triangle(4.5,7);
	res3 = area_square(3);
	printf("The area of rectangle is %f\n",res1);
	printf("The area of triangle is %f\n",res2);
	printf("The area of square is %f\n",res3);
	return 0;
}
float area_rectangle(float length,float breadth)//Function definition
{
	float area;
	area = length * breadth;
	return area;
}
float area_triangle(float base,float height)
{
 float area; area = 0.5 * base * height; 
 return area; 
}

 float area_square(float side)
 {
 float area; 
 area = side * side; 
 return area; 
}

Run

 

 


Short URL: http://tinyit.cc/8e898
By Cusp2207 on July 1, 2014 | Computer Science | A comment?
Tags: