SlideShare ist ein Scribd-Unternehmen logo
1 von 79
Unit 4
KIRTHIKA KM /AP/CSE
FUNCTION
 A Function is a sub-program, which contains one or
more statements and it performs some task when its
called.
 A computer program cannot handle all the tasks by it
self. Instead its requests other program like entities –
called functions in C to get its tasks done.
 A function is a self contained block of statements that
perform a coherent task of same kind
KIRTHIKA KM /AP/CSE
Why we use functions?
 Writing functions avoids rewriting the same code over and
over.
 Suppose that there is a section of code in a program that
calculates area of a circle. If later in the program we want to
calculate the area of a different circle, we wont like to write
the same instructions again.
 Instead, we would prefer to jump to a “section of code” that
calculates area and then jump back to the place from where
we left off.
 This section of code is nothing but a function.
KIRTHIKA KM /AP/CSE
 Using functions it becomes easier to write programs and
keep track of what they are doing.
 If the operation of a program can be divided in to separate
activities, and each activity placed in a different function,
then each could be written and checked more or less
independently.
 Separating the code in to modular functions also makes the
pro-gram easier to design and understand.
Why we use functions?
KIRTHIKA KM /AP/CSE
TYPES
 There are two different types of functions:
 Pre-defined functions
 User-defined functions
KIRTHIKA KM /AP/CSE
Pre-Defined Functions
 The pre-defined functions or library functions are
built-in functions.
 The user can use the functions, but cannot modify
those functions.
 Example: sqrt()
KIRTHIKA KM /AP/CSE
User-Defined Functions
 The functions defined by the user for their
requirements are called user-defined functions.
 Whenever it is needed, the user can modify this
function.
 Example: sum(a,b)
KIRTHIKA KM /AP/CSE
Advantage of User-Defined Functions
 The length of the source program can be reduced.
 It is easy to locate errors.
 It avoids coding of repeated instructions.
KIRTHIKA KM /AP/CSE
Elements of User-Defined
Function
 Function declaration
 Function definition
 Function call
KIRTHIKA KM /AP/CSE
Function
 Syntax
datatype function_name (parameters list)
{
local variable declaration;
…………………………
body of the function;
…………………………
return(expression);
}
KIRTHIKA KM /AP/CSE
How Function Works?
 Once a function is called the control passes to the
called function.
 The working of calling function is temporarily
stopped.
 When the execution of called function is completed
then the control returns back to the calling function
and executes the next statement.
KIRTHIKA KM /AP/CSE
KIRTHIKA KM /AP/CSE
Parameters
 Actual Parameter
These are the parameters which are transferred from
the calling function to the called function.
 Formal Parameter
These are the parameters which are used in the called
function.
KIRTHIKA KM /AP/CSE
KIRTHIKA KM /AP/CSE
return Statement
 The return statement may or may not send some
values to the calling function.
 Syntax:
return; (or)
return (expression);
KIRTHIKA KM /AP/CSE
Function Prototypes
There are four types:
 Function with no arguments and no return values.
 Function with arguments and no return values.
 Function with arguments and return values.
 Function with no arguments and with return values.
KIRTHIKA KM /AP/CSE
Function with no arguments
and no return values
 Here no data transfer takes place between the calling
function and the called function.
 These functions act independently, i.e. they get input
and display output in the same block.
KIRTHIKA KM /AP/CSE
KIRTHIKA KM /AP/CSE
Example
#include <stdio.h>
#include<conio.h>
void main() //calling function
{
void add(void);
add();
}
void add() //called function
{
int a,b,c;
printf("nEnter two number:");
scanf("%d%d",&a,&b);
c=a+b;
printf("nSum is:%d",c);
}
KIRTHIKA KM /AP/CSE
Output
Enter two number:3
4
Sum is:7
KIRTHIKA KM /AP/CSE
Function with arguments
and no return values
 Here data transfer take place between the calling
function and the called function.
 It is a one way data communication, i.e. the called
program receives data from calling program but it does
not return any value to the calling program.
KIRTHIKA KM /AP/CSE
KIRTHIKA KM /AP/CSE
Example
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b;
void add(int,int);
printf("nEnter two number:");
scanf("%d%d",&a,&b);
add(a,b);
}
void add(int x,int y) //function with arguments
{
int z;
z=x+y;
printf("nSum is:%d",z);
}
KIRTHIKA KM /AP/CSE
Output
Enter two number:2
4
Sum is:6
KIRTHIKA KM /AP/CSE
Function with arguments
and return values
 Here data transfer takes place between the calling
