230 likes | 347 Views
This comprehensive guide explores the concept of functions in programming, detailing their definitions, advantages, and practical use. Functions are sections of code that enhance program readability and maintainability, allowing code reuse and division of labor among programmers. The guide covers topics like function declaration, definition, and the differences between passing arguments by value and by reference. It provides illustrative examples and syntax to help you understand how to declare and define functions effectively, along with how to call them within your program.
E N D
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? 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
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
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
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
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
What is a Function? • Function Prototype declaration Eg void calcsum(); intcalcmean(); double calcstdev();
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
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.
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; }
// function definition //define the function writetitle() void writetitle() { string dept = “Computer Science”; cout << dept << endl; }
// function definition //define the function calcsum() void calcsum() { int val1 = 20; int val2 = 35; int tot = val1 + val2; cout << “The sum is = “ << tot endln; }
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
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
// Function arguments Note the FUNCTION the Caller FUNCTION Int main() { ................ writeline(); calcsum(); .................... }
// 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
// Function passing argumentsby value • Note the Function Prototype arguments and the Function definition arguments // Function Prototype declaration intgetmax(int n1, int n2); intgetnum();
// Function passing argumentsby value • // function definition intgetnum() { int num cout << “Enter a number”; cin >> num; return num; }
// Function passing argumentsby value • // function definition intgetmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }
// 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; }
// Function passing argumentsby value intgetnum() { int num cout << “Enter a number”; cin >> num; return num; }
// Function passing argumentsby value • // function definition intgetmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }
// 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