Previous | Home | Next |
Functions with no arguments and no return values.
Example of Function with no return type and no argument
#include <conio.h>
void printmsg()
{
printf ("Hello ! I Am A Function .");
}
int main()
{
printmsg();
return 0;
};
Output : Hello ! I Am A Function .
Other example
#include <conio.h>
void msg (void)
{
puts ("Hello");
};
Output : Hello
Functions with arguments and no return values.
#include <conio.h>
void sub (int a, int b)
{
int s;
clrscr();
s=a-b;
}
void main()
{
sub(45,40);
printf ("The subtraction is =",s);
}
Output : The subtraction is 5
Functions with arguments and return values.
#include <conio.h>
int addition( int, int);
main()
{
int i=1;
i = add(1, 1);
clrscr();
}
int add( int a, int b)
{
int c;
c = a + b;
return c;
};
Output : 2
Functions that return multiple values
#include <conio.h>
main()
{
int a[5]={2,4,,6,8,10};
int b[8]={1,3,5,7,9,11,13,15};
int c[10]={1,2,3,4,5,6,7,8,9,10};
clrscr();
printf ("sum of the array a : %d \n",add(a,5));
printf ("sum of the array b : %d \n",add(b,8));
printf ("sum of the array c : %d \n",add(c,10));
}
add (int arr[], int n)
{
int i, sum=0;
for (i=0;i<n; i++)
sum+=arr [i];
getch();
return sum;
}
Output :
sum of array a : 30
sum of array b : 64
sum of array c : 55
Previous | Home | Next |