This document discusses functions in C programming. It defines functions as blocks of code that perform tasks and return single values. It discusses two types of functions: built-in functions provided by the language and user-defined functions created by the programmer. It outlines the steps to working with user-defined functions: declaration, definition, and call. It provides examples of simple programs that demonstrate calculating area and circumference using functions, exchanging variable values using a function, and a welcome message function.
3. Definition
A Function is a self-contained block of statement
that perform a task of some kind.
OR
Function is a set of instruction to carryout a
particular task.
Function after its execution returns a single value
when we call it in our program.
Functions in C
4. 1.Built-in-Functions
• The built-in-function are
also called library function
or Standard Function. These
functions are
provided by programmer.
• printf(); and scanf(); are
Some common types of
built-in-Functions.
2.User-Defined Fun.
• Defined by user at the
time of writing the
program. User set these
program for her own
uses.
• Add(); Max(); etc are
some kinds of user-
defined functions.
Functions in C
5. Need of functions
• Enabling code reuse
• Better readability
• Improved debugging and testing
• Improved maintainability
• Reduce lines of code
Functions in C
6. Steps of User-Defined-F.
There are three steps of working with user
Defined functions:
1. Function Declaration, also known as function
prototype
2. Function Dentition means body of function.
3. Function use, also known as function call.
Functions in C
7. Programs.
• Simple program by Function :-
#include<stdio.h>
void massage();/*Function prototype declaration*/
void main()
{
void massage();/*function call*/
}
void massage()/*function definition*/
{
printf(“This is a simple program");
}
start
Function call
Void massag
Output
end
Out put :-
This is simple program
8. To find area and circumference of
circle.
#include<stdio.h>
#define PI 3.14159
float carea(float);
float ccir(float);
void main()
{ float radius;
printf("Enter floater Radius:");
scanf("%f",&radius);
carea(radius);
ccir(radius);
getch();
} float carea(float radius)
{ float area;
area=PI*radius*radius;
printf("Area= %fn",area);}
float ccir(float radius)
{ float ccir;
cir=2*PI*radius;
printf("Circumference= %f",cir);}
• Flo.
Programs.
start
Input radius
Function call
Float carea,crim
area=PI*radius*radius
cir=2*PI* radius
Output
Area,Cri
end
Enter floater Radius _5.0
Area=78.53975
Circumference=31.4159
9. To find area and circumference of
cube.
#include<stdio.h>
#define PI 3.14159
float carea(float);
float ccir(float);
void main()
{ float radius;
printf("Enter floater Radius:");
scanf("%f",&radius);
carea(radius);
ccir(radius);
getch();
} float carea(float radius)
{ float area;
area=4*PI*radius*radius;
printf("Area= %fn",area);}
float ccir(float radius)
{ float ccir;
cir=4/3*PI*radius*radius*radius*;
printf("Circumferernce= %f",cir);}
Programs. start
Input radius
Function call
Float carea,crim
area=4*PI*radius*radius
cir=4/3*PI* radius* radius*radius
Output
Area,Cir
end
Enter floater Radius _5.0
Area=1570.795
Circumference=523.59833335
10. To Exchange Values of 2 variables.
include<stdio.h>
void swap(int *x, int *y);
void main() {
int x, y;
printf("nEnter First number : ");
scanf("%d", &x);
printf("nEnter Second number : ");
scanf("%d", &y);
printf("nBefore Swaping x = %d and y = %d", x, y);
swap(&x, &y);
printf("nAfter Swaping x = %d and y = %d", x, y);
getch();}
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;}
Programs. start
Input x,y
Output
Values of x and y
end
Function call
Void swap
Temp=*x
*x=*y
Y=*temp
Enter First number_4
Enter Second number_8
Before Swaping x=4 and y=8
After Swaping x=8 and y=4