Union in C
Union in C
int absolute(int x)
{
if (x >= 0)
return x;
else
return -x;
}
Function Call
Example:
int distance = absolute(-5);
• The result of a function call is a value of type <return_type>
How Function works in C
void main()
{
….. …..
….. …..
function_name();
….. …..
}
return_type function_name()
{
….. …..
….. …..
return value;
}
C-program using function to find the area of
rectangle
#include <stdio.h>
// Function to calculate the area of a rectangle
float calculateArea(float length, float width)
{
return length * width; // return the product of length and width
}
int main() {
float length, width, area;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");OUTPUT:
scanf("%f", &width); Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
The area of the rectangle is: 15.00
// Calculate the area
area = calculateArea(length, width);
#include<stdio.h>
#include<math.h>
int main()
{
int main()
{
float f = 22;
printf("Temperature in Celsius : %0.2f",fahrenheit_to_Celsius(f));
return 0;
}
Function Prototype
• The function prototype declares the input and output
parameters of the function.
void sum_of_series();
void main()
{
//int n;
sum_of_series();
}
void sum_of_series()
{
int i,x,sum=0;
printf(“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
sum+=i;
printf(“Sum of series is: %d”,sum);
}
Input: Output:
Enter the value of n 5 Sum of series is: 15
2. Function without arguments and with
return value
return_type function_name();
void main()
{
….. …..
….. …..
var=function_name();
….. …..
}
Return_type function_name()
{
….. …..
….. …..
return value;
}
Example:
Int sum_of_series();
void main()
{
int result;
result=sum_of_series();
printf(“Sum of series is: %d”,result);
}
Int sum_of_series()
{
int i,x,sum=0;
printf(“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
sum+=i;
return(sum);
}
Input: Output:
Enter the value of n 5 Sum of series is: 15
3. Function with arguments and without
return value
Int sum_of_series(int);
void main()
{
int n,result;
printf(“Enter the value of n”);
scanf(“%d”,&n);
result=sum_of_series(n);
printf(“Sum of series is: %d”,result);
}
int sum_of_series(int x)
{
int i,sum=0;
for(i=1;i<=n;i++)
sum+=i;
return(sum);
}
Input: Output:
Pass by value
• Actual argument passed to the formal argument.
• Any changes to the formal argument does not affect the actual
argument.
N.Rajkumar 23
Example
#include <stdio.h>
int add(int,int);
int main()
{
int a,b,c;
clrscr();
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("\nSum is:%d",c);
}
int add(int x,int y)
{
int z;
z=x+y;
return(z);
}
N.Rajkumar 24
Output
Sum is:13
N.Rajkumar 25
Pass by reference
• Instead of passing value, the address of the argument will be passed.
• Any changes to the formal argument will affect the actual argument.
N.Rajkumar 26
Example
#include <stdio.h>
swap(&x,&y);
#include<conio.h> printf("\nx=%d,y=%d",x,y);
void swap(int*,int*); }
void main() void swap(int *a,int *b)
{
{ int c;
int x,y; c=*a;
printf("\nEnter value of x:"); *a=*b;
*b=c;
scanf("%d",&x);
printf("\na=%d,b=%d",*a,*b);
printf("\nEnter value of y:"); }
scanf("%d",&y);
Output
Enter value of x:5
Enter value of y:6
a=6,b=5
x=6,y=5
N.Rajkumar 27
Swapping the values of the two variables
Pass by Value Pass by Reference
#include <stdio.h> #include <stdio.h>
int fact(int n)
{
if(n==0 || n==1)
return 1;
else
return(n*fact(n-1));
}
Example:
void main()
{
int n,fact1;
printf(“Enter the value of n”);
scanf(“%d”,&n);
fact1=fact(n);
printf(“The factorial of %d is: %d”,n,fact1);
}
int fact(int x)
{
if(x==0 || x==1)
return 1;
else
return(x*fact(x-1));
}
Input: Output:
Enter the value of n 5 The factorial of 5 is: 120
Program to find sum of digits using recursion
/**
#include <stdio.h>
* Recursive function to find sum of digits of a
number
/* Function declaration */ */
int sumOfDigits(int num)
int sumOfDigits(int num);
{
// Base condition
int main()
{ if(num == 0)
int num, sum; return 0;
printf("Enter any number to find sum of digits: "); return ((num % 10) + sumOfDigits(num / 10));
scanf("%d", &num); }
sum = sumOfDigits(num);