Professional Documents
Culture Documents
Categories of User Defined Function iii)Function with Arguments and Return Values
#include<stdio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
int square(int); // Function Declaration {
int num,result; void swap(int a,int b); // function declaration
printf("Enter a number"); int a,b;
scanf("%d",&num); printf("enter two values\n");
result=square(num); //Function Calling scanf("%d%d",&a,&b);
printf("Square is:%d",result); printf("entered values are a=%d and b=%d",a,b);
} swap(a,b); // function calling
int square(int x) //Function Definition getch();
{ }
int j; void swap(int x, int y) // function definition
j=x*x; {
return j; int z;
} z=x,
x=y;
Input- Enter a number: 5 y=z;
Output- square is: 25 printf("\n exchanged values are a=%d b=
++++++++++++++++++++++++++++++ %d",x,y);
Recursive Function-
Que1 A recursive function for calculating the }
factorial of an integer.
#include<stdio.h> Input- Enter two values: 10
int fact(int); 30
void main() Output- entered values are a=10 and b=30
{ exchanged values are a=30 b=10
int x,y; ++++++++++++++++++++++++++++++
printf("Enter number:");
scanf("%d",&x);
y=fact(x);
printf("%d",y);
}
int fact(int n)
{
if(n==0)
return (1);
else
return(n*fact(n-1));
}
Array
#include<stdio.h>
#include<conio.h>
void main()
{
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i,sum;
float avg;
sum = 0;
//calculation process
for(i=0;i<10;i++)
{
sum=sum+array[i];
}
avg=(float)sum/10;
Output-