SlideShare a Scribd company logo
PREPARED BY:- 	PRADEEP DWIVEDI (pursuing B.TECH-IT) FROM H.C.S.T.(MATHURA) Mob-+919027843806 E-mail-pradeep.it74@gmail.com C-PROGRAMMING SLIDE-6 Friday, February 11, 2011 1 PRADEEP DWIVEDI(pur.B.TECH-IT)
C-6 Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 2 TOPIC:- USER DEFINED FUNCTION POINTERS
PART-6.1 Friday, February 11, 2011 3 PRADEEP DWIVEDI(pur.B.TECH-IT)
USER DEFINED FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 4 Every c program collection of one or more functions. It consist some predefine function and some user define function. main() is a user define function which is first executed.
ELEMENTS OF A USER DEFINED FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 5 Function prototyping/declaration. function definition. function invocation/calling.
FUNCTION DECLARATION/PROTOTYPING Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 6 Like variables , all functions in a c program must be declared, before they are invoked. A function declaration (also known as function prototype) consists of four parts-   function type(return type). function name. parameter list. terminating semicolon. eg:-    int sum(inta,int b);      or     int sum(int,int);
FUNCTION DECLARATION/PROTOTYPING Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 7 return type(function type) specifies that what type of value do you want to return. function name specifies the name of function this can be anything which do you want. in parameterized function at the time of declaration we specifies the parameter in parentheses as data type of parameter. (variable is optional) in declaration of a function after parentheses we must terminate by semicolon(;).
DEFINITION OF FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 8 A function definition, also known as function implementation shall include the following elements. Function name. Function type. List of parameter. Local variable declaration. Function statement. A return statement.
DEFINITION OF FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 9 Format of function definition given by a example- int sum(inta,int b) 			{ 			c=a+b; 			return c; 			} Function_type(return_type) Function name Parameter list Function statement return statement
FUNCTION CALLING Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 10 A function can be called by simply using the function name followed by the list actual parameter(if any), enclosed by parenthes. eg:- y=sum(5,10); ,[object Object],[object Object]
NOTE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 12 We can declare and invoke a function inside another function . But we can not define the body of a function inside any other function. Function execution always depends on function invocation . It never depends on the declaration or the definition. main() is a predefined function which declared in c library and called by the c compiler. so there is no need to declare or call the main() function and only define the body of main() function.
PROG2 //prog2 #include<stdio.h> #include<conio.h> void main() { void italy(); void brazil();          void argentina(); clrscr(); printf("I am in main"); italy(); brazil();		 argentina();                   getch(); } void italy() { printf("I am in italy"); } void brazil() { printf("I am in brazil"); } void argentina() { printf("I am in argentina"); }  Friday, February 11, 2011 13 PRADEEP DWIVEDI(pur.B.TECH-IT) Control comes here Function declaration Print this line Function calling
NOTE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 14 When we invoke a function inside another function these always execute in stack form. In other words , a function that is invoked at last would we the first one two finish.
prog3 //prog- function call inside another function. #include<stdio.h> #include<conio.h> void main() { void italy();  printf("I am in main() "); italy();    printf("I am finally back in main()"); getch(); } void italy() { void brazil();   printf("I am in italy"); brazil(); printf("I am back in italy"); } void brazil() { void argentina(); printf("I am in brazil"); argentina(); } void argentina() { printf("I am in argentina"); } Friday, February 11, 2011 15 PRADEEP DWIVEDI(pur.B.TECH-IT) function declaration print this line function calling control comes here
return KEYWORD Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 16 return is a keyword with the help of that we return a value and a value is always returned where from the function being called.  in the case of main() function value is return to the compiler. because main() function called by the compiler.
PART-6.2 Friday, February 11, 2011 17 PRADEEP DWIVEDI(pur.B.TECH-IT)
CATEGORIES OF A FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 18 On the bases of parameter function can be categories into two categories. Non parameterized function are those function in which we do not pass any parameter. Parameterized function are those function in which we pass a parameter.
Prog4(based on parameterized-function) //w.a.p. to add two number by using parameterized function #include<stdio.h> #include<conio.h> void main() { inta,b,sum; intcalsum(int,int); clrscr(); printf("Enter two values"); scanf("%d%d",&a,&b); sum=calsum(a,b); printf(" sum=%d",sum); getch(); } intcalsum(intx,int y) { int d; d=x+y; return(d); } Friday, February 11, 2011 19 PRADEEP DWIVEDI(pur.B.TECH-IT) Function calling Function declaration
TERMS RELATED TO PROGRAM Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 20 the declaration of variable specifies that when we call the calsum() function it take two integer value.  when we call calsum(a,b); function it takes two parameter  and these values are stored in variable x and y. after calculating the expression d=a+b; the value of d is returned where from which the function is called and the value of d is stored in variable sum. in nest statement the value of sum is printed out.
PARAMETERIZED FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 21 to invoke(call) a parameterize function there are two ways- call by vale. call by reference.
CALL BY VALUE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 22 if we pass the value as a function parameter that is known as call by value. in call by value if we make some changes in the called function these changes will not be appeared at the original place. (in the calling function). calling function are those function those invoke any other function.(where the function is being calling). called function are those function those are invoked by any other function. in the call by value the variable always maintain the separate-separate copy for the calling function and called function that is why no changes are reflected takes place.
prog5 //prog to swap the variable (using call by value). #include<stdio.h> #include<conio.h> void main() { int a=10; int b=20; clrscr(); void swapv(int,int); swapv(a,b); printf("a=%d",a); printf("b=%d",b); getch(); } void swapv(intx,int y) { int t; t=x; x=y; y=t; printf("x=%d",x); printf("y=%d",y); }  Friday, February 11, 2011 23 PRADEEP DWIVEDI(pur.B.TECH-IT) function declaration calling function  and pass (a,b) means(10,20) control comes here with x=10 y=20 control comes here print  x=20 y=10 print  a=10 b=20
CALL BY REFERENCE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 24  when we pass address as a function  parameter this is called call by reference. in call by reference, if we make some changes into the called function, these changes will also be appeared in the calling function. because they don’t  maintain the separate copy of the variables. CALL BY REFERENCE DISCUSSED IN DETAIL AFTER STUDYING THE POINTERS
PART-6.3 Friday, February 11, 2011 25 PRADEEP DWIVEDI(pur.B.TECH-IT)
POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 26 A pointer is a derived data type in c. it is built from one of the fundamental data types available in c. Pointer variable is a special variable that holds the address of any other variable. If we want to hold the address of any integer variable that time we need an integer pointer variable and so on. A pointer variable prefix asterisk sign(*) at declaration time. Eg:- int *p;
TAKE AN EXAMPLE FOR UNDERSTANDING POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 27 int a=20; it stores in memory as-                                 a                              5000 ,[object Object],integer variable declaration variable name 20 value address
TAKE AN EXAMPLE FOR UNDERSTANDING POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 28 to store the address we declare a pointer variable- int *p;  p=&a; (p is a pointer variable which holds the address) *p=25; (means this stores the 25 at address 5000) integer pointer variable declaration  p=5000 value at address
POINTER DECLARATION STYLE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 29 pointer variables are declared similarly as normal variables except for the addition of unary * operator. this symbol can appear anywhere between the type name and the pointer variable name. programmers use the following styles- int*       p; int   *p; int     *     p; style 1 style 2(generally used) style 3
prog6 //Demo for pointer #include<stdio.h> #include<conio.h> void main() { inti=3,*x; float j=1.5,*y; char k='c',*z; printf("the value of i=%d",i); printf("the value of j=%f",j); printf("the value of k=%c",k); x=&i; y=&j; z=&k; printf("original value of x=%d",x); printf("original value of y=%d",y); printf("original value of z=%d",z); x++; y++; z++; printf("new value of x=%d",x); printf("new value of y=%d",y); printf("new value of z=%d",z); getch(); } Friday, February 11, 2011 30 PRADEEP DWIVEDI(pur.B.TECH-IT)
EXPLAINATION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 31 step1	I	j	k	 x	 y	 z 		10	20	30	 40	 50	 60 step2 		 10	20	30	40	50	60 step3 		10	20	30	40	50	60	 3 1.5 c variable value address(suppose) 3 1.5 c 10 20 30 3 1.5 c 12 24 31
POINTER INCREMENT AND SCALE FACTOR Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 32 when we increment a pointer, its value is increased by the length of the data type. this length is called scale factors. eg:- let p1 is an integer pointer with an initial value says- 2800, then after operation p1=p1+1; the value of it will be 2802 , and not 2801. the length of various data type are as follows- character		1 byte integer		2 bytes float		4 bytes long integer 	4 bytes double		8 bytes
prog7 Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 33 //This demo is for pointer increment and scale factor void main() { inti=4,*j,*k; clrscr(); j=&i; printf("The value of j:%d",j); j=j+1; printf("The value of j:%d",j); k=j+3; printf("The value of k:%d",k); getch(); } Suppose &i=40 Address of I is assign to J Address of i(value of j) is printed Vlue of j is increased by 1(40+2=42)  Vlue of j is increased by 3(42+6=48)
POINTER EXPRESSION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 34 int *p1,*p2; y=*p1 * *p2;=(*p1)*(*p2)  z=*p1/*p2  we may not use pointers in division or multiplication. p1/p2 p1*p2 p1/3 valid (because these are the value at address) allowed c allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer from another. p1+4; p2-2;          p1-p2;  invalid (because these are the  address)
RULES FOR POINTER OPERATION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 35 a pointer variable can be assigned the address of another variable. a pointer variable can be assigned the value of another pointer variable. a pointer variable can be assigned with zero or NULL value. a pointer variable can prefixed or post fixed with increment or decrement operator. an integer value may be added or subtract a pointer variable. when two pointer point to the same array, one pointer variable can be subtracted from another. when two pointer points to the object of same data types, they can be compared using relational operator. a pointer variable can’t be multiplied by a constant. two pointer variable can’t be added a value can not be assigned to an arbitary address. 		(i.e.   & =10   is illegal)
CHAIN OF POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 36 int  a=10;               p2                   p1                 a p1 holds the address of integer variable int *p1; p1=&a; p2 holds the address of integer  pointer variable int **p2; p2=&p1; value address1 address2
prog8 //prog-for chain of pointer #include<stdio.h> #include<conio.h> void main() { int x,*p1,**p2; clrscr(); x=100; p1=&x; p2=&p1; printf("Address of a=%d",p1); printf("Address of p1=%d",p2); printf("Value at address p1=%d",**p2); printf("Value at address p2=%d",*p2); getch(); } explanation:- in last statement second last statement **p2=*(*p2)         =*(value at address p2) 	=*(p1) 	=value at address p1 	=100 Friday, February 11, 2011 37 PRADEEP DWIVEDI(pur.B.TECH-IT)
PART-6.4 Friday, February 11, 2011 38 PRADEEP DWIVEDI(pur.B.TECH-IT)
POINTER vs ARRAY Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 39 Array is also used to hold the address but, array variable used to holds the base address of an array. The term base address means address of very first element in the array. Suppose we declare an array- int x[5]={1,2,3,4,5};
POINTER vs ARRAY Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 40       x[0]   x[1]     x[2]    x[3]       x[4]        element                                                               value 	 1000  1002   1004  1006  1008        address 		      base address	 			i.e.   x=&x[0]=1000; x+1=&x[1]=1002 x+2=&x[2]=1004 x+3=&x[3]=1006 x+4=&x[4]=1008 with the help of base address we can calculate the address of any element by using its index and the scale factor of the data type address of x[3]=base address+(3*scale factor of int) =1000+(3*2)=1006
IMPORTANT POINT Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 41 An array variable is always having address of it zero index by default. Array variable can hold the address of only zero index element these can not hold the address of any other element. But pointer can hold the address of any element in the list. An array variable is a reference variable it is little bit similar with pointer.
prog9 //prog- pointer vs array #include<stdio.h> #include<conio.h> void main() { intarr[]={10,20,36,72,45,36}; int *j,*k; clrscr(); j=&arr[4]; k=(arr+4); if(j==k) printf("Two pointers point two the same location"); else printf("Two pointers do not point to the same location"); getch(); }  Friday, February 11, 2011 42 PRADEEP DWIVEDI(pur.B.TECH-IT) address of arr[4]=&arr[4] (arr+4) arr[0]+4=&arr[4] true
prog10 //wap to sibtract a pointer from anoter pointer #include<stdio.h> #include<conio.h> void main() { intarr[]={10,20,30,40,60,57,56}; int *i,*j; clrscr(); i=&arr[1]; j=&arr[5]; printf("value of i:%d",i); printf("value of j:%d",j); printf("The value at address i:%d",*i); printf("The value at address j:%d",*j); printf("The difference between address: %d",j-i); printf("The difference between values is %d",*j-*i); getch(); } Friday, February 11, 2011 43 PRADEEP DWIVEDI(pur.B.TECH-IT) I THINK THERE IS NO NEED TO EXPLIAN THIS PROGRAM
REMOVE CONFUSION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 44 I think pointer is very confusing topic in “C” Here we try to remove confusion – Suppose -     int *p; Pointer variable declared with (*) sign such as (*p) but address is stored in p not *p *p is always store the value at address Pointer variable declaration
POINTER AND CHARACTER STRINGS Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 45 The character arrays are declared and initialized as follows:-                    char str[5]=“good”; The compiler automatically inserts the null character ‘’ at the end of the string. C supports an alternative method to create string using pointer variable of type char. eg: char *str=“good”; ,[object Object],[object Object]
PROG11 Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 47 //PROG POINTER VS CHARACTER ARRAY #include<stdio.h> #include<conio.h> void main() { char arr[]="PRADEEP DWIVEDI"; char *s="HINDUSTAN COLLEGE OF SCIENCE AND TECHNOLOGY"; clrscr(); printf("%s",arr); printf("%s",s); printf("%d  %d",arr,s); getch(); } PRINT STRING PRINT ADDRESSES
SIZE OF OPERATOR Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 48 With the help of sizeof operator we can find out the size of value or variable that is occupied in the memory. Note:- each and every pointer variable always occupied two bytes in memory either it is float or char or int or etc.
prog12.c Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 49 //PROG- DEMO FOR SIZE OF OPERATOR. #include<stdio.h> #include<conio.h> void main() { char arr[10]="HINDUSTAN"; char *s="HINDUSTAN"; float *p; clrscr(); s++; //arr++; printf("%s",arr); printf("%s",s); printf("%d %d %d",sizeof(s),sizeof(arr),sizeof(p)); getch(); } print  HINDUSTAN print  INDUSTAN BECAUSE ADDRESS OF S IS INCREASED BY ONE CHARACTER THIS PRINT THE SIZE AS- 2       10        2
CALL BY REFERENCE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 50  when we pass address as a function  parameter this is called call by reference. in call by reference, if we make some changes into the called function, these changes will also be appeared in the calling function. because they don’t  maintain the separate copy of the variables.
prog13 //PROG-WAP FOR SWAPING void main() { int a=10; int b=20; void swapv(int*,int*); clrscr(); swapv(&a,&b); printf("a=%d",a); printf("b=%d",b); getch(); } void swapv(int *x,int *y) { int t; t=*x; *x=*y; *y=t; printf("*x=%d",*x); printf("*y=%d",*y); getch(); } Friday, February 11, 2011 51 PRADEEP DWIVEDI(pur.B.TECH-IT) WHEN WE RUN THIS PROGRAM THIS PROGRAM PRODUCE THE CHANGE IN SWAPV FUNCTION AND MAIN FUNCTION ALSO BECAUSE VALUE IS CHANGE AT ADDRESS function prototyping function  calling pass addresses stores    x=&a; y=&b; control comes here print the value at addres  20               10 values are also change here a=20      b=10
FUNCTION RETURNING POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 52 When we declare  a function as pointer type then function return pointer type value (address ).
prog14 //PROG- TO FIND THE MAXIMUM NUMBER #include<stdio.h> #include<conio.h> int *larger(int*,int*);//prototype void main() { int a=10; int b=20; int *p; clrscr(); p=larger(&a,&b);//function call printf("Greater number is:  %d",*p); getch(); } int *larger(int *x,int *y) { if(*x>*y) return(x);//address of a else return(y);//address of b } Friday, February 11, 2011 53 PRADEEP DWIVEDI(pur.B.TECH-IT) try  to under stand it self
C programming slide-6
C programming slide-6

More Related Content

What's hot

UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
JavvajiVenkat
 
C Prog. - Decision & Loop Controls
C Prog. - Decision & Loop ControlsC Prog. - Decision & Loop Controls
C Prog. - Decision & Loop Controls
vinay arora
 
C structure
C structureC structure
C structure
ankush9927
 
OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)
Kai-Feng Chou
 
Vcs15
Vcs15Vcs15
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
Kai-Feng Chou
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
indra Kishor
 
C++ Introduction
C++ IntroductionC++ Introduction
C++ Introduction
parmsidhu
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakur
Thakurkirtika
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
THE NORTHCAP UNIVERSITY
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
Mehmet Emin İNAÇ
 
Array strings
Array stringsArray strings
Array strings
Radhe Syam
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
Aditya Nihal Kumar Singh
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
Nitesh Kumar Pandey
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
imtiazalijoono
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
Akhil Ahuja
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
Nitesh Bichwani
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
Akira Takahashi
 
C language
C languageC language
C language
Mohamed Bedair
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
Sukrit Gupta
 

What's hot (20)

UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
C Prog. - Decision & Loop Controls
C Prog. - Decision & Loop ControlsC Prog. - Decision & Loop Controls
C Prog. - Decision & Loop Controls
 
C structure
C structureC structure
C structure
 
OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)OOP in C - Inherit (Chinese Version)
OOP in C - Inherit (Chinese Version)
 
Vcs15
Vcs15Vcs15
Vcs15
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
C++ Introduction
C++ IntroductionC++ Introduction
C++ Introduction
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakur
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Array strings
Array stringsArray strings
Array strings
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
 
C language
C languageC language
C language
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 

Viewers also liked

user defined function
user defined functionuser defined function
user defined function
King Kavin Patel
 
C programming slide c03
C programming slide c03C programming slide c03
C programming slide c03
pradeep dwivedi
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
programmings guru
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
Abu Zaman
 
C Tutorial
C TutorialC Tutorial
C Tutorial
biochelo
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
vinay arora
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
hossam nassar
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
Mohammed Jawad Ibne Ishaque (Taki)
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
vinay arora
 
C Prog. - Data Types, Variables and Constants
C Prog. - Data Types, Variables and ConstantsC Prog. - Data Types, Variables and Constants
C Prog. - Data Types, Variables and Constants
vinay arora
 
C Prog - Functions
C Prog - FunctionsC Prog - Functions
C Prog - Functions
vinay arora
 
C Prog. - ASCII Values, Break, Continue
C Prog. -  ASCII Values, Break, ContinueC Prog. -  ASCII Values, Break, Continue
C Prog. - ASCII Values, Break, Continue
vinay arora
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
vinay arora
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
CG - Introduction to Computer Graphics
CG - Introduction to Computer GraphicsCG - Introduction to Computer Graphics
CG - Introduction to Computer Graphics
vinay arora
 
Search engine and web crawler
Search engine and web crawlerSearch engine and web crawler
Search engine and web crawler
vinay arora
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
C Prog. - Introduction to Hardware, Software, Algorithm & Flowchart
C Prog. - Introduction to Hardware, Software, Algorithm & FlowchartC Prog. - Introduction to Hardware, Software, Algorithm & Flowchart
C Prog. - Introduction to Hardware, Software, Algorithm & Flowchart
vinay arora
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Beat Signer
 

Viewers also liked (20)

user defined function
user defined functionuser defined function
user defined function
 
C programming slide c03
C programming slide c03C programming slide c03
C programming slide c03
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
C Tutorial
C TutorialC Tutorial
C Tutorial
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
 
C Prog. - Data Types, Variables and Constants
C Prog. - Data Types, Variables and ConstantsC Prog. - Data Types, Variables and Constants
C Prog. - Data Types, Variables and Constants
 
C Prog - Functions
C Prog - FunctionsC Prog - Functions
C Prog - Functions
 
C Prog. - ASCII Values, Break, Continue
C Prog. -  ASCII Values, Break, ContinueC Prog. -  ASCII Values, Break, Continue
C Prog. - ASCII Values, Break, Continue
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
CG - Introduction to Computer Graphics
CG - Introduction to Computer GraphicsCG - Introduction to Computer Graphics
CG - Introduction to Computer Graphics
 
Search engine and web crawler
Search engine and web crawlerSearch engine and web crawler
Search engine and web crawler
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
C Prog. - Introduction to Hardware, Software, Algorithm & Flowchart
C Prog. - Introduction to Hardware, Software, Algorithm & FlowchartC Prog. - Introduction to Hardware, Software, Algorithm & Flowchart
C Prog. - Introduction to Hardware, Software, Algorithm & Flowchart
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
 

Similar to C programming slide-6

Functions
FunctionsFunctions
Functions
Mitali Chugh
 
Functions
FunctionsFunctions
Functions
Mitali Chugh
 
Savitch Ch 05
Savitch Ch 05Savitch Ch 05
Savitch Ch 05
Terry Yoast
 
Savitch Ch 05
Savitch Ch 05Savitch Ch 05
Savitch Ch 05
Terry Yoast
 
Savitch ch 05
Savitch ch 05Savitch ch 05
Savitch ch 05
Terry Yoast
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
Vikash Dhal
 
Programming in c function
Programming in c functionProgramming in c function
Programming in c function
Parvez Ahmed
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
Saurav Kumar
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
ppd1961
 
Function in cpu 2
Function in cpu 2Function in cpu 2
Function in cpu 2
Dhaval Jalalpara
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
Terry Yoast
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
Terry Yoast
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
MayankSinghRawat6
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
Function
FunctionFunction

Similar to C programming slide-6 (20)

Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Savitch Ch 05
Savitch Ch 05Savitch Ch 05
Savitch Ch 05
 
Savitch Ch 05
Savitch Ch 05Savitch Ch 05
Savitch Ch 05
 
Savitch ch 05
Savitch ch 05Savitch ch 05
Savitch ch 05
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Programming in c function
Programming in c functionProgramming in c function
Programming in c function
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Function in cpu 2
Function in cpu 2Function in cpu 2
Function in cpu 2
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
 
Function
FunctionFunction
Function
 

Recently uploaded

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 

Recently uploaded (20)

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 

C programming slide-6

  • 1. PREPARED BY:- PRADEEP DWIVEDI (pursuing B.TECH-IT) FROM H.C.S.T.(MATHURA) Mob-+919027843806 E-mail-pradeep.it74@gmail.com C-PROGRAMMING SLIDE-6 Friday, February 11, 2011 1 PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 2. C-6 Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 2 TOPIC:- USER DEFINED FUNCTION POINTERS
  • 3. PART-6.1 Friday, February 11, 2011 3 PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 4. USER DEFINED FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 4 Every c program collection of one or more functions. It consist some predefine function and some user define function. main() is a user define function which is first executed.
  • 5. ELEMENTS OF A USER DEFINED FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 5 Function prototyping/declaration. function definition. function invocation/calling.
  • 6. FUNCTION DECLARATION/PROTOTYPING Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 6 Like variables , all functions in a c program must be declared, before they are invoked. A function declaration (also known as function prototype) consists of four parts- function type(return type). function name. parameter list. terminating semicolon. eg:- int sum(inta,int b); or int sum(int,int);
  • 7. FUNCTION DECLARATION/PROTOTYPING Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 7 return type(function type) specifies that what type of value do you want to return. function name specifies the name of function this can be anything which do you want. in parameterized function at the time of declaration we specifies the parameter in parentheses as data type of parameter. (variable is optional) in declaration of a function after parentheses we must terminate by semicolon(;).
  • 8. DEFINITION OF FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 8 A function definition, also known as function implementation shall include the following elements. Function name. Function type. List of parameter. Local variable declaration. Function statement. A return statement.
  • 9. DEFINITION OF FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 9 Format of function definition given by a example- int sum(inta,int b) { c=a+b; return c; } Function_type(return_type) Function name Parameter list Function statement return statement
  • 10.
  • 11. NOTE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 12 We can declare and invoke a function inside another function . But we can not define the body of a function inside any other function. Function execution always depends on function invocation . It never depends on the declaration or the definition. main() is a predefined function which declared in c library and called by the c compiler. so there is no need to declare or call the main() function and only define the body of main() function.
  • 12. PROG2 //prog2 #include<stdio.h> #include<conio.h> void main() { void italy(); void brazil(); void argentina(); clrscr(); printf("I am in main"); italy(); brazil(); argentina(); getch(); } void italy() { printf("I am in italy"); } void brazil() { printf("I am in brazil"); } void argentina() { printf("I am in argentina"); } Friday, February 11, 2011 13 PRADEEP DWIVEDI(pur.B.TECH-IT) Control comes here Function declaration Print this line Function calling
  • 13. NOTE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 14 When we invoke a function inside another function these always execute in stack form. In other words , a function that is invoked at last would we the first one two finish.
  • 14. prog3 //prog- function call inside another function. #include<stdio.h> #include<conio.h> void main() { void italy(); printf("I am in main() "); italy(); printf("I am finally back in main()"); getch(); } void italy() { void brazil(); printf("I am in italy"); brazil(); printf("I am back in italy"); } void brazil() { void argentina(); printf("I am in brazil"); argentina(); } void argentina() { printf("I am in argentina"); } Friday, February 11, 2011 15 PRADEEP DWIVEDI(pur.B.TECH-IT) function declaration print this line function calling control comes here
  • 15. return KEYWORD Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 16 return is a keyword with the help of that we return a value and a value is always returned where from the function being called. in the case of main() function value is return to the compiler. because main() function called by the compiler.
  • 16. PART-6.2 Friday, February 11, 2011 17 PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 17. CATEGORIES OF A FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 18 On the bases of parameter function can be categories into two categories. Non parameterized function are those function in which we do not pass any parameter. Parameterized function are those function in which we pass a parameter.
  • 18. Prog4(based on parameterized-function) //w.a.p. to add two number by using parameterized function #include<stdio.h> #include<conio.h> void main() { inta,b,sum; intcalsum(int,int); clrscr(); printf("Enter two values"); scanf("%d%d",&a,&b); sum=calsum(a,b); printf(" sum=%d",sum); getch(); } intcalsum(intx,int y) { int d; d=x+y; return(d); } Friday, February 11, 2011 19 PRADEEP DWIVEDI(pur.B.TECH-IT) Function calling Function declaration
  • 19. TERMS RELATED TO PROGRAM Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 20 the declaration of variable specifies that when we call the calsum() function it take two integer value. when we call calsum(a,b); function it takes two parameter and these values are stored in variable x and y. after calculating the expression d=a+b; the value of d is returned where from which the function is called and the value of d is stored in variable sum. in nest statement the value of sum is printed out.
  • 20. PARAMETERIZED FUNCTION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 21 to invoke(call) a parameterize function there are two ways- call by vale. call by reference.
  • 21. CALL BY VALUE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 22 if we pass the value as a function parameter that is known as call by value. in call by value if we make some changes in the called function these changes will not be appeared at the original place. (in the calling function). calling function are those function those invoke any other function.(where the function is being calling). called function are those function those are invoked by any other function. in the call by value the variable always maintain the separate-separate copy for the calling function and called function that is why no changes are reflected takes place.
  • 22. prog5 //prog to swap the variable (using call by value). #include<stdio.h> #include<conio.h> void main() { int a=10; int b=20; clrscr(); void swapv(int,int); swapv(a,b); printf("a=%d",a); printf("b=%d",b); getch(); } void swapv(intx,int y) { int t; t=x; x=y; y=t; printf("x=%d",x); printf("y=%d",y); } Friday, February 11, 2011 23 PRADEEP DWIVEDI(pur.B.TECH-IT) function declaration calling function and pass (a,b) means(10,20) control comes here with x=10 y=20 control comes here print x=20 y=10 print a=10 b=20
  • 23. CALL BY REFERENCE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 24 when we pass address as a function parameter this is called call by reference. in call by reference, if we make some changes into the called function, these changes will also be appeared in the calling function. because they don’t maintain the separate copy of the variables. CALL BY REFERENCE DISCUSSED IN DETAIL AFTER STUDYING THE POINTERS
  • 24. PART-6.3 Friday, February 11, 2011 25 PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 25. POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 26 A pointer is a derived data type in c. it is built from one of the fundamental data types available in c. Pointer variable is a special variable that holds the address of any other variable. If we want to hold the address of any integer variable that time we need an integer pointer variable and so on. A pointer variable prefix asterisk sign(*) at declaration time. Eg:- int *p;
  • 26.
  • 27. TAKE AN EXAMPLE FOR UNDERSTANDING POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 28 to store the address we declare a pointer variable- int *p; p=&a; (p is a pointer variable which holds the address) *p=25; (means this stores the 25 at address 5000) integer pointer variable declaration p=5000 value at address
  • 28. POINTER DECLARATION STYLE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 29 pointer variables are declared similarly as normal variables except for the addition of unary * operator. this symbol can appear anywhere between the type name and the pointer variable name. programmers use the following styles- int* p; int *p; int * p; style 1 style 2(generally used) style 3
  • 29. prog6 //Demo for pointer #include<stdio.h> #include<conio.h> void main() { inti=3,*x; float j=1.5,*y; char k='c',*z; printf("the value of i=%d",i); printf("the value of j=%f",j); printf("the value of k=%c",k); x=&i; y=&j; z=&k; printf("original value of x=%d",x); printf("original value of y=%d",y); printf("original value of z=%d",z); x++; y++; z++; printf("new value of x=%d",x); printf("new value of y=%d",y); printf("new value of z=%d",z); getch(); } Friday, February 11, 2011 30 PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 30. EXPLAINATION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 31 step1 I j k x y z 10 20 30 40 50 60 step2 10 20 30 40 50 60 step3 10 20 30 40 50 60 3 1.5 c variable value address(suppose) 3 1.5 c 10 20 30 3 1.5 c 12 24 31
  • 31. POINTER INCREMENT AND SCALE FACTOR Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 32 when we increment a pointer, its value is increased by the length of the data type. this length is called scale factors. eg:- let p1 is an integer pointer with an initial value says- 2800, then after operation p1=p1+1; the value of it will be 2802 , and not 2801. the length of various data type are as follows- character 1 byte integer 2 bytes float 4 bytes long integer 4 bytes double 8 bytes
  • 32. prog7 Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 33 //This demo is for pointer increment and scale factor void main() { inti=4,*j,*k; clrscr(); j=&i; printf("The value of j:%d",j); j=j+1; printf("The value of j:%d",j); k=j+3; printf("The value of k:%d",k); getch(); } Suppose &i=40 Address of I is assign to J Address of i(value of j) is printed Vlue of j is increased by 1(40+2=42) Vlue of j is increased by 3(42+6=48)
  • 33. POINTER EXPRESSION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 34 int *p1,*p2; y=*p1 * *p2;=(*p1)*(*p2) z=*p1/*p2 we may not use pointers in division or multiplication. p1/p2 p1*p2 p1/3 valid (because these are the value at address) allowed c allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer from another. p1+4; p2-2; p1-p2; invalid (because these are the address)
  • 34. RULES FOR POINTER OPERATION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 35 a pointer variable can be assigned the address of another variable. a pointer variable can be assigned the value of another pointer variable. a pointer variable can be assigned with zero or NULL value. a pointer variable can prefixed or post fixed with increment or decrement operator. an integer value may be added or subtract a pointer variable. when two pointer point to the same array, one pointer variable can be subtracted from another. when two pointer points to the object of same data types, they can be compared using relational operator. a pointer variable can’t be multiplied by a constant. two pointer variable can’t be added a value can not be assigned to an arbitary address. (i.e. & =10 is illegal)
  • 35. CHAIN OF POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 36 int a=10; p2 p1 a p1 holds the address of integer variable int *p1; p1=&a; p2 holds the address of integer pointer variable int **p2; p2=&p1; value address1 address2
  • 36. prog8 //prog-for chain of pointer #include<stdio.h> #include<conio.h> void main() { int x,*p1,**p2; clrscr(); x=100; p1=&x; p2=&p1; printf("Address of a=%d",p1); printf("Address of p1=%d",p2); printf("Value at address p1=%d",**p2); printf("Value at address p2=%d",*p2); getch(); } explanation:- in last statement second last statement **p2=*(*p2) =*(value at address p2) =*(p1) =value at address p1 =100 Friday, February 11, 2011 37 PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 37. PART-6.4 Friday, February 11, 2011 38 PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 38. POINTER vs ARRAY Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 39 Array is also used to hold the address but, array variable used to holds the base address of an array. The term base address means address of very first element in the array. Suppose we declare an array- int x[5]={1,2,3,4,5};
  • 39. POINTER vs ARRAY Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 40 x[0] x[1] x[2] x[3] x[4] element value 1000 1002 1004 1006 1008 address base address i.e. x=&x[0]=1000; x+1=&x[1]=1002 x+2=&x[2]=1004 x+3=&x[3]=1006 x+4=&x[4]=1008 with the help of base address we can calculate the address of any element by using its index and the scale factor of the data type address of x[3]=base address+(3*scale factor of int) =1000+(3*2)=1006
  • 40. IMPORTANT POINT Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 41 An array variable is always having address of it zero index by default. Array variable can hold the address of only zero index element these can not hold the address of any other element. But pointer can hold the address of any element in the list. An array variable is a reference variable it is little bit similar with pointer.
  • 41. prog9 //prog- pointer vs array #include<stdio.h> #include<conio.h> void main() { intarr[]={10,20,36,72,45,36}; int *j,*k; clrscr(); j=&arr[4]; k=(arr+4); if(j==k) printf("Two pointers point two the same location"); else printf("Two pointers do not point to the same location"); getch(); } Friday, February 11, 2011 42 PRADEEP DWIVEDI(pur.B.TECH-IT) address of arr[4]=&arr[4] (arr+4) arr[0]+4=&arr[4] true
  • 42. prog10 //wap to sibtract a pointer from anoter pointer #include<stdio.h> #include<conio.h> void main() { intarr[]={10,20,30,40,60,57,56}; int *i,*j; clrscr(); i=&arr[1]; j=&arr[5]; printf("value of i:%d",i); printf("value of j:%d",j); printf("The value at address i:%d",*i); printf("The value at address j:%d",*j); printf("The difference between address: %d",j-i); printf("The difference between values is %d",*j-*i); getch(); } Friday, February 11, 2011 43 PRADEEP DWIVEDI(pur.B.TECH-IT) I THINK THERE IS NO NEED TO EXPLIAN THIS PROGRAM
  • 43. REMOVE CONFUSION Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 44 I think pointer is very confusing topic in “C” Here we try to remove confusion – Suppose - int *p; Pointer variable declared with (*) sign such as (*p) but address is stored in p not *p *p is always store the value at address Pointer variable declaration
  • 44.
  • 45. PROG11 Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 47 //PROG POINTER VS CHARACTER ARRAY #include<stdio.h> #include<conio.h> void main() { char arr[]="PRADEEP DWIVEDI"; char *s="HINDUSTAN COLLEGE OF SCIENCE AND TECHNOLOGY"; clrscr(); printf("%s",arr); printf("%s",s); printf("%d %d",arr,s); getch(); } PRINT STRING PRINT ADDRESSES
  • 46. SIZE OF OPERATOR Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 48 With the help of sizeof operator we can find out the size of value or variable that is occupied in the memory. Note:- each and every pointer variable always occupied two bytes in memory either it is float or char or int or etc.
  • 47. prog12.c Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 49 //PROG- DEMO FOR SIZE OF OPERATOR. #include<stdio.h> #include<conio.h> void main() { char arr[10]="HINDUSTAN"; char *s="HINDUSTAN"; float *p; clrscr(); s++; //arr++; printf("%s",arr); printf("%s",s); printf("%d %d %d",sizeof(s),sizeof(arr),sizeof(p)); getch(); } print HINDUSTAN print INDUSTAN BECAUSE ADDRESS OF S IS INCREASED BY ONE CHARACTER THIS PRINT THE SIZE AS- 2 10 2
  • 48. CALL BY REFERENCE Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 50 when we pass address as a function parameter this is called call by reference. in call by reference, if we make some changes into the called function, these changes will also be appeared in the calling function. because they don’t maintain the separate copy of the variables.
  • 49. prog13 //PROG-WAP FOR SWAPING void main() { int a=10; int b=20; void swapv(int*,int*); clrscr(); swapv(&a,&b); printf("a=%d",a); printf("b=%d",b); getch(); } void swapv(int *x,int *y) { int t; t=*x; *x=*y; *y=t; printf("*x=%d",*x); printf("*y=%d",*y); getch(); } Friday, February 11, 2011 51 PRADEEP DWIVEDI(pur.B.TECH-IT) WHEN WE RUN THIS PROGRAM THIS PROGRAM PRODUCE THE CHANGE IN SWAPV FUNCTION AND MAIN FUNCTION ALSO BECAUSE VALUE IS CHANGE AT ADDRESS function prototyping function calling pass addresses stores x=&a; y=&b; control comes here print the value at addres 20 10 values are also change here a=20 b=10
  • 50. FUNCTION RETURNING POINTER Friday, February 11, 2011 PRADEEP DWIVEDI(pur.B.TECH-IT) 52 When we declare a function as pointer type then function return pointer type value (address ).
  • 51. prog14 //PROG- TO FIND THE MAXIMUM NUMBER #include<stdio.h> #include<conio.h> int *larger(int*,int*);//prototype void main() { int a=10; int b=20; int *p; clrscr(); p=larger(&a,&b);//function call printf("Greater number is: %d",*p); getch(); } int *larger(int *x,int *y) { if(*x>*y) return(x);//address of a else return(y);//address of b } Friday, February 11, 2011 53 PRADEEP DWIVEDI(pur.B.TECH-IT) try to under stand it self