function and the called function as well as between
called function and calling function .
 It is a two way data communication, i.e. the called
program receives data from calling program and it
returns some value to the calling program.
KIRTHIKA KM /AP/CSE
KIRTHIKA KM /AP/CSE
Example
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int add(int,int);
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);
}
KIRTHIKA KM /AP/CSE
Output
Enter two number:6
7
Sum is:13
KIRTHIKA KM /AP/CSE
Function with no arguments
and with return values
 Here data transfer takes place between the called
function and the calling function.
 It is a one way data communication, i.e. the called
program does not any receive data from the calling
program but it returns some value to the calling
program.
KIRTHIKA KM /AP/CSE
KIRTHIKA KM /AP/CSE
Example
#include <stdio.h>
#include<conio.h>
void main()
{
int add(),d;
d=add();
printf("nSum is:%d",d);
}
int add() //function with no argument
{
int a,b,c;
printf("nEnter two number:");
scanf("%d%d",&a,&b);
c=a+b;
return(c);
}
KIRTHIKA KM /AP/CSE
Output
Enter two number:5
8
Sum is:13
KIRTHIKA KM /AP/CSE
Parameter Passing Methods
 There are two different ways of passing parameters to a
method, they are:
 Call by value
 Call by reference
KIRTHIKA KM /AP/CSE
Call by value
 Actual arguments are passed to the formal arguments.
 Any changes made to the formal argument does not
