SlideShare a Scribd company logo
1 of 23
Functions
Advantages 
• Modularizes the Code -> Code is more 
readable 
• Change in Internal Implementation of 
Functions does not propagate further changes 
in code 
• Increases re-usability of code.
Writing Functions 
• Return_Type Function_Name ( type_1 
variable_1 , type_2 variable_2 , .. ) 
• In Functions, you can either pass parameters 
by value or by reference 
• Also, you have to declare a function prototype 
if you are about to make a call to a function 
which is written below the caller function.
Declaring Functions
Demo #1 
• Write a function which takes two integers as 
arguments and returns the value of the 
maximum.
Demo #2 
• Write a function which squares an integer.
Approach 
• Break the problem into Arguments of Function 
and the Return value of the function. 
Here the Argument to the function is a single 
Integer and the Return value is an Integer as 
well.
Call By Value 
• You CALL your friend to get your mobile number recharged. 
You give him a slip which has your number written over it. He 
TAKES ANOTHER (FRESH) SLIP AND COPIES THE NUMBER ON 
THIS (FRESH) SLIP, goes to the shopkeeper and gets the work 
done. But, while returning, he met another friend who also 
asked him for mobile recharge, so he COPIES THE NEW 
NUMBER ON THE (his own) SLIP NOW. Whose number is 
written on your slip now ? 
• Same is the case in call by value where if you call a function, 
passing a particular argument by value, then the function 
TAKES ANOTHER (identical) VARIABLE AND COPIES THE VALUE 
OF THE ARGUMENT ON THIS VARIABLE.
Any changes made to argument1/2 will not change value1/2 
Value1 Value2 Argument1 Argument2 
In Memory
So, Even if you do: 
This will change Memory contents for “val” not variable
Call By Reference 
• You CALL your friend to get your mobile number 
recharged. You give him a slip which has your number 
written over it. He TAKES THIS SLIP and goes to the 
shopkeeper and gets the work done. But, while 
returning, he met another friend who also asked him 
for mobile recharge, so he COPIES THE NEW NUMBER 
ON THE (Your) SLIP NOW. Whose number is written on 
your slip now ? 
• Same is the case with functions. If you pass an 
argument by Reference then any changes (New 
Number copied) made to the argument will be 
reflected.
Call By Reference 
• If you want the function to manipulate the 
values of the arguments. 
• For ex: Sorting an array , Reversing a string etc. 
• Just use the Reference “&” operator.
So, If you do:
Demo #3 
• Write a Function which takes two characters 
and prints them in Upper Case but the first 
character after the function call must be in 
Upper Case while the second character must 
not be changed.
Approach 
• Break the Problem into Arguments of the 
function and the return value of the function 
• Think about the “call type” for each argument as 
in Call by Value or Call by Reference. 
Here the Function should have two arguments both 
of type “char”. Pass the first argument as reference 
(i.e. using the & operator) and the second one by 
value/reference. What about return type ? “void”. 
Use “return;” in such cases if you want to force the 
program to return immediately.
Call By Reference 
• You can also pass a pointer to the argument if 
you want the change to be reflected. 
Note: What about call type of pvalue here
Call by Value 
Arrays are always passed as Reference. ? 
• General: 
• If function a is calling function b in its’ body then 
function b should be written above function a. 
What if both functions are calling each other. 
Define Function Prototypes at the very beginning 
of the File. What is a Function Prototype ? 
• Return_type function_name (type_1 arg1 , .. ); 
• Note the semicolon at the end.
Lets’ Tackle Some Real Problem 
• Given a string S check if any rotation of S is 
palindrome or not. 
• Rotation of S is defined as follows: 
IF S = c1 c2 c3 c4…. ck ck+1 ck+2 …. cn 
then ck+1 ck+2 … cn c1 c2 c3 .. ck 
is a rotation of S for all 1<=k<n
Approach 
• First break the problem into sub-problems: 
• What all independent sub-problems need to 
be solved? 
->Palindrome Checking 
-> String Reversal 
->Substring Finding 
What about Parameters and Return Types and 
“call by” of the arguments. 
Lets’ see a naïve algorithm for the problem.
How to pass struct objects to functions 
• Pass Arguments just like any other data type.
Demo #4 
• Write getter and a setter functions for all the 
members of a struct. Assume the struct has 
only two attributes string (say name of 
student), int (student id)
Some Thoughts 
• What if we want to return more than one (or 
more) values (say minimum and maximum both 
simultaneously in an array)? 
– Hint: Can we use pointers/array ? 
• What if the data types of the values to be 
returned are different ? 
– Hint: Can we use structs? 
• What if our function has no arguments ? 
– Hint: Main itself is a function. 
– Have you seen it taking arguments ?

