Functions
Functions
getch();
}
// Function definition
void add()
{
int a=5,b=7,s;
s=a+b;
printf("SUM=%d",s);
}
Output:
Program:
// Functions without return type and without arguments
#include<stdio.h>
// Function declarations
void add();
void subtract();
void main()
{
clrscr();
// Function calls
add();
subtract();
add();
getch();
}
// Function definitions
void add()
{
int a=5,b=7,s;
s=a+b;
printf("SUM=%d\n",s);
}
void subtract()
{
int x=9,y=5;
printf("Diff=%d\n",x-y);
}
Output:
or
int find()
{
int a=4,b=5;
return(a+b);--->returned to function call
// This is also correct: return a+b;
}
Program:
// Function with return type but without arguments
#include<stdio.h>
int add(); // Function declaration
void main()
{
int sum;
clrscr();
sum=add(); // Function call
printf("SUM=%d",sum);
getch();
}
int add() // Function definition
{
int x=8,y=5,s;
s=x+y;
return s; // This is also correct: return(s);
}
/*
This also works:
int add()
{
int x=8,y=5;
return(x+y);
}
*/
Output:
Program:
// Function with return type but without arguments
#include<stdio.h>
// Function declarations
int add();
float product();
void main()
{
clrscr();
printf("SUM=%d\n",add()); // Function call
printf("Product=%f",product()); Function call
getch();
}
// Function definitions
int add()
{
int x=8,y=5;
return(x+y); // This is also correct: return x+y;
}
float product()
{
float x,y;
printf("Enter x and y:");
scanf("%f%f",&x,&y);
return x*y;
}
Output:
or
void find(int x,int y)
{
printf(“SUM=%d”,x+y);
}
Program:
// Functions without return type but with arguments
#include<stdio.h>
void add(int x,int y); // Function declaration
void main()
{
clrscr();
add(9,7); // Function call(When ever we call a function control transfers to
// the function definition)
getch();
}
// Function definition
void add(int x,int y) // 9 is pushed into x and 7 is passed into y
{
int s;
s=x+y;
printf("SUM=%d",s);
}
Output:
Program:
// Functions without return type but with arguments
#include<stdio.h>
// Function declarations
void add(int x,int y,int z);
void product(float x,float y);
void main()
{
int a=4,b=8,c=5;
float x,y;
clrscr();
// Function calls
add(a,b,c);
printf("Enter a,b:");
scanf("%f%f",&x,&y);
product(x,y);
getch();
}
// Function definitions
void add(int x,int y,int z)
{
int s;
s=x+y+z;
printf("SUM=%d\n",s);
}
void product(float x,float y)
{
printf("Diff=%f",x-y);
}
Output:
Program:
// Functions without return type but with arguments
#include<stdio.h>
void add(int x,int y,int z); // Function declaration
void main()
{
int a=4,b=8,c=5;
clrscr();
add(a,b,c); // Function call
getch();
}
// Function definitions
void add(int x,int y,int z)
{
int s;
s=x+y+z;
printf("SUM=%d\n",s);
}
Output:
4. Functions with arguments and with return type:
Function declaration:
Syntax:
return_type function_name(list of parameters);
Example:
int find(int x,int y);
Function call:
Syntax:
function_name(list of arguments);
Note: When we call a function any thing given in the brackets can be called as
arguments. At function declaration and function definition we call them as
parameters.
Example:
find(7,9);
Function definition:
Syntax:
int function_name(list of parameters)
{
---
---
}
Example:
int find(int x,int y)
{
int sum;
sum=x+y;
return(sum); // This is also correct: return sum;
}
or
int find(int x,int y)
{
return(x+y); // This is also correct: return x+y;
}
Program:
// Function with return type and with arguments
#include<stdio.h>
int add(int x,int y); // Function declaration
void main()
{
int x=20,y=10;
clrscr();
printf("SUM=%d",add(x,y)); // Function call(x and y are the arguments passed
// to function definition)
getch();
}
int add(int x,int y) // Function definition
{
return x+y;
}
Output:
Program:
// Function with return type and with arguments
#include<stdio.h>
#include<math.h>
// Function declarations
int add(int x,int y);
float sroot(float x);
void main()
{
int x=20,y=10;
float a=9.0;
clrscr();
printf("SUM=%d\n",add(x,y)); // Function call
printf("SQ.ROOT=%f",sroot(a)); // Function call
getch();
}
// Function definitions
int add(int x,int y)
{
return x+y;
}
float sroot(float x)
{
/*
This is also correct:
float sr;
sr=sqrt(x); // sqrt() is a predefined function that finds square root of a
number return sr;
*/
return sqrt(x);
}
Output: