Lecture_Notes_21_22_23

Lecture_Notes_21_22_23 - Functions A function is a...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Functions A function is a subprogram that can act on data and return a value main() { Statement1; func1(); Statement2; func2(); func4(); } func1 _____ _____ return; func2 func3() _____ return; func4 _____ _____ return; func3 _____ _____ return;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Purpose of Functions? • Modularity – Each function performs a specific task and has independent local variables (does not depend on other functions) • Reuse of code – All of the code in the function is for a specific task. And, just call it when needed
Background image of page 2
Function Types Functions are of two types: Built-in: These functions are part of compiler package – they are supplied by manufacturer for your use. For example, float pow(float, float) User-defined: These functions you write yourself
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <iostream> #include <cstdlib> using namespace std; void print() { cout<< "Inside print function" <<endl; } int main() { print(); cout<< "main Function" <<endl; print(); system( "PAUSE" ); return 0; } A simple function When you don’t want your function to return a value, you declare its return type to be void .
Background image of page 4
Purpose of Functions - Example #include <iostream> using namespace std; int main() { double n=0, roundNumber=0; cout<< "Enter n" ; cin>>n; if (n>0) roundNumber= int (n+0.5); else roundNumber= int (n-0.5); cout<< "Rounded Number is " <<roundNumber<<endl; system( "PAUSE" ); return 0; } In this program, all the required statements are part of main function. However, to develop a large program, it is better to construct it from smaller pieces so that each piece can be more manageable
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1. #include <iostream> 2. using namespace std; 3. int rnd( double n) 4. { 5. if (n>=0) 6. return int (n+0.5); 7. else 8. return int (n-0.5); 9. } 10. int main() 11.{ 12. double n=0, roundNumber=0; 13. cout<< "Enter n" ; 14. cin>>n; 15. roundNumber=rnd(n); //this function round-off decimals 16. cout<< "Rounded Number is " <<roundNumber<<endl; 17. system( "PAUSE" ); 18. return 0; 19.} Purpose of Functions - Example Main function is cleaner and more understandable, less error prone
Background image of page 6
1. #include <iostream> 2. using namespace std; 3. int rnd( double n) 4. { 5. if (n>=0) 6. return int (n+0.5); 7. else 8. return int (n-0.5); 9. } 10. int main() 11.{ 12. double n=0, roundNumber=0; 13. cout<< "Enter n" ; 14. cin>>n; 15. roundNumber=rnd(n); //this function round-off decimals 16. cout<< "Rounded Number is " <<roundNumber<<endl; 17. system( "PAUSE" ); 18. return 0; 19.} Purpose of Functions - Example return-type name (parameters-list) information coming back information coming into function
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function definition, return-value, parameter-list, and arguments 1. #include <iostream> 2. using namespace std; 3. int rnd( double n) 4. { 5. statements; 6. return int (n-0.5); 7. } 8. int main() 9. { 10. statements; 11. roundNumber=rnd(n); //this function round-off decimals 12. statements; 13.} The format of function definition is return-value-type function-name ( parameter-list ) { statements; } This is the definition of rnd function return-value-type function-name parameter-list arguments
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 46

Lecture_Notes_21_22_23 - Functions A function is a...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online