More Related Content

What's hot (20)

Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
CPP07 - Scope
CPP07 - ScopeCPP07 - Scope
CPP07 - Scope
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Function
FunctionFunction
Function
 
Class7
Class7Class7
Class7
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java script function
Java script functionJava script function
Java script function
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)
 
Functions
FunctionsFunctions
Functions
 
Functions in python
Functions in python Functions in python
Functions in python
 
Type conversions
Type conversionsType conversions
Type conversions
 
Compiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsCompiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statements
 
Operators in java By cheena
Operators in java By cheenaOperators in java By cheena
Operators in java By cheena
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
ASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS ANDASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS AND
 
Beyond Mere Actors
Beyond Mere ActorsBeyond Mere Actors
Beyond Mere Actors
 

Viewers also liked

Ley de Tramitación Digital y Clave única
Ley de Tramitación Digital y Clave únicaLey de Tramitación Digital y Clave única
Ley de Tramitación Digital y Clave únicaClaudio Igor Ortiz
 
Lecture 10 : trees - 2
Lecture 10 : trees - 2Lecture 10 : trees - 2
Lecture 10 : trees - 2Vivek Bhargav
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heapsVivek Bhargav
 
Lecture 7 & 8: Stack & queue
Lecture 7 & 8: Stack  & queueLecture 7 & 8: Stack  & queue
Lecture 7 & 8: Stack & queueVivek Bhargav
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationVivek Bhargav
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointersVivek Bhargav
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sortingVivek Bhargav
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntaxVivek Bhargav
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked listVivek Bhargav
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basicsVivek Bhargav
 
pengertian,fungsi,perkembangan dan peranan PERS
pengertian,fungsi,perkembangan dan peranan PERSpengertian,fungsi,perkembangan dan peranan PERS
pengertian,fungsi,perkembangan dan peranan PERSAhya Amstrong
 
Sistem pemerintahan di berbagai negara
Sistem pemerintahan di berbagai negaraSistem pemerintahan di berbagai negara
Sistem pemerintahan di berbagai negaraAhya Amstrong
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsVivek Bhargav
 

Viewers also liked (14)

Ley de Tramitación Digital y Clave única
Ley de Tramitación Digital y Clave únicaLey de Tramitación Digital y Clave única
Ley de Tramitación Digital y Clave única
 
Lecture 10 : trees - 2
Lecture 10 : trees - 2Lecture 10 : trees - 2
Lecture 10 : trees - 2
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heaps
 
Lecture 7 & 8: Stack & queue
Lecture 7 & 8: Stack  & queueLecture 7 & 8: Stack  & queue
Lecture 7 & 8: Stack & queue
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory Allocation
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntax
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
 
pengertian,fungsi,perkembangan dan peranan PERS
pengertian,fungsi,perkembangan dan peranan PERSpengertian,fungsi,perkembangan dan peranan PERS
pengertian,fungsi,perkembangan dan peranan PERS
 
Sistem pemerintahan di berbagai negara
Sistem pemerintahan di berbagai negaraSistem pemerintahan di berbagai negara
Sistem pemerintahan di berbagai negara
 
Breast Carcinoma
Breast CarcinomaBreast Carcinoma
Breast Carcinoma
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithms
 

Similar to Lecture 4: Functions

Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptxzueZ3
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
Intro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionIntro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionBlue Elephant Consulting
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptRajasekhar364622
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and commentMalligaarjunanN
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in CRAJ KUMAR
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptxYagna15
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuplesMalligaarjunanN
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
 

Similar to Lecture 4: Functions (20)

Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
 
Intro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionIntro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, Recursion
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Functions
Functions Functions
Functions
 
Functions
FunctionsFunctions
Functions
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
C language presentation
C language presentationC language presentation
C language presentation
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 

Recently uploaded

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 

Recently uploaded (20)

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 

