Presentation is loading. Please wait.

Presentation is loading. Please wait.

Built-In and user-Defined functions 773.707 Software Design Concepts Lecture IV Dr. Sothy Vignarajah.

Similar presentations


Presentation on theme: "Built-In and user-Defined functions 773.707 Software Design Concepts Lecture IV Dr. Sothy Vignarajah."— Presentation transcript:

1 Built-In and user-Defined functions 773.707 Software Design Concepts Lecture IV Dr. Sothy Vignarajah

2 AVignarajah 773.707 Software Design Concepts lecture VIII 2 Functions Functions serve different purposes: - To break a large complex program into small, manageable tasks. - To allow programmers to use existing code to perform common tasks. Example - a string copy - A function once written can be called many times without having to duplicate the code in each of those instances. Example – drawAline.

3 AVignarajah 773.707 Software Design Concepts lecture VIII 3 Functions Contd. A block of code that performs a specific task Built-in Functions – provided in the predefined language environment like the math or standard input/output functions Programmer-defined functions created by the programmer – void or value returning functions.after creating a function, the programmer can invoke(call) the function from one or more places in the program.

4 AVignarajah 773.707 Software Design Concepts lecture VIII 4 Functions Contd. Void functions are functions that do not return a value.You create and invoke a programmer-defined function by including a function definition and one or more calls in a program. If a function definition appears below the main function, you will also need a function prototype in the program.

5 AVignarajah 773.707 Software Design Concepts lecture VIII 5 Void Functions Contd. The function prototype is entered above the main program.(if entire code of function entered above main function, then a prototype is not needed – will discuss that in class ie the program will compile user-defined function before it compiles the main function) The function’s definition header as well as the function prototype must begin with the keyword void. The call to a void function appears as a separate statement in a program.

6 AVignarajah 773.707 Software Design Concepts lecture VIII 6 Programmer-Defined Function Definition Syntax for a void function in C++: void functionName(optional parameterlist) { one or more statements ending with a semicolon }

7 AVignarajah 773.707 Software Design Concepts lecture VIII 7 Passing information to a programmer-defined function To allow a function to receive information that is passed to it, one or more formal parameters are included in the function header’s parameter list. Both the data type and the name of the formal parameters must be explicitly stated in the parameter list.

8 AVignarajah 773.707 Software Design Concepts lecture VIII 8 Programmer-defined function Contd. The function call however includes only the name, and not the data type of the actual arguments. Example: Function call: displayGross(hrsWked, payRate); Function Header: void displayGross( float hrs, float hrlyPay)

9 AVignarajah 773.707 Software Design Concepts lecture VIII 9 Scope and Lifetime of a Variable A variable’s scope indicates which portions of the program can use the variable. A variable’s lifetime indicates how long the variable remains in memory. The scope which can be local or global, is determined by where you declare the variable in the program.

10 AVignarajah 773.707 Software Design Concepts lecture VIII 10 Scope of a Variable contd. Local variables are declared in a statement block, which is a group of statements enclosed in braces. Exceptions to this rule are the formal parameters listed in a function header. Local variables are known only to the function or statement block in which they are declared.

11 AVignarajah 773.707 Software Design Concepts lecture VIII 11 Scope of a Variable contd. Global variables are declared outside of any function in the program.Unlike a local variable which can be used only by the function or block in which it is declared, a global variable can be used by any statement that follows its declaration in the program.

12 AVignarajah 773.707 Software Design Concepts lecture VIII 12 Scope of a Variable contd. Declaring a variable global rather than local, allows unintentional errors to occur when a function that should not have access to it inadvertently changes its contents. If more than one function needs access to the same variable, it is better to create a local variable in one of the functions and to pass it only to the other functions that need it.

13 AVignarajah 773.707 Software Design Concepts lecture VIII 13 Passing Variables by Value and by Reference When a variable is passed by value, C++ passes only the contents of the variable to the receiving function. The receiving function does not have access to the variable in memory, hence it cannot change the variable’s contents. You pass a variable by value when the receiving function needs to know only the value stored inside the variable, but the receiving function does not need to change the value stored in the variable.

14 AVignarajah 773.707 Software Design Concepts lecture VIII 14 Passing Variables by Value and by Reference Contd. When a variable passes a variable by reference, C++ passes the variable’s address ie. The location in memory. You pass a variable by reference when the receiving function needs to change the contents of the variable.

15 AVignarajah 773.707 Software Design Concepts lecture VIII 15 // C++ program uses a void function to calculate the square and square root of a number, and a void function to display a line. #include // function prototypes void displayLine(); void calcSquareSqRoot(float, float &, float &) void main() { float numberInput = (float) 0.0; float square = (float) 0.0; float sqRoot = (float) 0.0; cout << “Enter a number:”; cin >> numberInput; calcSquareSqRoot(numberInput, square, sqRoot); displayLine(); cout << “Square and Square Root cacluations”; cout << “ Square: “ << square << endl; cout << “ Square Root: “ << sqRoot << endl; }

16 AVignarajah 773.707 Software Design Concepts lecture VIII 16 void displayLine() { cout << “-----------------------------------------------“; cout <, endl; } void calcSquareSqRoot(float numberInput, float &square, float &sqRoot) { square = (float) pow(numberInput,2); sqRoot = (float) sqrt(numberInput); }


Download ppt "Built-In and user-Defined functions 773.707 Software Design Concepts Lecture IV Dr. Sothy Vignarajah."

Similar presentations


Ads by Google