SlideShare a Scribd company logo
COMPUTATIONAL PHYSICS
LEC#05
REHMAN RASHEED
Part 6: Functions
Functions The best way to develop and maintain a
large program is to construct it from smaller parts
(modules). Modules in C++ are called functions and
classes. C++ standard library has many useful
functions. Functions written by a programmer are
programmer-defined-functions.
Math Library Functions Math library functions allows
to perform most common mathematical calculations
Some math library functions: cos(x) sin(x) tan(x)
sqrt(x) exp(x) log(x) log10(x) pow(x,y) fabs(x)
floor(x) fmod(x,y) ceil(x)
Functions prototypes A function-prototype tells the
compiler the name of the function, the type of data
returned by the function, the number of parameters,
the type of parameters, and the order of parameters.
Function prototype: value-type function-name (par-
type1, par-type2, …) The compiler uses function
prototypes to validate function calls
Functions definitions Function definition: return-
value-type function-name(parameter-list) {
declarations and statements (function body) } A type
must be listed explicitly for each parameter in the
parameter-list of a function All variables declared in
function definitions are local variables – they are
known only in the function.
//example: a programmer-defined function #include
using namespace std; int square( int ); // function
prototype int main() { for ( int x = 1; x <= 10; x++ )
cout << square( x ) << " "; cout << endl; return 0; }
// Function definition int square( int y ) { int result;
result = y * y; return result; } OUTPUT 1 4 9 16 25
36 49 64 81 100
Functions definitions If a function does not receive
any values parameter-list is void or left empty. If a
function does not return any value, then return-
value-type of that function is void both in the
function prototype and function definition
//example: a "void" case #include using namespace
std; void out2(void); // function prototype int main()
{ out2(); return 0; } // Function definition void
out2(void) { cout << "output from function out2" <<
endl; return; } OUTPUT output from function out2
Part 7: Arrays
Arrays An array is a consecutive group of memory
locations that all have the same name and the same
type. To refer to a particular location or element in
the array, we specify the name of the array and the
position number of the particular element in the
array. The first element in every array is the 0th
element.
Arrays in C/C++ Most of us were not taught by our
mothers to count on our fingers starting with the
thumb as zero! Accordingly, you will probably make
fewer n - 1 errors if you do not use zero subscripts
when dealing with matrices.
Declaring Arrays Arrays occupy space in memory.
The programmer specifies the type of elements and
the number of elements required, so that the
compiler may reserve the appropriate amount of
memory. Example: reserve 12 elements for integer
array c Example: declaration and initialization of an
array n int c[12]; int n[6]={2, 18, 33, 5, 21, 39};
// Initialize array a and fill with numbers #include
#include using namespace std; int main() { const int
arraySize = 5; int i, a[ arraySize ]; for ( i = 0; i <
arraySize; i = i + 1 ) a[ i ] = 2 * i; cout
<<"Element"<<setw(12)<<"Value"<< endl; for ( i =
0; i < arraySize; i = i + 1 ) cout
<<setw(7)<<i<<setw(12)<<
cp05.pptx

More Related Content

Similar to cp05.pptx

C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithmsNico Ludwig
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.Mohamed Fawzy
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts Pavan Babu .G
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndEdward Chen
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerApache Traffic Server
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonCP-Union
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptMuhammad Sikandar Mustafa
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in cfloraaluoch3
 

Similar to cp05.pptx (20)

C language
C languageC language
C language
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithms
 
Clanguage
ClanguageClanguage
Clanguage
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic Server
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 

More from RehmanRasheed3

Principle And Working of A Semiconductor Laser.pptx
Principle And Working of A Semiconductor Laser.pptxPrinciple And Working of A Semiconductor Laser.pptx
Principle And Working of A Semiconductor Laser.pptxRehmanRasheed3
 
Cocchi_talk_EWPAA2019.pptx
Cocchi_talk_EWPAA2019.pptxCocchi_talk_EWPAA2019.pptx
Cocchi_talk_EWPAA2019.pptxRehmanRasheed3
 
Numerical Methods.pptx
Numerical Methods.pptxNumerical Methods.pptx
Numerical Methods.pptxRehmanRasheed3
 