Lecture 4: Functions

  • 2. Advantages • Modularizes the Code -> Code is more readable • Change in Internal Implementation of Functions does not propagate further changes in code • Increases re-usability of code.
  • 3. Writing Functions • Return_Type Function_Name ( type_1 variable_1 , type_2 variable_2 , .. ) • In Functions, you can either pass parameters by value or by reference • Also, you have to declare a function prototype if you are about to make a call to a function which is written below the caller function.
  • 4.
  • 6. Demo #1 • Write a function which takes two integers as arguments and returns the value of the maximum.
  • 7. Demo #2 • Write a function which squares an integer.
  • 8. Approach • Break the problem into Arguments of Function and the Return value of the function. Here the Argument to the function is a single Integer and the Return value is an Integer as well.
  • 9. Call By Value • You CALL your friend to get your mobile number recharged. You give him a slip which has your number written over it. He TAKES ANOTHER (FRESH) SLIP AND COPIES THE NUMBER ON THIS (FRESH) SLIP, goes to the shopkeeper and gets the work done. But, while returning, he met another friend who also asked him for mobile recharge, so he COPIES THE NEW NUMBER ON THE (his own) SLIP NOW. Whose number is written on your slip now ? • Same is the case in call by value where if you call a function, passing a particular argument by value, then the function TAKES ANOTHER (identical) VARIABLE AND COPIES THE VALUE OF THE ARGUMENT ON THIS VARIABLE.
  • 10. Any changes made to argument1/2 will not change value1/2 Value1 Value2 Argument1 Argument2 In Memory
  • 11. So, Even if you do: This will change Memory contents for “val” not variable
  • 12. Call By Reference • You CALL your friend to get your mobile number recharged. You give him a slip which has your number written over it. He TAKES THIS SLIP and goes to the shopkeeper and gets the work done. But, while returning, he met another friend who also asked him for mobile recharge, so he COPIES THE NEW NUMBER ON THE (Your) SLIP NOW. Whose number is written on your slip now ? • Same is the case with functions. If you pass an argument by Reference then any changes (New Number copied) made to the argument will be reflected.
  • 13. Call By Reference • If you want the function to manipulate the values of the arguments. • For ex: Sorting an array , Reversing a string etc. • Just use the Reference “&” operator.
  • 14. So, If you do:
  • 15. Demo #3 • Write a Function which takes two characters and prints them in Upper Case but the first character after the function call must be in Upper Case while the second character must not be changed.
  • 16. Approach • Break the Problem into Arguments of the function and the return value of the function • Think about the “call type” for each argument as in Call by Value or Call by Reference. Here the Function should have two arguments both of type “char”. Pass the first argument as reference (i.e. using the & operator) and the second one by value/reference. What about return type ? “void”. Use “return;” in such cases if you want to force the program to return immediately.
  • 17. Call By Reference • You can also pass a pointer to the argument if you want the change to be reflected. Note: What about call type of pvalue here
  • 18. Call by Value Arrays are always passed as Reference. ? • General: • If function a is calling function b in its’ body then function b should be written above function a. What if both functions are calling each other. Define Function Prototypes at the very beginning of the File. What is a Function Prototype ? • Return_type function_name (type_1 arg1 , .. ); • Note the semicolon at the end.
  • 19. Lets’ Tackle Some Real Problem • Given a string S check if any rotation of S is palindrome or not. • Rotation of S is defined as follows: IF S = c1 c2 c3 c4…. ck ck+1 ck+2 …. cn then ck+1 ck+2 … cn c1 c2 c3 .. ck is a rotation of S for all 1<=k<n
  • 20. Approach • First break the problem into sub-problems: • What all independent sub-problems need to be solved? ->Palindrome Checking -> String Reversal ->Substring Finding What about Parameters and Return Types and “call by” of the arguments. Lets’ see a naïve algorithm for the problem.
  • 21. How to pass struct objects to functions • Pass Arguments just like any other data type.
  • 22. Demo #4 • Write getter and a setter functions for all the members of a struct. Assume the struct has only two attributes string (say name of student), int (student id)
  • 23. Some Thoughts • What if we want to return more than one (or more) values (say minimum and maximum both simultaneously in an array)? – Hint: Can we use pointers/array ? • What if the data types of the values to be returned are different ? – Hint: Can we use structs? • What if our function has no arguments ? – Hint: Main itself is a function. – Have you seen it taking arguments ?