affect the actual argument.
KIRTHIKA KM /AP/CSE
Example
#include <stdio.h>
#include<conio.h>
void main()
{
int x,y,change(int,int);
printf("nEnter value of x:");
scanf("%d",&x);
printf("nEnter value of y:");
scanf("%d",&y);
change(x,y);
printf("nnValues in the
Main()-->x=%d,y=%d",x,y);
}
int change(int a,int b)
{
int c;
c=a;
a=b;
b=c;
printf("nValues in the
Fuction --
>x=%d,y=%d",a,b);
}
KIRTHIKA KM /AP/CSE
Output
Enter value of x:5
Enter value of y:6
Values in the Function -->x=6,y=5
Values in the Main()-->x=5,y=6
KIRTHIKA KM /AP/CSE
Call by reference
 Instead of passing values, the address of the argument
will be passed.
 Any changes made to the formal argument will affect
the actual argument.
KIRTHIKA KM /AP/CSE
Example
#include <stdio.h>
#include<conio.h>
void main()
{
int x,y,change(int*,int*);
printf("nEnter value of
x:");
scanf("%d",&x);
printf("nEnter value of
y:");
scanf("%d",&y);
change(&x,&y);
printf("nnValues in
the Main()--
>x=%d,y=%d",x,y);
}
int change(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
printf("nValues in the
Function --
>x=%d,y=%d",*a,*b);
}
KIRTHIKA KM /AP/CSE
Output
Enter value of x:5
Enter value of y:6
Values in the Function -->x=6,y=5
Values in the Main()-->x=6,y=5
KIRTHIKA KM /AP/CSE
Recursion
 It is a process of calling the same function itself again and
again until some condition is satisfied.
Syntax:
func1()
{
………..
func1();
…………
}
KIRTHIKA KM /AP/CSE
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int rec(int);
printf("nEnter the number:");
scanf("%d",&a);
printf("The factorial of %d! is
%d",a,rec(a));
}
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
Output:
Enter the number:5
The factorial of 5! is 120
KIRTHIKA KM /AP/CSE
Example: Working of 3!
KIRTHIKA KM /AP/CSE
Library Function
 Library functions are the pre-defined functions.
 The library function provides functions like
mathematical, string manipulation etc,.
 In order to use a library function, it is necessary to call
the appropriate header file at the beginning of the
program.
 The header file informs the program of the name,
type, and number and type of arguments, of all of the
functions contained in the library in question.
 A header file is called via the preprocessor statement.
KIRTHIKA KM /AP/CSE
Some Examples of Library
Functions
 sqrt(x):
It is used to find the square root of x
Example: sqrt(36) is 6
 abs(x):
It is used to find the absolute value of x
Example: abs(-36) is 36
 pow(x,y):
It is used to find the value of xy
Example: pow(5,2) is 25
KIRTHIKA KM /AP/CSE
 ceil(x):
It is used to find the smallest integer greater than or equal to x.
Example: ceil(7.7) is 8
 rand():
It is used to generate a random number.
 sin(x):
It is used to find the sine value of x
Example: sin(30) is 0.5
 cos (x):
It is used to find the cosine value of x
Example: cos(30) is 0.86
KIRTHIKA KM /AP/CSE
 tan(x):
It is used to find the tan value of x
Example: tan(30) is 0.577
 toascii(x):
It is used to find the ASCII value of x
Example: toascii(a) is 97
 toupper(x):
It is used to convert lowercase character to uppercase.
Example: toupper(‘a’) is A toupper(97) is A
 tolower(x):
It is used to convert uppercase character to lowercase.
Example: tolower(‘A’) is a
KIRTHIKA KM /AP/CSE
Example:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
void main()
{
int x,y=2;
printf("nEnter the number:");
scanf("%d",&x);
printf("nThe square root of %d is %f",x,sqrt(x));
printf("nThe value of %d power%dis%f ",x,y,pow(6,2));
printf("nThe ceiling of 6.7 is %f",ceil(6.7));
printf("nThe floor of 6.7 is %f",floor(6.7));
KIRTHIKA KM /AP/CSE
printf("nThe absolute value of -6 is %d",abs(-6));
printf("nThe value of sin 45 is %f",sin(45));
printf("nThe uppercase of 'a' is %c",toupper('a'));
printf("nThe uppercase of 97 is %c",toupper(97));
getch();
}
KIRTHIKA KM /AP/CSE
Output:
Enter the number:6
The square root of 6 is 2.449490
The value of 6 power 2 is 36.000000
The ceiling of 6.7 is 7.000000
The floor of 6.7 is 6.000000
The absolute value of -6 is 6
The value of sin 45 is 0.850904
The uppercase of 'a' is A
The uppercase of 97 is A
KIRTHIKA KM /AP/CSE
Applications:
 Math functions
 Computation of Sine Series
 Random Number Generation
 Tower of Hanoi
 Factorial using Recursive functions.
KIRTHIKA KM /AP/CSE
Math functions
KIRTHIKA KM /AP/CSE
Pointers
 Pointer is a variable that contains the address of
another variable i.e.. direct address of the memory
location.
 Like any variable or constant, you must declare a
pointer before you can use it to store any variable
address.
KIRTHIKA KM /AP/CSE
Example:
x=5
x Variable
1002 Address
5 Value
KIRTHIKA KM /AP/CSE
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int x=5;
printf("n The Address of x = %u",&x);
printf("n The Value of x = %d",x);
}
Output
The Address of x = 8714
The Value of x = 5
KIRTHIKA KM /AP/CSE
Pointer Declaration
 Syntax:
data-type *pointer-name;
data-type - Type of the data to
which the pointer points.
pointer-name - Name of the pointer
 Example: int *a;
KIRTHIKA KM /AP/CSE
Accessing Variable through Pointer
 If a pointer is declared and assigned to a variable, then
the variable can be accessed through the pointer.
 Example:
int *a;
x=5;
a=&x;
KIRTHIKA KM /AP/CSE
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int y=10;
int *a;
a=&y;
printf("n The Value of y = %d",y);
printf("n The Address of y = %u",&y);
printf("n The Value of a = %d",a);
printf("n The Address of a = %u",&a);
}
KIRTHIKA KM /AP/CSE
Illustration of the example:
5001 10
8000
a y
5001
Variable
Value
Address
KIRTHIKA KM /AP/CSE
Output
The Value of y = 10
The Address of y = 5001
The Value of a = 5001
The Address of a = 8000
KIRTHIKA KM /AP/CSE
Null Pointer
 A pointer is said to be null pointer if zero is assigned to
the pointer.
 For Example:
int *a,*b;
a=b=0;
KIRTHIKA KM /AP/CSE
Pointer to Pointer
 Here one pointer stores the address of another pointer
variable.
 Example:
int x=10,*a,**b;
a=&x;
b=&a;
KIRTHIKA KM /AP/CSE
 Illustration:
5001 10
8000
a x
5001
Variable
Value
Address
8000
9000
b
KIRTHIKA KM /AP/CSE
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *b,**c;
b=&a;
c=&b;
printf("n The Value of a = %d",a);
printf("n The Address of a = %u",&a);
printf("n The Value of b = %d",b);
printf("n The Address of b = %u",&b);
printf("n The Value of c = %d",c);
printf("n The Address of c = %u",&c);
}
KIRTHIKA KM /AP/CSE
Output
The Value of a = 10
The Address of a = 5001
The Value of b = 5001
The Address of b = 8000
The Value of c = 8000
The Address of c = 9000
KIRTHIKA KM /AP/CSE
Pointers and Arrays
 The elements of the array can also be accessed through
a pointer.
 Example
int a[3]={2,3,7};
int *b;
b=a;
KIRTHIKA KM /AP/CSE
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3]={2,3,7};
int *b;
b=a;
printf("n The Value of a[0] = %d",a[0]);
printf("n The Address of a[0] = %u",&a[0]);
printf("n The Value of b = %d",b);
}
KIRTHIKA KM /AP/CSE
 Illustration of the example:
8744 2
9000
b a[0]
8744
Variable
Value
Address
KIRTHIKA KM /AP/CSE
Output
The Value of a[0] = 2
The Address of a[0] = 8744
The Value of b = 8744
KIRTHIKA KM /AP/CSE
Example 2
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={2,3,7,9,10};
int i;
for(i=0;i<5;i++)
{
printf("n The Value of a[%d] = %d",i,a[i]);
printf("n The Address of a[%d] = %u",i,&a[i]);
}
}
KIRTHIKA KM /AP/CSE
Illustration of the example:
2 3 7 9 10
a[0] a[1] a[2] a[3] a[4]
8724 8726 8728 8730 8732
Array
Value
Address
KIRTHIKA KM /AP/CSE
Output
The Value of a[0] = 2
The Address of a[0] = 8724
The Value of a[1] = 3
The Address of a[1] = 8726
The Value of a[2] = 7
The Address of a[2] = 8728
The Value of a[3] = 9
The Address of a[3] = 8730
The Value of a[4] = 10
The Address of a[4] = 8732
KIRTHIKA KM /AP/CSE
Example 3
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={1,2,3,4,5};
int i,sum=0;
int *b;
b=a;
for(i=0;i<5;i++)
{
sum=sum + *b;
b++; //b=b+1
}
printf("n The Sum is %d",sum);
}
KIRTHIKA KM /AP/CSE
Output
The Sum is 15
KIRTHIKA KM /AP/CSE
Pointers and Structures
 Syntax:
struct structure_name
{
structure element1;
structure element2;
…………………….
}variable,*ptr;
KIRTHIKA KM /AP/CSE
 A sample code illustrating pointers and structure
struct stud
{
int sno;
char name[10];
int mark;
};
struct stud *s;
KIRTHIKA KM /AP/CSE
Example
#include<stdio.h>
#include<conio.h>
struct stud
{
int regno;
char name[10];
int m1;
int m2;
int m3;
};
struct stud s;
struct stud *t;
KIRTHIKA KM /AP/CSE
void main()
{
float tot,avg;
t=&s;
printf("nEnter the student regno,name,m1,m2,m3:");
scanf("%d%s%d%d%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3);
tot=s.m1+s.m2+s.m3;
avg=tot/3;
printf("nThe student Details are:");
printf("n%dt%st%ft%f",s.regno,s.name,tot,avg);
printf("n%dt%st%ft%f",t->regno,t->name,tot,avg);
}
KIRTHIKA KM /AP/CSE
Output
Enter the student regno,name,m1,m2,m3:1
aaa
76
89
76
The student Details are:
1 aaa 241.000000 80.333336
1 aaa 241.000000 80.333336
KIRTHIKA KM /AP/CSE
KIRTHIKA KM /AP/CSE

Weitere ähnliche Inhalte

Was ist angesagt? (19)

Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
functions in C
functions in Cfunctions in C
functions in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
C++ language basic
C++ language basicC++ language basic
C++ language basic
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Tut1
Tut1Tut1
Tut1
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Cs211 module 1_2015
Cs211 module 1_2015Cs211 module 1_2015
Cs211 module 1_2015
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
What is c
What is cWhat is c
What is c
 
C functions
C functionsC functions
C functions
 
Tutorial on c language programming
Tutorial on c language programmingTutorial on c language programming
Tutorial on c language programming
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 

Ähnlich wie Unit 4 functions and pointers

Ähnlich wie Unit 4 functions and pointers (20)

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
C function
C functionC function
C function
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 

Kürzlich hochgeladen

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 

Kürzlich hochgeladen (20)

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 

Unit 4 functions and pointers

  • 2. FUNCTION  A Function is a sub-program, which contains one or more statements and it performs some task when its called.  A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions in C to get its tasks done.  A function is a self contained block of statements that perform a coherent task of same kind KIRTHIKA KM /AP/CSE
  • 3. Why we use functions?  Writing functions avoids rewriting the same code over and over.  Suppose that there is a section of code in a program that calculates area of a circle. If later in the program we want to calculate the area of a different circle, we wont like to write the same instructions again.  Instead, we would prefer to jump to a “section of code” that calculates area and then jump back to the place from where we left off.  This section of code is nothing but a function. KIRTHIKA KM /AP/CSE
  • 4.  Using functions it becomes easier to write programs and keep track of what they are doing.  If the operation of a program can be divided in to separate activities, and each activity placed in a different function, then each could be written and checked more or less independently.  Separating the code in to modular functions also makes the pro-gram easier to design and understand. Why we use functions? KIRTHIKA KM /AP/CSE
  • 5. TYPES  There are two different types of functions:  Pre-defined functions  User-defined functions KIRTHIKA KM /AP/CSE
  • 6. Pre-Defined Functions  The pre-defined functions or library functions are built-in functions.  The user can use the functions, but cannot modify those functions.  Example: sqrt() KIRTHIKA KM /AP/CSE
  • 7. User-Defined Functions  The functions defined by the user for their requirements are called user-defined functions.  Whenever it is needed, the user can modify this function.  Example: sum(a,b) KIRTHIKA KM /AP/CSE
  • 8. Advantage of User-Defined Functions  The length of the source program can be reduced.  It is easy to locate errors.  It avoids coding of repeated instructions. KIRTHIKA KM /AP/CSE
  • 9. Elements of User-Defined Function  Function declaration  Function definition  Function call KIRTHIKA KM /AP/CSE
  • 10. Function  Syntax datatype function_name (parameters list) { local variable declaration; ………………………… body of the function; ………………………… return(expression); } KIRTHIKA KM /AP/CSE
  • 11. How Function Works?  Once a function is called the control passes to the called function.  The working of calling function is temporarily stopped.  When the execution of called function is completed then the control returns back to the calling function and executes the next statement. KIRTHIKA KM /AP/CSE
  • 13. Parameters  Actual Parameter These are the parameters which are transferred from the calling function to the called function.  Formal Parameter These are the parameters which are used in the called function. KIRTHIKA KM /AP/CSE
  • 15. return Statement  The return statement may or may not send some values to the calling function.  Syntax: return; (or) return (expression); KIRTHIKA KM /AP/CSE
  • 16. Function Prototypes There are four types:  Function with no arguments and no return values.  Function with arguments and no return values.  Function with arguments and return values.  Function with no arguments and with return values. KIRTHIKA KM /AP/CSE
  • 17. Function with no arguments and no return values  Here no data transfer takes place between the calling function and the called function.  These functions act independently, i.e. they get input and display output in the same block. KIRTHIKA KM /AP/CSE
  • 19. Example #include <stdio.h> #include<conio.h> void main() //calling function { void add(void); add(); } void add() //called function { int a,b,c; printf("nEnter two number:"); scanf("%d%d",&a,&b); c=a+b; printf("nSum is:%d",c); } KIRTHIKA KM /AP/CSE
  • 20. Output Enter two number:3 4 Sum is:7 KIRTHIKA KM /AP/CSE
  • 21. Function with arguments and no return values  Here data transfer take place between the calling function and the called function.  It is a one way data communication, i.e. the called program receives data from calling program but it does not return any value to the calling program. KIRTHIKA KM /AP/CSE
  • 23. Example #include <stdio.h> #include<conio.h> void main() { int a,b; void add(int,int); printf("nEnter two number:"); scanf("%d%d",&a,&b); add(a,b); } void add(int x,int y) //function with arguments { int z; z=x+y; printf("nSum is:%d",z); } KIRTHIKA KM /AP/CSE
  • 24. Output Enter two number:2 4 Sum is:6 KIRTHIKA KM /AP/CSE
  • 25. Function with arguments and return values  Here data transfer takes place between the calling function and the called function as well as between called function and calling function .  It is a two way data communication, i.e. the called program receives data from calling program and it returns some value to the calling program. KIRTHIKA KM /AP/CSE
  • 27. Example #include <stdio.h> #include<conio.h> void main() { int a,b,c; int add(int,int); 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); } KIRTHIKA KM /AP/CSE
  • 28. Output Enter two number:6 7 Sum is:13 KIRTHIKA KM /AP/CSE
  • 29. Function with no arguments and with return values  Here data transfer takes place between the called function and the calling function.  It is a one way data communication, i.e. the called program does not any receive data from the calling program but it returns some value to the calling program. KIRTHIKA KM /AP/CSE
  • 31. Example #include <stdio.h> #include<conio.h> void main() { int add(),d; d=add(); printf("nSum is:%d",d); } int add() //function with no argument { int a,b,c; printf("nEnter two number:"); scanf("%d%d",&a,&b); c=a+b; return(c); } KIRTHIKA KM /AP/CSE
  • 32. Output Enter two number:5 8 Sum is:13 KIRTHIKA KM /AP/CSE
  • 33. Parameter Passing Methods  There are two different ways of passing parameters to a method, they are:  Call by value  Call by reference KIRTHIKA KM /AP/CSE
  • 34. Call by value  Actual arguments are passed to the formal arguments.  Any changes made to the formal argument does not affect the actual argument. KIRTHIKA KM /AP/CSE
  • 35. Example #include <stdio.h> #include<conio.h> void main() { int x,y,change(int,int); printf("nEnter value of x:"); scanf("%d",&x); printf("nEnter value of y:"); scanf("%d",&y); change(x,y); printf("nnValues in the Main()-->x=%d,y=%d",x,y); } int change(int a,int b) { int c; c=a; a=b; b=c; printf("nValues in the Fuction -- >x=%d,y=%d",a,b); } KIRTHIKA KM /AP/CSE
  • 36. Output Enter value of x:5 Enter value of y:6 Values in the Function -->x=6,y=5 Values in the Main()-->x=5,y=6 KIRTHIKA KM /AP/CSE
  • 37. Call by reference  Instead of passing values, the address of the argument will be passed.  Any changes made to the formal argument will affect the actual argument. KIRTHIKA KM /AP/CSE
  • 38. Example #include <stdio.h> #include<conio.h> void main() { int x,y,change(int*,int*); printf("nEnter value of x:"); scanf("%d",&x); printf("nEnter value of y:"); scanf("%d",&y); change(&x,&y); printf("nnValues in the Main()-- >x=%d,y=%d",x,y); } int change(int *a,int *b) { int c; c=*a; *a=*b; *b=c; printf("nValues in the Function -- >x=%d,y=%d",*a,*b); } KIRTHIKA KM /AP/CSE
  • 39. Output Enter value of x:5 Enter value of y:6 Values in the Function -->x=6,y=5 Values in the Main()-->x=6,y=5 KIRTHIKA KM /AP/CSE
  • 40. Recursion  It is a process of calling the same function itself again and again until some condition is satisfied. Syntax: func1() { ……….. func1(); ………… } KIRTHIKA KM /AP/CSE
  • 41. Example #include<stdio.h> #include<conio.h> void main() { int a; int rec(int); printf("nEnter the number:"); scanf("%d",&a); printf("The factorial of %d! is %d",a,rec(a)); } int rec(int x) { int f; if(x==1) return(1); else f=x*rec(x-1); return(f); } Output: Enter the number:5 The factorial of 5! is 120 KIRTHIKA KM /AP/CSE
  • 42. Example: Working of 3! KIRTHIKA KM /AP/CSE
  • 43. Library Function  Library functions are the pre-defined functions.  The library function provides functions like mathematical, string manipulation etc,.  In order to use a library function, it is necessary to call the appropriate header file at the beginning of the program.  The header file informs the program of the name, type, and number and type of arguments, of all of the functions contained in the library in question.  A header file is called via the preprocessor statement. KIRTHIKA KM /AP/CSE
  • 44. Some Examples of Library Functions  sqrt(x): It is used to find the square root of x Example: sqrt(36) is 6  abs(x): It is used to find the absolute value of x Example: abs(-36) is 36  pow(x,y): It is used to find the value of xy Example: pow(5,2) is 25 KIRTHIKA KM /AP/CSE
  • 45.  ceil(x): It is used to find the smallest integer greater than or equal to x. Example: ceil(7.7) is 8  rand(): It is used to generate a random number.  sin(x): It is used to find the sine value of x Example: sin(30) is 0.5  cos (x): It is used to find the cosine value of x Example: cos(30) is 0.86 KIRTHIKA KM /AP/CSE
  • 46.  tan(x): It is used to find the tan value of x Example: tan(30) is 0.577  toascii(x): It is used to find the ASCII value of x Example: toascii(a) is 97  toupper(x): It is used to convert lowercase character to uppercase. Example: toupper(‘a’) is A toupper(97) is A  tolower(x): It is used to convert uppercase character to lowercase. Example: tolower(‘A’) is a KIRTHIKA KM /AP/CSE
  • 47. Example: #include<stdio.h> #include<conio.h> #include<math.h> #include<ctype.h> void main() { int x,y=2; printf("nEnter the number:"); scanf("%d",&x); printf("nThe square root of %d is %f",x,sqrt(x)); printf("nThe value of %d power%dis%f ",x,y,pow(6,2)); printf("nThe ceiling of 6.7 is %f",ceil(6.7)); printf("nThe floor of 6.7 is %f",floor(6.7)); KIRTHIKA KM /AP/CSE
  • 48. printf("nThe absolute value of -6 is %d",abs(-6)); printf("nThe value of sin 45 is %f",sin(45)); printf("nThe uppercase of 'a' is %c",toupper('a')); printf("nThe uppercase of 97 is %c",toupper(97)); getch(); } KIRTHIKA KM /AP/CSE
  • 49. Output: Enter the number:6 The square root of 6 is 2.449490 The value of 6 power 2 is 36.000000 The ceiling of 6.7 is 7.000000 The floor of 6.7 is 6.000000 The absolute value of -6 is 6 The value of sin 45 is 0.850904 The uppercase of 'a' is A The uppercase of 97 is A KIRTHIKA KM /AP/CSE
  • 50. Applications:  Math functions  Computation of Sine Series  Random Number Generation  Tower of Hanoi  Factorial using Recursive functions. KIRTHIKA KM /AP/CSE
  • 52. Pointers  Pointer is a variable that contains the address of another variable i.e.. direct address of the memory location.  Like any variable or constant, you must declare a pointer before you can use it to store any variable address. KIRTHIKA KM /AP/CSE
  • 53. Example: x=5 x Variable 1002 Address 5 Value KIRTHIKA KM /AP/CSE
  • 54. Example #include<stdio.h> #include<conio.h> void main() { int x=5; printf("n The Address of x = %u",&x); printf("n The Value of x = %d",x); } Output The Address of x = 8714 The Value of x = 5 KIRTHIKA KM /AP/CSE
  • 55. Pointer Declaration  Syntax: data-type *pointer-name; data-type - Type of the data to which the pointer points. pointer-name - Name of the pointer  Example: int *a; KIRTHIKA KM /AP/CSE
  • 56. Accessing Variable through Pointer  If a pointer is declared and assigned to a variable, then the variable can be accessed through the pointer.  Example: int *a; x=5; a=&x; KIRTHIKA KM /AP/CSE
  • 57. Example #include<stdio.h> #include<conio.h> void main() { int y=10; int *a; a=&y; printf("n The Value of y = %d",y); printf("n The Address of y = %u",&y); printf("n The Value of a = %d",a); printf("n The Address of a = %u",&a); } KIRTHIKA KM /AP/CSE
  • 58. Illustration of the example: 5001 10 8000 a y 5001 Variable Value Address KIRTHIKA KM /AP/CSE
  • 59. Output The Value of y = 10 The Address of y = 5001 The Value of a = 5001 The Address of a = 8000 KIRTHIKA KM /AP/CSE
  • 60. Null Pointer  A pointer is said to be null pointer if zero is assigned to the pointer.  For Example: int *a,*b; a=b=0; KIRTHIKA KM /AP/CSE
  • 61. Pointer to Pointer  Here one pointer stores the address of another pointer variable.  Example: int x=10,*a,**b; a=&x; b=&a; KIRTHIKA KM /AP/CSE
  • 62.  Illustration: 5001 10 8000 a x 5001 Variable Value Address 8000 9000 b KIRTHIKA KM /AP/CSE
  • 63. Example #include<stdio.h> #include<conio.h> void main() { int a=10; int *b,**c; b=&a; c=&b; printf("n The Value of a = %d",a); printf("n The Address of a = %u",&a); printf("n The Value of b = %d",b); printf("n The Address of b = %u",&b); printf("n The Value of c = %d",c); printf("n The Address of c = %u",&c); } KIRTHIKA KM /AP/CSE
  • 64. Output The Value of a = 10 The Address of a = 5001 The Value of b = 5001 The Address of b = 8000 The Value of c = 8000 The Address of c = 9000 KIRTHIKA KM /AP/CSE
  • 65. Pointers and Arrays  The elements of the array can also be accessed through a pointer.  Example int a[3]={2,3,7}; int *b; b=a; KIRTHIKA KM /AP/CSE
  • 66. Example 1 #include<stdio.h> #include<conio.h> void main() { int a[3]={2,3,7}; int *b; b=a; printf("n The Value of a[0] = %d",a[0]); printf("n The Address of a[0] = %u",&a[0]); printf("n The Value of b = %d",b); } KIRTHIKA KM /AP/CSE
  • 67.  Illustration of the example: 8744 2 9000 b a[0] 8744 Variable Value Address KIRTHIKA KM /AP/CSE
  • 68. Output The Value of a[0] = 2 The Address of a[0] = 8744 The Value of b = 8744 KIRTHIKA KM /AP/CSE
  • 69. Example 2 #include<stdio.h> #include<conio.h> void main() { int a[5]={2,3,7,9,10}; int i; for(i=0;i<5;i++) { printf("n The Value of a[%d] = %d",i,a[i]); printf("n The Address of a[%d] = %u",i,&a[i]); } } KIRTHIKA KM /AP/CSE
  • 70. Illustration of the example: 2 3 7 9 10 a[0] a[1] a[2] a[3] a[4] 8724 8726 8728 8730 8732 Array Value Address KIRTHIKA KM /AP/CSE
  • 71. Output The Value of a[0] = 2 The Address of a[0] = 8724 The Value of a[1] = 3 The Address of a[1] = 8726 The Value of a[2] = 7 The Address of a[2] = 8728 The Value of a[3] = 9 The Address of a[3] = 8730 The Value of a[4] = 10 The Address of a[4] = 8732 KIRTHIKA KM /AP/CSE
  • 72. Example 3 #include<stdio.h> #include<conio.h> void main() { int a[5]={1,2,3,4,5}; int i,sum=0; int *b; b=a; for(i=0;i<5;i++) { sum=sum + *b; b++; //b=b+1 } printf("n The Sum is %d",sum); } KIRTHIKA KM /AP/CSE
  • 73. Output The Sum is 15 KIRTHIKA KM /AP/CSE
  • 74. Pointers and Structures  Syntax: struct structure_name { structure element1; structure element2; ……………………. }variable,*ptr; KIRTHIKA KM /AP/CSE
  • 75.  A sample code illustrating pointers and structure struct stud { int sno; char name[10]; int mark; }; struct stud *s; KIRTHIKA KM /AP/CSE
  • 76. Example #include<stdio.h> #include<conio.h> struct stud { int regno; char name[10]; int m1; int m2; int m3; }; struct stud s; struct stud *t; KIRTHIKA KM /AP/CSE
  • 77. void main() { float tot,avg; t=&s; printf("nEnter the student regno,name,m1,m2,m3:"); scanf("%d%s%d%d%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3); tot=s.m1+s.m2+s.m3; avg=tot/3; printf("nThe student Details are:"); printf("n%dt%st%ft%f",s.regno,s.name,tot,avg); printf("n%dt%st%ft%f",t->regno,t->name,tot,avg); } KIRTHIKA KM /AP/CSE
  • 78. Output Enter the student regno,name,m1,m2,m3:1 aaa 76 89 76 The student Details are: 1 aaa 241.000000 80.333336 1 aaa 241.000000 80.333336 KIRTHIKA KM /AP/CSE