SlideShare une entreprise Scribd logo
1  sur  10
Télécharger pour lire hors ligne
CS110
Computational Engineering
Function Calls
Feb, 19 - 2008
V. Kamakoti

Course Material – P.Sreenivasa Kumar, N.S.Narayanaswamy, Deepak Khemani, V. Kamakoti– CS&E, IIT M
1
Running with less memory
• Functions
• Provided to break up our problem into more basic units
• Control flow – flows from function to function, saving the
current context, changing contexts, then returning…..
• Helps the program to run with lesser memory, but slower
than in the case of a long big program

• The issue now with data associated with other
functions.
• Typically functions communicate using the
arguments and return values
PSK, NSN, DK, VK – CS&E, IIT M

2
Call by Value
In C, function arguments are passed “by value”
– values of the arguments given to the called function
in temporary variables rather than the originals
– the modifications to the parameter variables do not
affect the variables in the calling function

“Call by reference”
– variables are passed by reference
• subject to modification by the function

– achieved by passing the “address of” variables

PSK, NSN, DK, VK – CS&E, IIT M

3
Call by Value - an example
main( ) {
Function prototype
int p = 1, q = 2, r = 3, s;
int test(int, int, int);
Function call
…;
s = test (p, q, r); … /* s is assigned 9 */
}
/* p,q,r don’t change, only their copies do */
int test( int a, int b, int c){
a ++; b ++; c ++;
return (a + b + c);
}
PSK, NSN, DK, VK – CS&E, IIT M

Function definition

4
Call by Reference
#include <stdio.h>
void quoRem(int, int, int*, int*);
main(){
int x, y, quo, rem;
scanf(“%d%d”, &x, &y);
quoRem(x, y, &quo, &rem);
printf(“%d %d”, quo , rem);
}

/*pointers*/
Passing
addresses
Does not return
anything

void quoRem(int num, int den, int* quoAdr, int* remAdr){
*quoAdr = num / den; *remAdr = num % den;
}
PSK, NSN, DK, VK – CS&E, IIT M

5
Recursive Function Example
int power (int num, int exp) {
int p;
if (exp = = 1) return num;
p = power(num, exp/2);
if (exp % 2 = = 0) return p*p;
else return p*p*num;
}

The base case exp = 1
Guarantees termination

power (3, 13)
36*36*3 = 729*729*3 = 1594323
power (3, 6)
33*33 = 27*27 = 729
power(3,3)
31*31*3 = 27
power(3,1)
= 3
PSK, NSN, DK, VK – CS&E, IIT M

6
Factorial (n)
• n! = 1 * 2 * 3 * .... * (n-2) * (n-1) * n
Iterative version
int fact(int n)
{
int i; int result;
result = 1;
for (i = 1; i <= n; i++)
result = result * i;
return result;
}
PSK, NSN, DK, VK – CS&E, IIT M

In practice int may
not be enough!

7
Factorial (n) – recursive program

fact(n) = n*fact(n-1)
int fact(int n)
{
if (n == 1) return 1;
return n * fact(n - 1);
}
• Shorter, simpler to understand
• Uses fewer variables
• Machine has to do more work running this one!
PSK, NSN, DK, VK – CS&E, IIT M

8
Pending computations
• In this recursive version the calling
version still has pending work after
it gets the return value.

int fact(int n)
{
if (n == 1) return 1;
return n * fact(n - 1);
}

(fact 4)
4 * (fact 3)
It needs to save
some values for
future use

3 * (fact 2)
2 * (fact 1)
1

2*1 =2
3*2 = 6
4*6 = 24
PSK, NSN, DK, VK – CS&E, IIT M

9
Tail recursion
int fact(n)
{ return fact_aux(n, 1); }
Auxiliary variable

int fact_aux(int n, int result)
{
if (n == 1) return result;
return fact_aux(n - 1, n * result)
}
PSK, NSN, DK, VK – CS&E, IIT M

The recursive call
is in the return
statement. The
function simply
returns what it
gets from the call
it makes. The
calling version
does not have
save any values!
10

Contenu connexe

Tendances

Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
A calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesA calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesPolytechnique Montréal
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructureKumar
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and designMegha V
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysisjayavignesh86
 

Tendances (20)

6-Python-Recursion.pdf
6-Python-Recursion.pdf6-Python-Recursion.pdf
6-Python-Recursion.pdf
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
[ppt]
[ppt][ppt]
[ppt]
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
A calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesA calculus of mobile Real-Time processes
A calculus of mobile Real-Time processes
 
Sortingnetworks
SortingnetworksSortingnetworks
Sortingnetworks
 
Sorting network
Sorting networkSorting network
Sorting network
 
Recursion
RecursionRecursion
Recursion
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructure
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Ch10 Recursion
Ch10 RecursionCh10 Recursion
Ch10 Recursion
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 

En vedette

Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringSri Harsha Pamu
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringSri Harsha Pamu
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringSri Harsha Pamu
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringSri Harsha Pamu
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringSri Harsha Pamu
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringSri Harsha Pamu
 

En vedette (7)

Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational Engineering
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational Engineering
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
 

Similaire à Lec19-CS110 Computational Engineering

C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementSreedhar Chowdam
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
ML with python.pdf
ML with python.pdfML with python.pdf
ML with python.pdfn58648017
 
OmpSs – improving the scalability of OpenMP
OmpSs – improving the scalability of OpenMPOmpSs – improving the scalability of OpenMP
OmpSs – improving the scalability of OpenMPIntel IT Center
 
C++ and Data Structure.ppt
C++ and Data Structure.pptC++ and Data Structure.ppt
C++ and Data Structure.pptRich Alex
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Codemotion
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxDeepasCSE
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringSri Harsha Pamu
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Brendan Eich
 
