SlideShare a Scribd company logo
1 of 30
Lecture by: Nauman Ur Rehman
Organized by: Shahid Jameel
 A function is a type of procedure or routine
which performs some operation.
 Note: A function can have multiple input
(arguments) but only one output (return value)
Output = f(a,b)
 A function is a group of statements that
together perform a task.
 It is the part of a code that is called with
some input (arguments), on which it
performs a task, and then returns some
value.
Arguments
Body
(Statements)
Return
 It makes program more manageable by
dividing it into small parts.
 We can avoid the repetition of the program
using functions.
 It also makes a program easy to debug (find
errors, if any)
 There are many built-in functions included in
the pre-defined libraries; e.g. getline, read,
cin.
Prototype or Function Declaration
Function Call
Function Definition or Function Body
 <return type> <function_name> (parameters);
 1. int search (int value, int size);
 2. int search (int , int);
 Both 1 and 2 are the “prototypes” of the same
function.
 Never forget the semicolon at last.
 A function parameter is a variable declared in
the prototype or declaration of a function.
 To call a function, we simply write the name
of the function and provide it arguments:
1. y =search (7, x);
//x=5 & int search (int value, int size);
2. cout<< search (7,x);
//displays the returned value
3. search (7, x);
// void search (int, int );
 An argument is the value that is passed to
the function in place of a parameter.
 <return type> <function name> (parameters)
{ //curly braces
statement 1;
statement 2;
.
return <int_value>;
}
//no semicolon before or after curly braces
 int search (int value, int size)
{
size = value * 2;
return size;
}
 #include<iostream>
 using namespace std;
 int func (int, int);
 int main()
 {
 int x=0, y=1;
 cout<<x<<endl<<y<<endl;
 x=func(x,y);
 cout<<x<<endl;
 cout<<func(x,y)<<endl;
 func(x,y);
 return 0;
 }
 int func (int x, int y)
 {
 return x+y;
 }
Declaration or prototype
Function Call 2
Definition or Body
Function Call 1
Function Call 3
1. Functions with no input and no return:
void search (void);
void main (void)
{
search ();
}
void search (void)
{
int x;
x = 4;
cout<<x<<endl; //displays 4 but no return
}
2. Functions with input but no return:
void search (int);
void main ()
{
search (4);
}
void search (int size)
{
size++;
cout<<size<<endl; //displays 5 but
} // no return
3. Functions with no input but return:
int function (void);
int main ()
{
cout<<search ()<<endl;
return 0;
}
int search (void)
{
int x = 4;
return x; //return 4
cout<<x<<endl; //do not execute
} // as return is already encountered
4. Functions with input and return:
int function (int);
void main ()
{
cout<<search (4)<<endl; //displays 5
}
int search (int size)
{
size++;
return size; //return 5
}
 Pointer is a variable that is used to store address
of a variable.
 Declaration:
<data type> * <pointer_name>;
int * a;
 Initialization:
a = &b ; //address of b will be stored in a
// & sign is used to denote address
 Dereferencing:
*a; //denote the value stored in variable
//where a is pointing
// ‘ * ‘ is called the dereference operator.
 int var = 50; //address of var = 1001
 int * ptr; // ptr declared (address=2047)
 ptr = & var; //ptr initialized
 cout<<var<<endl;
 cout<<&var<<endl;
 cout<<ptr<<endl;
 cout<<*ptr<<endl;
 cout<<&ptr<<endl;
 * ptr = 5;
 cout<< var<<endl;
 cout<<* ptr<<endl;
 cout<<*& ptr<<endl //*& cancel each other
Note:
address of var = 0012FF60
address of ptr = 0012FF54
 int i=50; //address of i = 2000
int * ptr2 = &i; //direct initialization
int * * ptr1 = &ptr2; //address of ptr1 = 3000
cout<< i<<endl;
cout<<ptr2<<endl;
cout<<*ptr2<<endl;
cout<<ptr1<<endl;
cout<<*ptr1<<endl;
cout<<* * ptr1<<endl;
cout<<& ptr1<<endl;
 int i=50; //address of i = 2000
int * ptr2 = &i; //direct initialization
int * * ptr1 = &ptr2; //address of ptr1 = 3000
cout<< i<<endl; // 50
cout<<ptr2<<endl; // 2000
cout<<*ptr2<<endl; // 50
cout<<ptr1<<endl; //3000
cout<<*ptr1<<endl; //2000
cout<<* * ptr1<<endl; //50
cout<<& ptr1<<endl; //4000
1. Pointer pointing to variable:
int * ptr;
e.g.
int * ptr = &a;
a++;
ptr = &b;
2. Pointer pointing to constant variable:
const int * ptr;
e.g.
const int * ptr = &a;
a++; // error
ptr = &b;
3. Constant pointer pointing to variable:
int * const ptr;
e.g.
int * const ptr = &a;
a++;
ptr = &b; // error
4. Constant pointer pointing to constant variable:
const int * const ptr;
e.g
const int * const ptr = &a;
a++; // error
ptr = &b; // error
 Array is itself, in a way, a pointer.
 Array name is actually the address of the very
first element of the array.
 Array uses the subscripts to access different
elements e.g. Arr[5]: gives access to 5th
element of an array named “Arr”.
 A pointer to an array is initialized using array
name to get address of the array’s 1st element.
 #include <iostream>
 using namespace std;
 int main ()
 { int arr[5]; //declared array named “arr” with 5 as a size
 int * p; //declared a pointer p
 p = arr; //address of 1st element of array stored in p
 *p = 10; //puts arr[0] = 10
 p++; //address of 2nd element of arry stored in p
 *p = 20; //puts arr[1] = 20
 p = &arr[2]; // address of 3rd element of array stored in p
 *p = 30; //puts arr[2] = 30
 p = arr + 3; // address of 4th element of array stored in p
 *p = 40; // puts arr[3] = 40
 p = arr; // address of 5th element of array stored in p
 *(p+4) = 50; // puts arr[4] =50
 for (int n=0; n<5; n++)
 cout << arr[n] << ", ";
 return 0; }
p
1. Pass-by-value
2. Pass-by-reference with reference
arguments
3. Pass-by-reference with pointer
arguments
 void multiply (int x);
 void main()
{
int x=3;
multiply (x);
cout<<x; // displays 3
}
 void multiply (int x)
{
x*=4; // x = x * 4 = 12
cout<<x<<endl; // displays 12
}
 void multiply (int &a);
 void main()
{
int x=3;
multiply (x);
cout<<x; //displays 12
}
 void multiply (int &a)
{
a*=4;
cout<<a<<endl; //displays 12
}
Note: a is put equivalent to x.
address of a = address of x
Pass-by-reference with pointer arguments
 void multiply (int *a);
 void main()
{
int x=3;
multiply (&x);
cout<<x; //displays 12
}
 void multiply (int *a)
{
*a*=4; //*a = *a * 4
cout<<*a<<endl; //displays 12
}
 void multiply (int [], int );
 void main()
{
int x[3]={1,2,3};
multiply (x, 3);
cout<<x[1]<<endl; //displays 8
}
 void multiply (int a[], int size)
{
for (int y=0; y<size; y++)
a[y]*=4; //a[y] = a[y] * 4
cout<<a[1]<<endl; //displays 8
}
 Note: Arrays are passed by refrence.
Learn  c++ (functions) with nauman ur rehman

More Related Content

What's hot

Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)Ameer Hamxa
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Dharma Kshetri
 
Functions in python
Functions in pythonFunctions in python
Functions in pythonIlian Iliev
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Rick Copeland
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 

What's hot (20)

c programming
c programmingc programming
c programming
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
c programming
c programmingc programming
c programming
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
Pointers
PointersPointers
Pointers
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 

