0% found this document useful (0 votes)
31 views

Union in C

Uploaded by

Srivatsan SP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Union in C

Uploaded by

Srivatsan SP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

UNIT-4

FUNCTIONS AND STRUCTURES


Introduction to Functions
• A large C program is divided into basic building blocks
called C function. C function contains set of
instructions enclosed by “{ }” which performs specific
operation in a C program.
• A complex problem is often easier to solve by dividing
it into several smaller parts, each of which can be
solved by itself.
• This is called structured programming.
• These parts are sometimes made into functions in C.
• main() then uses these functions to solve the
original problem.
Advantages of Functions
a) To improve the readability of code.

b) Improves the reusability of the code, same function


can be used in any program rather than writing the
same code from scratch.

c) Debugging of the code would be easier if you use


functions, as errors are easy to be traced.

d) Reduces the size of the code, duplicate set of


statements are replaced by function calls.
Types of functions
• C allows the use of both user-defined and pre-
defined (built-in ) functions.
• Pre-defined functions (e.g., abs, ceil, rand, sqrt,
etc.) are usually grouped into specialized libraries
(e.g., iostream, stdlib, math, etc.)
User-Defined Functions
C programs usually have the following form:
Function prototypes
A function prototype is a function declaration that specifies
the data types of its arguments in the parameter list.
Function Calling
Function definitions
A function definition provides the actual body of the
function.
Function Definition
A function definition has the following syntax:
<return-type> <function name>(<parameter list>){
<local declarations>
<sequence of statements>
}
For example: Definition of a function that computes the absolute value of an integer:

int absolute(int x)
{
if (x >= 0)
return x;
else
return -x;
}
Function Call

• A function call has the following syntax:


<function name>(<argument list>);

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);

// Display the area N.Rajkumar 9


C Program to Find Factorial of a Number using Functions

#include<stdio.h>
#include<math.h>
int main()
{

printf("Enter a Number to Find Factorial: ");


printf("\nFactorial of a Given Number is: %d ",fact());
return 0;
}
int fact()
{
int i,fact=1,n; Output :
Enter a Number to Find Factorial: 5
scanf("%d",&n);
for(i=1; i<=n; i++)
Factorial of a Given Number is: 120
{
fact=fact*i;
}
return fact;
}
Absolute Value
#include <stdio.h>
int absolute(int);// function prototype for absolute()
int main()
{
int num, answer;
printf("Enter an integer (0 to stop“);
scanf(“%d”,&num);
while (num!=0)
{
answer = absolute(num);
printf("The absolute value of %d is:“,num,answer););
scanf(“%d”,num);
}
return 0;
}
// Define a function to take absolute value of an integer
int absolute(int x)
{
C Program to Convert Fahrenheit to Celsius
using Functions
#include <stdio.h>

//Method to convert the Fahrenheit to Celsius value


float fahrenheit_to_Celsius(float fahrenheit)
{
return ((fahrenheit - 32.0) * 5.0 / 9.0);
}

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.

• The function prototype has the following syntax:


<type> <function name>(<type list>);

• Example: A function that returns the absolute value of


an integer is: int absolute(int);
Parameter Passing
• Actual parameters (arguments)are the parameters that appear in the
function call.
average = AverageTwo (value1, value2) ;

• Formal parameters are the parameters that appear in the function


header.
function AverageTwo (num1, num2)

• Actual and formal parameters are matched by position. Each formal


parameter receives the value of its corresponding actual parameter.
1) Function without arguments
and without return value
void function_name();
void main()
{
….. …..
….. …..
function_name();
….. …..
}
void function_name()
{
….. …..
….. …..
}
Example:
//Write a C program to sum of given series : 1,2,3,…..n

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

void function_name(list of arguments);


void main()
{
….. …..
….. …..
function_name(list of arguments);
….. …..
}
void function_name(list of arguments)
{
….. …..
….. …..
}
Example:
void sum_of_series(int);
void main()
{
int n;
printf(“Enter the value of n”);
scanf(“%d”,&n);
sum_of_series(n);
}
void sum_of_series(int x)
{
int i,sum=0;
for(i=1;i<=x;i++)
sum+=i;
printf(“Sum of series is: %d”,sum);
}
Input: Output:
Enter the value of n 6 Sum of series is: 21
4. Function with arguments and with return value

return_type function_name(List of arguments);


void main()
{
….. …..
….. …..
var=function_name(list of arguments);
….. …..
}
return_type function_name(list of arguments)
{
….. …..
….. …..
return(values);
}
Example:

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

Enter two number:6


7

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>

void swap(int x, int y){ void swap(int *x, int *y){


int temp = x; int temp = *x;
x = y; *x = *y;
y = temp; *y = temp;
} }

int main() int main()


{ {
int x = 10; int x = 10;
int y = 11; int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y); printf("Values before swap: x = %d, y = %d\n", x,y);
swap(x,y); swap(&x,&y);
printf("Values after swap: x = %d, y = %d", x,y); printf("Values after swap: x = %d, y = %d", x,y);
} }
Recursive Function
 The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function.
 Using recursive algorithm, certain problems can be solved quite easily.
 Example:

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);

printf("Sum of digits of %d = %d", num, sum);


OUTPUT
return 0;
Enter any number to find sum of digits: 1234
} Sum of digits of 1234 = 10

You might also like