how to reuse code
how to reuse codehow to reuse code
how to reuse codejleed1
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonAlex Payne
 

Similaire à Lec19-CS110 Computational Engineering (20)

C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
ML with python.pdf
ML with python.pdfML with python.pdf
ML with python.pdf
 
OmpSs – improving the scalability of OpenMP
OmpSs – improving the scalability of OpenMPOmpSs – improving the scalability of OpenMP
OmpSs – improving the scalability of OpenMP
 
Ns2 by khan
Ns2 by khan Ns2 by khan
Ns2 by khan
 
L4 functions
L4 functionsL4 functions
L4 functions
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
C++ and Data Structure.ppt
C++ and Data Structure.pptC++ and Data Structure.ppt
C++ and Data Structure.ppt
 
Recursion
RecursionRecursion
Recursion
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
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
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational Engineering
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the Horizon
 

Plus de Sri Harsha Pamu

Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringSri Harsha Pamu
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringSri Harsha Pamu
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringSri Harsha Pamu
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringSri Harsha Pamu
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringSri Harsha Pamu
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringSri Harsha Pamu
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringSri Harsha Pamu
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringSri Harsha Pamu
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringSri Harsha Pamu
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability studySri Harsha Pamu
 

Plus de Sri Harsha Pamu (15)

Lec13
Lec13Lec13
Lec13
 
Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational Engineering
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational Engineering
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational Engineering
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational Engineering
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational Engineering
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational Engineering
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational Engineering
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
 
Android..imp google
Android..imp googleAndroid..imp google
Android..imp google
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability study
 
Android gui framework
Android gui frameworkAndroid gui framework
Android gui framework
 
Hackernote on gsoc
Hackernote on gsocHackernote on gsoc
Hackernote on gsoc
 
Boot2Gecko Hackernote
Boot2Gecko HackernoteBoot2Gecko Hackernote
Boot2Gecko Hackernote
 

Dernier

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
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
 
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
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Dernier (20)

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
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
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
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
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Lec19-CS110 Computational Engineering

  • 1. CS110 Computational Engineering Function Calls Feb, 19 - 2008 V. Kamakoti Course Material – P.Sreenivasa Kumar, N.S.Narayanaswamy, Deepak Khemani, V. Kamakoti– CS&E, IIT M 1
  • 2. Running with less memory • Functions • Provided to break up our problem into more basic units • Control flow – flows from function to function, saving the current context, changing contexts, then returning….. • Helps the program to run with lesser memory, but slower than in the case of a long big program • The issue now with data associated with other functions. • Typically functions communicate using the arguments and return values PSK, NSN, DK, VK – CS&E, IIT M 2
  • 3. Call by Value In C, function arguments are passed “by value” – values of the arguments given to the called function in temporary variables rather than the originals – the modifications to the parameter variables do not affect the variables in the calling function “Call by reference” – variables are passed by reference • subject to modification by the function – achieved by passing the “address of” variables PSK, NSN, DK, VK – CS&E, IIT M 3
  • 4. Call by Value - an example main( ) { Function prototype int p = 1, q = 2, r = 3, s; int test(int, int, int); Function call …; s = test (p, q, r); … /* s is assigned 9 */ } /* p,q,r don’t change, only their copies do */ int test( int a, int b, int c){ a ++; b ++; c ++; return (a + b + c); } PSK, NSN, DK, VK – CS&E, IIT M Function definition 4
  • 5. Call by Reference #include <stdio.h> void quoRem(int, int, int*, int*); main(){ int x, y, quo, rem; scanf(“%d%d”, &x, &y); quoRem(x, y, &quo, &rem); printf(“%d %d”, quo , rem); } /*pointers*/ Passing addresses Does not return anything void quoRem(int num, int den, int* quoAdr, int* remAdr){ *quoAdr = num / den; *remAdr = num % den; } PSK, NSN, DK, VK – CS&E, IIT M 5
  • 6. Recursive Function Example int power (int num, int exp) { int p; if (exp = = 1) return num; p = power(num, exp/2); if (exp % 2 = = 0) return p*p; else return p*p*num; } The base case exp = 1 Guarantees termination power (3, 13) 36*36*3 = 729*729*3 = 1594323 power (3, 6) 33*33 = 27*27 = 729 power(3,3) 31*31*3 = 27 power(3,1) = 3 PSK, NSN, DK, VK – CS&E, IIT M 6
  • 7. Factorial (n) • n! = 1 * 2 * 3 * .... * (n-2) * (n-1) * n Iterative version int fact(int n) { int i; int result; result = 1; for (i = 1; i <= n; i++) result = result * i; return result; } PSK, NSN, DK, VK – CS&E, IIT M In practice int may not be enough! 7
  • 8. Factorial (n) – recursive program fact(n) = n*fact(n-1) int fact(int n) { if (n == 1) return 1; return n * fact(n - 1); } • Shorter, simpler to understand • Uses fewer variables • Machine has to do more work running this one! PSK, NSN, DK, VK – CS&E, IIT M 8
  • 9. Pending computations • In this recursive version the calling version still has pending work after it gets the return value. int fact(int n) { if (n == 1) return 1; return n * fact(n - 1); } (fact 4) 4 * (fact 3) It needs to save some values for future use 3 * (fact 2) 2 * (fact 1) 1 2*1 =2 3*2 = 6 4*6 = 24 PSK, NSN, DK, VK – CS&E, IIT M 9
  • 10. Tail recursion int fact(n) { return fact_aux(n, 1); } Auxiliary variable int fact_aux(int n, int result) { if (n == 1) return result; return fact_aux(n - 1, n * result) } PSK, NSN, DK, VK – CS&E, IIT M The recursive call is in the return statement. The function simply returns what it gets from the call it makes. The calling version does not have save any values! 10