Similar to Learn c++ (functions) with nauman ur rehman

how to reuse code
how to reuse codehow to reuse code
how to reuse codejleed1
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointersPrincess Sam
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Abdul Samee
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Tomohiro Kumagai
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 

Similar to Learn c++ (functions) with nauman ur rehman (20)

how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
functions of C++
functions of C++functions of C++
functions of C++
 
Pointer
PointerPointer
Pointer
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 

Recently uploaded

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 

Recently uploaded (20)

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 

Learn c++ (functions) with nauman ur rehman

  • 1. Lecture by: Nauman Ur Rehman Organized by: Shahid Jameel
  • 2.  A function is a type of procedure or routine which performs some operation.  Note: A function can have multiple input (arguments) but only one output (return value) Output = f(a,b)
  • 3.  A function is a group of statements that together perform a task.  It is the part of a code that is called with some input (arguments), on which it performs a task, and then returns some value. Arguments Body (Statements) Return
  • 4.  It makes program more manageable by dividing it into small parts.  We can avoid the repetition of the program using functions.  It also makes a program easy to debug (find errors, if any)  There are many built-in functions included in the pre-defined libraries; e.g. getline, read, cin.
  • 5. Prototype or Function Declaration Function Call Function Definition or Function Body
  • 6.  <return type> <function_name> (parameters);  1. int search (int value, int size);  2. int search (int , int);  Both 1 and 2 are the “prototypes” of the same function.  Never forget the semicolon at last.  A function parameter is a variable declared in the prototype or declaration of a function.
  • 7.  To call a function, we simply write the name of the function and provide it arguments: 1. y =search (7, x); //x=5 & int search (int value, int size); 2. cout<< search (7,x); //displays the returned value 3. search (7, x); // void search (int, int );  An argument is the value that is passed to the function in place of a parameter.
  • 8.  <return type> <function name> (parameters) { //curly braces statement 1; statement 2; . return <int_value>; } //no semicolon before or after curly braces  int search (int value, int size) { size = value * 2; return size; }
  • 9.  #include<iostream>  using namespace std;  int func (int, int);  int main()  {  int x=0, y=1;  cout<<x<<endl<<y<<endl;  x=func(x,y);  cout<<x<<endl;  cout<<func(x,y)<<endl;  func(x,y);  return 0;  }  int func (int x, int y)  {  return x+y;  } Declaration or prototype Function Call 2 Definition or Body Function Call 1 Function Call 3
  • 10.
  • 11. 1. Functions with no input and no return: void search (void); void main (void) { search (); } void search (void) { int x; x = 4; cout<<x<<endl; //displays 4 but no return }
  • 12. 2. Functions with input but no return: void search (int); void main () { search (4); } void search (int size) { size++; cout<<size<<endl; //displays 5 but } // no return
  • 13. 3. Functions with no input but return: int function (void); int main () { cout<<search ()<<endl; return 0; } int search (void) { int x = 4; return x; //return 4 cout<<x<<endl; //do not execute } // as return is already encountered
  • 14. 4. Functions with input and return: int function (int); void main () { cout<<search (4)<<endl; //displays 5 } int search (int size) { size++; return size; //return 5 }
  • 15.  Pointer is a variable that is used to store address of a variable.  Declaration: <data type> * <pointer_name>; int * a;  Initialization: a = &b ; //address of b will be stored in a // & sign is used to denote address  Dereferencing: *a; //denote the value stored in variable //where a is pointing // ‘ * ‘ is called the dereference operator.
  • 16.  int var = 50; //address of var = 1001  int * ptr; // ptr declared (address=2047)  ptr = & var; //ptr initialized  cout<<var<<endl;  cout<<&var<<endl;  cout<<ptr<<endl;  cout<<*ptr<<endl;  cout<<&ptr<<endl;  * ptr = 5;  cout<< var<<endl;  cout<<* ptr<<endl;  cout<<*& ptr<<endl //*& cancel each other
  • 17. Note: address of var = 0012FF60 address of ptr = 0012FF54
  • 18.  int i=50; //address of i = 2000 int * ptr2 = &i; //direct initialization int * * ptr1 = &ptr2; //address of ptr1 = 3000 cout<< i<<endl; cout<<ptr2<<endl; cout<<*ptr2<<endl; cout<<ptr1<<endl; cout<<*ptr1<<endl; cout<<* * ptr1<<endl; cout<<& ptr1<<endl;
  • 19.  int i=50; //address of i = 2000 int * ptr2 = &i; //direct initialization int * * ptr1 = &ptr2; //address of ptr1 = 3000 cout<< i<<endl; // 50 cout<<ptr2<<endl; // 2000 cout<<*ptr2<<endl; // 50 cout<<ptr1<<endl; //3000 cout<<*ptr1<<endl; //2000 cout<<* * ptr1<<endl; //50 cout<<& ptr1<<endl; //4000
  • 20. 1. Pointer pointing to variable: int * ptr; e.g. int * ptr = &a; a++; ptr = &b; 2. Pointer pointing to constant variable: const int * ptr; e.g. const int * ptr = &a; a++; // error ptr = &b;
  • 21. 3. Constant pointer pointing to variable: int * const ptr; e.g. int * const ptr = &a; a++; ptr = &b; // error 4. Constant pointer pointing to constant variable: const int * const ptr; e.g const int * const ptr = &a; a++; // error ptr = &b; // error
  • 22.  Array is itself, in a way, a pointer.  Array name is actually the address of the very first element of the array.  Array uses the subscripts to access different elements e.g. Arr[5]: gives access to 5th element of an array named “Arr”.  A pointer to an array is initialized using array name to get address of the array’s 1st element.
  • 23.  #include <iostream>  using namespace std;  int main ()  { int arr[5]; //declared array named “arr” with 5 as a size  int * p; //declared a pointer p  p = arr; //address of 1st element of array stored in p  *p = 10; //puts arr[0] = 10  p++; //address of 2nd element of arry stored in p  *p = 20; //puts arr[1] = 20  p = &arr[2]; // address of 3rd element of array stored in p  *p = 30; //puts arr[2] = 30  p = arr + 3; // address of 4th element of array stored in p  *p = 40; // puts arr[3] = 40  p = arr; // address of 5th element of array stored in p  *(p+4) = 50; // puts arr[4] =50  for (int n=0; n<5; n++)  cout << arr[n] << ", ";  return 0; } p
  • 24. 1. Pass-by-value 2. Pass-by-reference with reference arguments 3. Pass-by-reference with pointer arguments
  • 25.  void multiply (int x);  void main() { int x=3; multiply (x); cout<<x; // displays 3 }  void multiply (int x) { x*=4; // x = x * 4 = 12 cout<<x<<endl; // displays 12 }
  • 26.  void multiply (int &a);  void main() { int x=3; multiply (x); cout<<x; //displays 12 }  void multiply (int &a) { a*=4; cout<<a<<endl; //displays 12 } Note: a is put equivalent to x. address of a = address of x
  • 27.
  • 28. Pass-by-reference with pointer arguments  void multiply (int *a);  void main() { int x=3; multiply (&x); cout<<x; //displays 12 }  void multiply (int *a) { *a*=4; //*a = *a * 4 cout<<*a<<endl; //displays 12 }
  • 29.  void multiply (int [], int );  void main() { int x[3]={1,2,3}; multiply (x, 3); cout<<x[1]<<endl; //displays 8 }  void multiply (int a[], int size) { for (int y=0; y<size; y++) a[y]*=4; //a[y] = a[y] * 4 cout<<a[1]<<endl; //displays 8 }  Note: Arrays are passed by refrence.