SlideShare a Scribd company logo
1 of 6
Download to read offline
2015-11-20
1
Unit 4
Functions
• Set of instructions defined to perform
specified task.
• Reduces complexity.
• Provide modularity.
• Easy for error detection and
debugging.
• Divide complex tasks into manageable
tasks.
Types of Functions
• Two types.
• Pre defined functions –
e.g. main(), strlen(), etc.,
• User defined functions –
e.g. add() – a sub-function
to perform addition.
Need & Advantages of Functions
• Program is too large and complex.
• Debugging, testing becomes difficult.
• Reduces length.
• Easy to detect and debug errors.
• Top-down approach.
• Avoids coding of repeated execution.
Three elements of Function
• Function declaration
• Function calling
• Function definition
• Declaration of a sub function in main
function – Function Declaration
• Calling of sub function in main function –
Function Calling
• Execution of main function – Function
Definition
• Return to the main function.
• Start executing the next statement after the
function calling statement.
• Syntax of function Declaration section
<<Return type>> funname(parameter list);
• Syntax of function Definition section
<<Return type>> funname(parameter list)
{
body of the function
}
• Syntax of function calling section
Funname(parameters);
2015-11-20
2
Types of parameters
• Actual parameters - used during a function
call
• Formal parameters - used in function
definition and function declaration
Example function to display a value
#include<stdio.h>
void main()
{
void fun(int); //declaration
fun(10); //Call – Actual Parameters
}
void fun(int x) //definition – Formal Parameters
{
printf(“%d”,x);
}
Return statement
• Used when sub function returns some
integer value to the main function.
• Syntax:
•return(value);
Or
•return variable;
• Example:
•return(0); or return a;
Function prototypes
Depending upon arguments function is classified
into
•Function with no arguments and no
return value.
•Function with arguments and no return
value
•Function with arguments and with return
value.
•Function with no arguments and with
return value.
Function with no arguments and
no return value
#include<stdio.h>
void main()
{
void fun(); //no arguments
fun();
}
void fun()
{
int x=5;
printf(“%d”,x); //no return type
}
Function with arguments and no
return value
#include<stdio.h>
void main()
{
void fun(int); //integer argument
fun(10);
}
void fun(int x)
{
printf(“%d”,x); //no return value
}
2015-11-20
3
Function with arguments and with
return value
#include<stdio.h>
void main()
{
int a;
int fun(int); //integer argument & integer return type
a=fun(10);
printf(“%d”,a); //a=11
}
int fun(int x)
{
x++;
return x; //return integer value
}
Function with no arguments and
with return value
#include<stdio.h>
void main()
{
int a;
int fun(); //no argument & integer return type
a=fun();
printf(“%d”,a); // a= 5
}
int fun()
{
int x=5;
return x; //return value
}
Parameter Passing Methods
Two ways of passing parameters to a function
are
• Call by value
• Copies the values of actual parameters
into formal parameters and actual
parameters doesn’t change.
• Call by reference
• Copies the address of the actual
parameters into the formal parameters.
Call by Value
• Calling a function with parameters passed as
values
int a=10; void fun(int a)
fun(a); {
defn;
}
Here fun(a) is a call by value.
Any modification done with in the function is local to
it and will not be effected outside the function
Call by reference
• Calling a function by passing pointers as
parameters (address of variables is passed instead
of variables)
int a=1; void fun(int *x)
fun(&a); {
defn;
}
Any modification done to variable a will effect
outside the function also
Example program – Call by value
#include<stdio.h>
void main()
{
int a=10;
printf(“%d”,a); a=10
fun(a);
printf(“%d”,a); a=10
}
void fun(int x)
{
printf(“%d”,x) x=10
x++;
printf(“%d”,x); x=11
}
2015-11-20
4
Explanation – Call by value
Example Program – Call by
reference
#include<stdio.h>
void main()
{
int a=10;
printf(“%d”,a); a=10
fun(&a);
printf(“%d”,a); a=10
}
void fun(int *x)
{
int t;
t=*x;
printf(“%d”,t) x=10
t++;
printf(“%d”,t); x=11
}
a and x are referring to same location. So value will be over
written.
Explanation – Call by reference Conclusion
• Call by value => copying value of variable
in another variable. So any change made
in the copy will not affect the original
location.
• Call by reference => Creating link for the
parameter to the original location. Since
the address is same, changes to the
parameter will refer to original location
and the value will be over written.
Recursion or Recursive Function
Definition: A function is called recursion or recursive function if it calls
by itself i.e. the statements inside the function has a call to the same
function.
e.g.
void main( )
{
void add( );
…..
add();
}
add( )
{
add( );
}
Function declaration
Function calling
Function definition
Advantages of Recursion
• Useful for branching processes
•Written with less number of statements.
•Requires only few variables which requires program clean
•Recursive functions are more effective in computing values
Disadvantages of Recursion
•Hard to think logic of the function.
•Difficult to debug the code containing recursion.
2015-11-20
5
Applications of Recursive
function
•Factorial of a number
•Finding length of the string
•Fibonacci series generation
•Towers of hanoi program
Factorial - Recursion
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,b;
int fact(int);
clrscr();
printf(“nEnter the value to compute:”);
scanf(“%d”,&n);
b=fact(n);
printf(“nFactorial of %d is %d”,n,b);
getch();
}
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*fact(n-1);
}
Fibonacci - Recursion
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,n;
int fibo(int);
clrscr( );
printf(“nEnter no. of elements in series:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“%d”,fibo(i));
}
getch( );
}
void fibo(int i)
{
if(i==0)
{
return 0;
}
if(i==1)
{
return 1;
}
return fibo(i-1)+fibo(n-2);
}
Towers of Hanoi - Recursion
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int disk, moves;
void hanoi(int,char,char,char);
clrscr( );
printf(“nEnter no. of disks:”);
scanf(“%d”,&disk);
moves=pow(2,disk)-1;
printf(“nNo.of moved req:%d”,moves);
hanoi(disk, ‘A’, ‘C’, ‘B’);
getch( );
}
void hanoi(int x,char from, char to, char aux)
{
if(x==1)
printf(“nMove disk from %c to %c”,from,to);
else
{
hanoi(x-1,from,aux,to);
printf(“nMove disk from %c to %c”,from,to);
hanoi(x-1,aux,to,from);
}
}
• Pointer
• Definition
• Initialization
• Pointers arithmetic
• Pointers and arrays
• Eg programs
Pointers
Definition:
Pointer is a variable that holds the address of variable or
function.
It is declared same as other variable but the variable
preceded by *(asterisk) (i.e) indirection operator
Syntax: datatype *variable_name;
eg: int *a,*c; char *b;
Here a stores address of integer variable and b stores
address of character variable.
It is a variable whose value is the address of another
variable(same data type).
It is also called address variable and it is a derived data type
Pointers can be used to access and manipulate data stored
in the memory
2015-11-20
6
Features of Pointers
• More effective and useful in handling
arrays.
• Used to return multiple values from a
function.
• Supports dynamic memory management.
• Reduces complexity and length of a
program.
• Reduces execution time of a program.
Limitations of Pointers
• Should have prefix “*”
• Combination of data types not allowed
Accessing the address of a variable:
Address for the variable is given by
machine
The symbol &(address of operator) is used
to get the address of the variable.
Syntax: &variable_name;#include<stdio.h>
#include<conio.h>
void main()
{
int *a , b=10;
a=&b;
clrscr();
printf("n Address of variable b is
%u",a);
//printf("n Address of variable b is
%u",&b);
printf("n value of b is %d ",*a);
getch();
}
10
b
655
24
655
24
655
24
avaria
ble
655
22
655
22
value
addr
ess
&b=65524
&a= 65522
Value of a=
65524
Value of b=10
a=&b;  a=addr of
b(65524)
*a  value of a
(65524)  10
Initialization of variable:
• The process of assigning the address of a variable to a
pointer variable is known as initialization.
• Eg: p=&n;  p contains address of variable n, the data
item of n can be accessed by *p
Initialize pointer variable while declaring it:
int n=15;
int *p=&n; initialization while declaring
#include<stdio.h>
#include<conio.h>
void main()
{
int n=15;
int *p;
p=&n;
clrscr();
printf("%d",*p);
getch();
}
15
#include<stdio.h>
#include<conio.h>
void main()
{
int n=15;
int *p=&n;
clrscr();
printf("%d",*p);
getch();
}
15
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int *c,*d;
clrscr();
printf("nEnter the values of a and b:");
scanf("%d%d",&a,&b);
c=&a;
d=&b;
printf("nValue of a is:%d",a);
printf("nValue of a is:%d",*c);
printf("nAddress of a is:%u",&a);
printf("nAddress of a is:%u",c);
printf("nValue of b is:%d",b);
printf("nValue of b is:%d",*d);
printf("nAddress of b is:%u",&b);
printf("nAddress of b is:%u",d);
printf("nAddress of pointer variable c
is:%u",&c);
printf("nAddress of pointer variable d
is:%u",&d);
getch();
}
Double pointer
Definition: is a variable used to store address of another
pointer variable.
Syntax:
data_type **var_name;
e.g.
void main()
{
int c;
int *b,**a;
b=&c; //b displays address of c and *b displays values of c
a=&b; //a->double pointer variable and displays address of b
and *a displays address of c
}
Double pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int n=15;
int *p=&n;
int **a=&p;
clrscr() ;
printf("n *p is %u",*p);
printf("n **a ia %u",**a);
getch();
}

More Related Content

What's hot (20)

Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function
Function Function
Function
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Built in function
Built in functionBuilt in function
Built in function
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Function
FunctionFunction
Function
 
Function in c++
Function in c++Function in c++
Function in c++
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Functions in C
Functions in CFunctions in C
Functions in C
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function C++
Function C++ Function C++
Function C++
 
Functions
Functions Functions
Functions
 
Function lecture
Function lectureFunction lecture
Function lecture
 
functions of C++
functions of C++functions of C++
functions of C++
 

Viewers also liked

Paranas (wright) - 2016 Official Ballot
Paranas (wright) - 2016 Official BallotParanas (wright) - 2016 Official Ballot
Paranas (wright) - 2016 Official BallotCalbayog Journal
 
San sebastian - 2016 Official Ballot
San sebastian - 2016 Official BallotSan sebastian - 2016 Official Ballot
San sebastian - 2016 Official BallotCalbayog Journal
 
Vacantes provinciale gu tras sentencia
Vacantes provinciale gu tras sentenciaVacantes provinciale gu tras sentencia
Vacantes provinciale gu tras sentenciasindicatosatif
 
Pagsanghan - 2016 Official Ballot
Pagsanghan - 2016 Official BallotPagsanghan - 2016 Official Ballot
Pagsanghan - 2016 Official BallotCalbayog Journal
 
San jose de_buan - 2016 Official Ballot
San jose de_buan - 2016 Official BallotSan jose de_buan - 2016 Official Ballot
San jose de_buan - 2016 Official BallotCalbayog Journal
 
Jiabong - 2016 Official Ballot
Jiabong - 2016 Official BallotJiabong - 2016 Official Ballot
Jiabong - 2016 Official BallotCalbayog Journal
 
Hinabangan - 2016 Official Ballot
Hinabangan - 2016 Official BallotHinabangan - 2016 Official Ballot
Hinabangan - 2016 Official BallotCalbayog Journal
 
Zumarraga - 2016 Official Ballot
Zumarraga - 2016 Official BallotZumarraga - 2016 Official Ballot
Zumarraga - 2016 Official BallotCalbayog Journal
 
Santa margarita - 2016 Official Ballot
Santa margarita - 2016 Official BallotSanta margarita - 2016 Official Ballot
Santa margarita - 2016 Official BallotCalbayog Journal
 
Motiong - 2016 Official Ballot
Motiong - 2016 Official BallotMotiong - 2016 Official Ballot
Motiong - 2016 Official BallotCalbayog Journal
 
Villareal - 2016 Official Ballot
Villareal - 2016 Official BallotVillareal - 2016 Official Ballot
Villareal - 2016 Official BallotCalbayog Journal
 
Santa rita - 2016 Official Ballot
Santa rita - 2016 Official BallotSanta rita - 2016 Official Ballot
Santa rita - 2016 Official BallotCalbayog Journal
 

Viewers also liked (13)

Paranas (wright) - 2016 Official Ballot
Paranas (wright) - 2016 Official BallotParanas (wright) - 2016 Official Ballot
Paranas (wright) - 2016 Official Ballot
 
San sebastian - 2016 Official Ballot
San sebastian - 2016 Official BallotSan sebastian - 2016 Official Ballot
San sebastian - 2016 Official Ballot
 
Vacantes provinciale gu tras sentencia
Vacantes provinciale gu tras sentenciaVacantes provinciale gu tras sentencia
Vacantes provinciale gu tras sentencia
 
Pagsanghan - 2016 Official Ballot
Pagsanghan - 2016 Official BallotPagsanghan - 2016 Official Ballot
Pagsanghan - 2016 Official Ballot
 
San jose de_buan - 2016 Official Ballot
San jose de_buan - 2016 Official BallotSan jose de_buan - 2016 Official Ballot
San jose de_buan - 2016 Official Ballot
 
Jiabong - 2016 Official Ballot
Jiabong - 2016 Official BallotJiabong - 2016 Official Ballot
Jiabong - 2016 Official Ballot
 
Hinabangan - 2016 Official Ballot
Hinabangan - 2016 Official BallotHinabangan - 2016 Official Ballot
Hinabangan - 2016 Official Ballot
 
Zumarraga - 2016 Official Ballot
Zumarraga - 2016 Official BallotZumarraga - 2016 Official Ballot
Zumarraga - 2016 Official Ballot
 
Santa margarita - 2016 Official Ballot
Santa margarita - 2016 Official BallotSanta margarita - 2016 Official Ballot
Santa margarita - 2016 Official Ballot
 
Motiong - 2016 Official Ballot
Motiong - 2016 Official BallotMotiong - 2016 Official Ballot
Motiong - 2016 Official Ballot
 
Villareal - 2016 Official Ballot
Villareal - 2016 Official BallotVillareal - 2016 Official Ballot
Villareal - 2016 Official Ballot
 
Santa rita - 2016 Official Ballot
Santa rita - 2016 Official BallotSanta rita - 2016 Official Ballot
Santa rita - 2016 Official Ballot
 
Logo_upd
Logo_updLogo_upd
Logo_upd
 

Similar to Functions and pointers guide

Similar to Functions and pointers guide (20)

Functions
FunctionsFunctions
Functions
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Function
FunctionFunction
Function
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
C function
C functionC function
C function
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
3 Function & Storage Class.pptx
3 Function & Storage Class.pptx3 Function & Storage Class.pptx
3 Function & Storage Class.pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
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
 

Recently uploaded

Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Recently uploaded (20)

Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

Functions and pointers guide

  • 1. 2015-11-20 1 Unit 4 Functions • Set of instructions defined to perform specified task. • Reduces complexity. • Provide modularity. • Easy for error detection and debugging. • Divide complex tasks into manageable tasks. Types of Functions • Two types. • Pre defined functions – e.g. main(), strlen(), etc., • User defined functions – e.g. add() – a sub-function to perform addition. Need & Advantages of Functions • Program is too large and complex. • Debugging, testing becomes difficult. • Reduces length. • Easy to detect and debug errors. • Top-down approach. • Avoids coding of repeated execution. Three elements of Function • Function declaration • Function calling • Function definition • Declaration of a sub function in main function – Function Declaration • Calling of sub function in main function – Function Calling • Execution of main function – Function Definition • Return to the main function. • Start executing the next statement after the function calling statement. • Syntax of function Declaration section <<Return type>> funname(parameter list); • Syntax of function Definition section <<Return type>> funname(parameter list) { body of the function } • Syntax of function calling section Funname(parameters);
  • 2. 2015-11-20 2 Types of parameters • Actual parameters - used during a function call • Formal parameters - used in function definition and function declaration Example function to display a value #include<stdio.h> void main() { void fun(int); //declaration fun(10); //Call – Actual Parameters } void fun(int x) //definition – Formal Parameters { printf(“%d”,x); } Return statement • Used when sub function returns some integer value to the main function. • Syntax: •return(value); Or •return variable; • Example: •return(0); or return a; Function prototypes Depending upon arguments function is classified into •Function with no arguments and no return value. •Function with arguments and no return value •Function with arguments and with return value. •Function with no arguments and with return value. Function with no arguments and no return value #include<stdio.h> void main() { void fun(); //no arguments fun(); } void fun() { int x=5; printf(“%d”,x); //no return type } Function with arguments and no return value #include<stdio.h> void main() { void fun(int); //integer argument fun(10); } void fun(int x) { printf(“%d”,x); //no return value }
  • 3. 2015-11-20 3 Function with arguments and with return value #include<stdio.h> void main() { int a; int fun(int); //integer argument & integer return type a=fun(10); printf(“%d”,a); //a=11 } int fun(int x) { x++; return x; //return integer value } Function with no arguments and with return value #include<stdio.h> void main() { int a; int fun(); //no argument & integer return type a=fun(); printf(“%d”,a); // a= 5 } int fun() { int x=5; return x; //return value } Parameter Passing Methods Two ways of passing parameters to a function are • Call by value • Copies the values of actual parameters into formal parameters and actual parameters doesn’t change. • Call by reference • Copies the address of the actual parameters into the formal parameters. Call by Value • Calling a function with parameters passed as values int a=10; void fun(int a) fun(a); { defn; } Here fun(a) is a call by value. Any modification done with in the function is local to it and will not be effected outside the function Call by reference • Calling a function by passing pointers as parameters (address of variables is passed instead of variables) int a=1; void fun(int *x) fun(&a); { defn; } Any modification done to variable a will effect outside the function also Example program – Call by value #include<stdio.h> void main() { int a=10; printf(“%d”,a); a=10 fun(a); printf(“%d”,a); a=10 } void fun(int x) { printf(“%d”,x) x=10 x++; printf(“%d”,x); x=11 }
  • 4. 2015-11-20 4 Explanation – Call by value Example Program – Call by reference #include<stdio.h> void main() { int a=10; printf(“%d”,a); a=10 fun(&a); printf(“%d”,a); a=10 } void fun(int *x) { int t; t=*x; printf(“%d”,t) x=10 t++; printf(“%d”,t); x=11 } a and x are referring to same location. So value will be over written. Explanation – Call by reference Conclusion • Call by value => copying value of variable in another variable. So any change made in the copy will not affect the original location. • Call by reference => Creating link for the parameter to the original location. Since the address is same, changes to the parameter will refer to original location and the value will be over written. Recursion or Recursive Function Definition: A function is called recursion or recursive function if it calls by itself i.e. the statements inside the function has a call to the same function. e.g. void main( ) { void add( ); ….. add(); } add( ) { add( ); } Function declaration Function calling Function definition Advantages of Recursion • Useful for branching processes •Written with less number of statements. •Requires only few variables which requires program clean •Recursive functions are more effective in computing values Disadvantages of Recursion •Hard to think logic of the function. •Difficult to debug the code containing recursion.
  • 5. 2015-11-20 5 Applications of Recursive function •Factorial of a number •Finding length of the string •Fibonacci series generation •Towers of hanoi program Factorial - Recursion #include<stdio.h> #include<conio.h> void main( ) { int n,b; int fact(int); clrscr(); printf(“nEnter the value to compute:”); scanf(“%d”,&n); b=fact(n); printf(“nFactorial of %d is %d”,n,b); getch(); } int fact(int n) { if(n==1) { return 1; } else { return n*fact(n-1); } Fibonacci - Recursion #include<stdio.h> #include<conio.h> void main( ) { int i,n; int fibo(int); clrscr( ); printf(“nEnter no. of elements in series:”); scanf(“%d”,&n); for(i=0;i<n;i++) { printf(“%d”,fibo(i)); } getch( ); } void fibo(int i) { if(i==0) { return 0; } if(i==1) { return 1; } return fibo(i-1)+fibo(n-2); } Towers of Hanoi - Recursion #include<stdio.h> #include<conio.h> #include<math.h> void main( ) { int disk, moves; void hanoi(int,char,char,char); clrscr( ); printf(“nEnter no. of disks:”); scanf(“%d”,&disk); moves=pow(2,disk)-1; printf(“nNo.of moved req:%d”,moves); hanoi(disk, ‘A’, ‘C’, ‘B’); getch( ); } void hanoi(int x,char from, char to, char aux) { if(x==1) printf(“nMove disk from %c to %c”,from,to); else { hanoi(x-1,from,aux,to); printf(“nMove disk from %c to %c”,from,to); hanoi(x-1,aux,to,from); } } • Pointer • Definition • Initialization • Pointers arithmetic • Pointers and arrays • Eg programs Pointers Definition: Pointer is a variable that holds the address of variable or function. It is declared same as other variable but the variable preceded by *(asterisk) (i.e) indirection operator Syntax: datatype *variable_name; eg: int *a,*c; char *b; Here a stores address of integer variable and b stores address of character variable. It is a variable whose value is the address of another variable(same data type). It is also called address variable and it is a derived data type Pointers can be used to access and manipulate data stored in the memory
  • 6. 2015-11-20 6 Features of Pointers • More effective and useful in handling arrays. • Used to return multiple values from a function. • Supports dynamic memory management. • Reduces complexity and length of a program. • Reduces execution time of a program. Limitations of Pointers • Should have prefix “*” • Combination of data types not allowed Accessing the address of a variable: Address for the variable is given by machine The symbol &(address of operator) is used to get the address of the variable. Syntax: &variable_name;#include<stdio.h> #include<conio.h> void main() { int *a , b=10; a=&b; clrscr(); printf("n Address of variable b is %u",a); //printf("n Address of variable b is %u",&b); printf("n value of b is %d ",*a); getch(); } 10 b 655 24 655 24 655 24 avaria ble 655 22 655 22 value addr ess &b=65524 &a= 65522 Value of a= 65524 Value of b=10 a=&b;  a=addr of b(65524) *a  value of a (65524)  10 Initialization of variable: • The process of assigning the address of a variable to a pointer variable is known as initialization. • Eg: p=&n;  p contains address of variable n, the data item of n can be accessed by *p Initialize pointer variable while declaring it: int n=15; int *p=&n; initialization while declaring #include<stdio.h> #include<conio.h> void main() { int n=15; int *p; p=&n; clrscr(); printf("%d",*p); getch(); } 15 #include<stdio.h> #include<conio.h> void main() { int n=15; int *p=&n; clrscr(); printf("%d",*p); getch(); } 15 #include<stdio.h> #include<conio.h> void main() { int a,b; int *c,*d; clrscr(); printf("nEnter the values of a and b:"); scanf("%d%d",&a,&b); c=&a; d=&b; printf("nValue of a is:%d",a); printf("nValue of a is:%d",*c); printf("nAddress of a is:%u",&a); printf("nAddress of a is:%u",c); printf("nValue of b is:%d",b); printf("nValue of b is:%d",*d); printf("nAddress of b is:%u",&b); printf("nAddress of b is:%u",d); printf("nAddress of pointer variable c is:%u",&c); printf("nAddress of pointer variable d is:%u",&d); getch(); } Double pointer Definition: is a variable used to store address of another pointer variable. Syntax: data_type **var_name; e.g. void main() { int c; int *b,**a; b=&c; //b displays address of c and *b displays values of c a=&b; //a->double pointer variable and displays address of b and *a displays address of c } Double pointer #include<stdio.h> #include<conio.h> void main() { int n=15; int *p=&n; int **a=&p; clrscr() ; printf("n *p is %u",*p); printf("n **a ia %u",**a); getch(); }