C Function Type
There
are 4 type of function in c which are known as user-define function in c.
1.no
argument pass and no return value
2.argument
pass but no return value
3.no
argument but a return value
4.argument
pass and a return value
Fig: C function type |
1.
No argument pass and no return value
When a function has no arguments, it does not receive any
data from the calling function. Similarly when it does not return a value, the
calling function does not receive any data from the called function.
Syntax :
Function declaration:
void function();
Function call:
function();
Function definition :
void function()
{
Statement;
}
Program
//code of no argument and no return value
#include <stdio.h> void value(void); void main() { value(); } void value(void) { int year =
1, period = 5, amount = 5000, inrate = 0.12; float sum;
sum =
amount; while
(year <= period) { sum
= sum * (1 + inrate); year
= year + 1; } printf("
The total amount is %f:", sum); } |
Output:
The total amount is 5000.000000
2.
Function with arguments but no return value
When a
function has arguments, it receive any data from the calling function but it
returns no values.
Syntax :
Function declaration:
void function( int );
Function call:
function(x);
Function definition :
void function(int x)
{
Statement;
}
Program
//
with argument but no return value
#include <stdio.h>
void
function(int, int[], char[]);
int
main()
{
int a = 20;
int ar[5] = { 10, 20, 30, 40, 50 };
char str[30] = "dwivedicyberdairy";
function(a, &ar[0], &str[0]);
return 0;
}
void
function(int a, int* ar, char* str)
{
int i;
printf("value of a is
%d\n\n", a);
for (i = 0; i < 5; i++) {
printf("value of ar[%d]
is %d\n", i, ar[i]);
}
printf("\nvalue of str is
%s\n", str);
}
Output:
Value
of a is 20
Value
of ar[0] is 10
Value
of ar[1] is 20
Value
of ar[2] is 30
Value
of ar[3] is 40
Value
of ar[4] is 50
The
given string is : dwivedicyberdairy
3. Function with no arguments but returns a value
There could be occasions where we may need to design
functions that may not take any arguments but returns a value to the calling
function. A example for this is getchar function it has no parameters but it
returns an integer an integer type data that represents a character.
Syntax :
Function declaration:
int function( );
Function call:
function();
Function definition :
int function()
{
Statement;
return x;
}
Program
//
C code for function with no arguments
//
but have return value
#include
<math.h>
#include <stdio.h>
int
sum();
int
main()
{
int num;
num = sum();
printf("\nSum of two given values
= %d", num);
return 0;
}
int
sum()
{
int a = 50, b = 80, sum;
sum = sqrt(a) + sqrt(b);
return sum;
}
Output:
Sum
of two given values=16
4.
4.Function with arguments and return value
Syntax :
Function declaration:
int function( int );
Function call:
function(x);
Function definition :
int function(int x)
{
Statement;
return x;
}
Program
/ /
C code for function with arguments
// and with return value
#include
<stdio.h>
#include
<string.h>
int
function(int, int[]);
int
main()
{
int i, a = 20;
int arr[5] = { 10, 20, 30, 40, 50 };
a = function(a, &arr[0]);
printf("value of a is %d\n",
a);
for (i = 0; i < 5; i++) {
printf("value of arr[%d]
is %d\n", i, arr[i]);
}
return 0;
}
int
function(int a, int* arr)
{
int i;
a = a + 20;
arr[0] = arr[0] + 50;
arr[1] = arr[1] + 50;
arr[2] = arr[2] + 50;
arr[3] = arr[3] + 50;
arr[4] = arr[4] + 50;
return a;
}
Output:
Value
of a is 40
Value
of arr[0] is 60
Value
of arr[1] is 70
Value
of arr[2] is 80
Value
of arr[3] is 90
Value of arr[4] is 100
No comments:
Post a Comment