1 / 23

FUNCTIONS

FUNCTIONS. - What Is A Function? - Advantages Function Declaration Function Definition Variable scope Function Arguments Passing Arguments by value Passing Arguments by Reference Calling other Functions / Recursive. What is a Function?.

lara-moses
Download Presentation

FUNCTIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FUNCTIONS - What Is A Function? - Advantages Function Declaration Function Definition Variable scope Function Arguments Passing Arguments by value Passing Arguments by Reference Calling other Functions / Recursive

  2. What is a Function? It is a section of code that is enclosed and that provides specific functionality to a program. i.e. - it is enclosed outside the main program - therefore it is called from outside the main program or other functions to provide a service / functionality

  3. What is a Function? • 3 Main Advantages • Code Easier :- They make the program code easier to understand and to maintain • Code Reuse :Tried and tested function can be reused by other programs • Division of labour : several programmers can divide the workload in a large project by working on different functions for the program

  4. What is a Function? • Function Declaration • functions must be declared early in the program code, just as it is done for primitive variables • It is declared as a prototype

  5. What is a Function? • Declaration syntax return-data-typefunction-name (arguments-list) Where return-data-type can be int string char double void among others

  6. What is a Function? where arguments-list - are values to be passed as arguments from the caller - and these can be of any quantity and data type - they must agree with those specified in the prototype function declaration

  7. What is a Function? • Function Prototype declaration Eg void calcsum(); intcalcmean(); double calcstdev();

  8. What is a Function? void calcsum(); declares a function named calcsum(); that accepts no argument -- () is empty and returns no value : because type is void

  9. What is a Function? • Definition - Appears later in the program code • Comprises a repeat of the prototype together with the function body where the function body is a pair of braces { } surrounding the statements that are to be executed when the function is called.

  10. Examples #Include<iostream> using namespace std; //declare the function prototype early void calcsum(); void writetitle(); int sum; int main() { writetitle(); // function calls from main rsum = calcsum(); cout << “The Sum is = “ << rsum << endl ; system(“pause”); return 0; }

  11. // function definition //define the function writetitle() void writetitle() { string dept = “Computer Science”; cout << dept << endl; }

  12. // function definition //define the function calcsum() void calcsum() { int val1 = 20; int val2 = 35; int tot = val1 + val2; cout << “The sum is = “ << tot endln; }

  13. Passing values to functions • Functions can be passed values • The caller function passes value in the form of arguments to a function that it calls • These can be of any quantity and data type • But they must agree with those specified in the prototype function declaration

  14. Functions can return a value • return - the function can return a value of any data-type, as long as it is of the type specified in the function prototype

  15. // Function arguments Note the FUNCTION the Caller FUNCTION Int main() { ................ writeline(); calcsum(); .................... }

  16. // Function arguments #Include<iostream> using namespace std; //declare the function prototype early void calcsum(); void writetitle(); int sum; int main() { writetitle(); // function calls from main calcsum(); system(“pause”); return 0; } //define the function writetitle() void writetitle() { string dept = “Computer Science”; cout << dept << endl; } //define the function calcsum() void calcsum() { int val1 = 20; int val2 = 35; int tot = val1 + val2; cout << “The sum is = “ << tot endln; } Pfunction.txt

  17. // Function passing argumentsby value • Note the Function Prototype arguments and the Function definition arguments // Function Prototype declaration intgetmax(int n1, int n2); intgetnum();

  18. // Function passing argumentsby value • // function definition intgetnum() { int num cout << “Enter a number”; cin >> num; return num; }

  19. // Function passing argumentsby value • // function definition intgetmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }

  20. // Function passing argumentsby value #include<iostream> using namespace std; // function declaration prototype intgetnum(); intgetmax(int n1 , int n2); //main function int main() { int num1 , num2; num1 = getnum(); num2 = getnum(); // determine the maximum of num1 and num2 cout << “Max number : “ << getmax(num1, num2) << endl; }

  21. // Function passing argumentsby value intgetnum() { int num cout << “Enter a number”; cin >> num; return num; }

  22. // Function passing argumentsby value • // function definition intgetmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }

  23. // Function passing argumentsby value • Passing arguments by value when arguments are passed to a function it is the value that is passed, not the variable itself

More Related