Gauss jordan method.pptx
Gauss jordan method.pptxGauss jordan method.pptx
Gauss jordan method.pptxRehmanRasheed3
 

More from RehmanRasheed3 (8)

Principle And Working of A Semiconductor Laser.pptx
Principle And Working of A Semiconductor Laser.pptxPrinciple And Working of A Semiconductor Laser.pptx
Principle And Working of A Semiconductor Laser.pptx
 
Cocchi_talk_EWPAA2019.pptx
Cocchi_talk_EWPAA2019.pptxCocchi_talk_EWPAA2019.pptx
Cocchi_talk_EWPAA2019.pptx
 
chapter17.ppt
chapter17.pptchapter17.ppt
chapter17.ppt
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Numerical Methods.pptx
Numerical Methods.pptxNumerical Methods.pptx
Numerical Methods.pptx
 
5-1-reimann-sums.ppt
5-1-reimann-sums.ppt5-1-reimann-sums.ppt
5-1-reimann-sums.ppt
 
Prerna actual.pptx
Prerna actual.pptxPrerna actual.pptx
Prerna actual.pptx
 
Gauss jordan method.pptx
Gauss jordan method.pptxGauss jordan method.pptx
Gauss jordan method.pptx
 

Recently uploaded

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfPo-Chuan Chen
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 

Recently uploaded (20)

B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 

cp05.pptx

  • 3. Functions The best way to develop and maintain a large program is to construct it from smaller parts (modules). Modules in C++ are called functions and classes. C++ standard library has many useful functions. Functions written by a programmer are programmer-defined-functions.
  • 4. Math Library Functions Math library functions allows to perform most common mathematical calculations Some math library functions: cos(x) sin(x) tan(x) sqrt(x) exp(x) log(x) log10(x) pow(x,y) fabs(x) floor(x) fmod(x,y) ceil(x)
  • 5. Functions prototypes A function-prototype tells the compiler the name of the function, the type of data returned by the function, the number of parameters, the type of parameters, and the order of parameters. Function prototype: value-type function-name (par- type1, par-type2, …) The compiler uses function prototypes to validate function calls
  • 6. Functions definitions Function definition: return- value-type function-name(parameter-list) { declarations and statements (function body) } A type must be listed explicitly for each parameter in the parameter-list of a function All variables declared in function definitions are local variables – they are known only in the function.
  • 7. //example: a programmer-defined function #include using namespace std; int square( int ); // function prototype int main() { for ( int x = 1; x <= 10; x++ ) cout << square( x ) << " "; cout << endl; return 0; } // Function definition int square( int y ) { int result; result = y * y; return result; } OUTPUT 1 4 9 16 25 36 49 64 81 100
  • 8. Functions definitions If a function does not receive any values parameter-list is void or left empty. If a function does not return any value, then return- value-type of that function is void both in the function prototype and function definition
  • 9. //example: a "void" case #include using namespace std; void out2(void); // function prototype int main() { out2(); return 0; } // Function definition void out2(void) { cout << "output from function out2" << endl; return; } OUTPUT output from function out2
  • 11. Arrays An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. The first element in every array is the 0th element.
  • 12. Arrays in C/C++ Most of us were not taught by our mothers to count on our fingers starting with the thumb as zero! Accordingly, you will probably make fewer n - 1 errors if you do not use zero subscripts when dealing with matrices.
  • 13. Declaring Arrays Arrays occupy space in memory. The programmer specifies the type of elements and the number of elements required, so that the compiler may reserve the appropriate amount of memory. Example: reserve 12 elements for integer array c Example: declaration and initialization of an array n int c[12]; int n[6]={2, 18, 33, 5, 21, 39};
  • 14. // Initialize array a and fill with numbers #include #include using namespace std; int main() { const int arraySize = 5; int i, a[ arraySize ]; for ( i = 0; i < arraySize; i = i + 1 ) a[ i ] = 2 * i; cout <<"Element"<<setw(12)<<"Value"<< endl; for ( i = 0; i < arraySize; i = i + 1 ) cout <<setw(7)<<i<<setw(12)<<