Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS - What Is A Function? - Advantages Function Declaration

Similar presentations


Presentation on theme: "FUNCTIONS - What Is A Function? - Advantages Function Declaration"— 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-type function-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();
int calcmean(); 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
- 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 arguments by value
Note the Function Prototype arguments and the Function definition arguments // Function Prototype declaration int getmax(int n1, int n2); int getnum();

18 // Function passing arguments by value
// function definition int getnum() { int num cout << “Enter a number”; cin >> num; return num; }

19 // Function passing arguments by value
// function definition int getmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }

20 // Function passing arguments by value
#include<iostream> using namespace std; // function declaration prototype int getnum(); int getmax(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 arguments by value
int getnum() { int num cout << “Enter a number”; cin >> num; return num; }

22 // Function passing arguments by value
// function definition int getmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }

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


Download ppt "FUNCTIONS - What Is A Function? - Advantages Function Declaration"

Similar presentations


Ads by Google