1 / 32

Data Structures

Data Structures. AL- Furat AL - Awsat Technical University Karbala Technical Institute Department - Computer Systems Techniques. To the second year students. Prepared by the lecturer:. Mohammed Thajeel. 2016-2017. Lecture (8&9). Outline. Modular Programming.

caseya
Download Presentation

Data Structures

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. Data Structures AL- Furat AL - Awsat Technical University Karbala Technical Institute Department - Computer Systems Techniques To the second year students Prepared by the lecturer: Mohammed Thajeel 2016-2017

  2. Lecture (8&9) Outline • Modular Programming • Defining and Calling Functions • Void Functions • Calling a Void Function • Function Declarations or Function Prototypes • Sending Data into a Function • Passing Data by Value • Passing Data by Reference variable • The return Statement • Returning a Value from a Function • Calling a Value-Returning Function • Returning a Boolean Value

  3. Modular Programming • A program may be broken up into a set of manageable functions, or modules. This is called modular programming. • A functionis a collection of statements that performs a specific task. • So far you have used functions in two ways: 1) you have created a function called main in every program you’ve written, and 2) you have called library functions such as powand sqrt. • Another reason to write functions is that they simplify programs. If a specific task is performed in several places in a program, a function can be written once to perform that task, and then be executed anytime it is needed. This benefit of using functions is known as code reusebecause you are writing the code to perform a task once and then reusing it each time you need to perform the task. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (1)

  4. Modular Programming cont’d Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (2)

  5. Defining and Calling Functions • A function definition contains the statements that make up the function. • A function call is a statement that causes a function to execute. • When creating a function, you must write its definition. All function definitions have the following parts: • Return type: A function can send a value back to the program module that called it. The return type is the data type of the value being sent back. • Name: Every function must have a name. In general, the same rules that apply to variable names also apply to function names. • Parameter list: The program module that calls a function can send data to it. The parameter list is the list of variables that hold the values being passed to the function. If no values are being passed to the function, its parameter list is empty. • Body: The body of a function is the set of statements that carry out the task the function is performing. These statements are enclosed in a set of braces. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (3)

  6. Defining and Calling Functions cont’d • The above figure shows the definition of a simple function with the various parts labeled. Notice that the function’s return type is actually listed first. • The line in the definition that reads int main () is called the function header. Data Structures : Lecture (8&9) page (4) Prepared by : Mohammed Thajeel

  7. Void Functions • You already know that a function can return a value. The main function in all of the programs you have seen before is declared to return an int value to the operating system. • The return 0; statement causes the value 0 to be returned when the main function finishes executing. • It isn’t necessary for all functions to return a value, however. Some functions simply perform one or more statements and then return. In C++ these are called void functions. • The displayMessagefunction shown here is an example: void displayMessage(){ cout <<"Hello from the function displayMessage.\n"; } Data Structures : Lecture (8&9) page (5) Prepared by : Mohammed Thajeel

  8. Calling a Void Function • A function is executed when it is called. Function main is called automatically when a program starts, but all other functions must be executed by function call statements. • When a function is called, the program branches to that function and executes the statements in its body. Let’s look at following Program, which contains two functions: mainanddisplayMessage. Data Structures : Lecture (8&9) page (6) Prepared by : Mohammed Thajeel

  9. Calling a Void Function cont’d // This program has two functions: main and displayMessage. #include <iostream> voiddisplayMessage(){ // This function displays a greeting cout << "Hello from the function displayMessage.\n"; } int main(){ // main function cout << "Hello from main.\n"; displayMessage(); // Call displayMessage cout << "Back in function main again.\n"; return 0; } Program Output Hello from main. Hello from the function displayMessage. Back in function main again. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (7)

  10. Calling a Void Function cont’d • The function displayMessage is called by the following statement: displayMessage(); • This statement is the function call. It is simply the name of the function followed by a set of parentheses and a semicolon. Let’s compare this with the function header: Function Header voiddisplayMessage() Function Call displayMessage(); • The function header is part of the function definition. It declares the function’s return type, name, and parameter list. It must not be terminated with a semicolon because the definition of the function’s body follows it. • The function call is a statement that executes the function, so it is terminated with a semicolon like all other C++ statements. Notice that the function call does not list the return type and, if the program is not passing data into the function, the parentheses are left empty. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (8)

  11. Calling a Void Function cont’d • How program flows. It starts, of course, in function main. When the call to displayMessageis encountered, the program branches to that function and performs its statements. • Once displayMessage has finished executing, the program branches back to function main and resumes with the line that follows the function call. This is illustrated in fellow figure: Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (9)

  12. Calling a Void Function cont’d // The function displayMessage is repeatedly called from within a loop. #include <iostream> void displayMessage(){ // This function displays a greeting cout << "Hello from the function displayMessage.\n"; } int main(){ // main function cout << "Hello from main.\n"; for (int count = 0; count < 3; count++) displayMessage(); // Call displayMessage cout << "Back in function main again.\n"; return 0; } Program Output Hello from main. Hello from the function displayMessage. Hello from the function displayMessage. Hello from the function displayMessage. Back in function main again. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (10)

  13. Calling a Void Function cont’d • It is possible to have many functions and function calls in a program. The next program has three functions: main, first, and second. #include <iostream> void first() { cout<< "I am now inside the function first.\n"; } voidsecond() { cout<< "I am now inside the function second.\n"; } int main() { cout<<"I am starting in function main.\n"; first(); // Call function first second(); // Call function second cout<< "Back in function main again.\n"; return 0; } Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (11)

  14. Calling a Void Function cont’d Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (12)

  15. Calling a Void Function cont’d • Functions may also be called in a hierarchical, or layered. This is demonstrated by the next program , which has three functions: main, deep, and deeper. #include <iostream> void deeper() { cout<< "I am now inside the function deeper.\n"; } voiddeep() { cout<< "I am now inside the function deep.\n"; deeper(); // Call function deeper cout<< "Now I am back in deep.\n"; } int main() { cout<<"I am starting in function main.\n"; deep(); // Call function deep cout<< "Back in function main again.\n"; return 0; } Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (13)

  16. Calling a Void Function cont’d Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (14)

  17. Function Declarations or Function Prototypes • A function Declarations eliminates the need to place a function definition before all calls to the function. • Before the compiler encounters a call to a particular function, it must already know certain things about the function. In particular, it must know the number of parameters the function uses, the type of each parameter, and the return type of the function. • FunctionDeclarations are usually placed near the top of a program so the compiler will encounter them before any function calls. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (15)

  18. Function Declarations or Function Prototypes cont’d #include <iostream> void first(); // Function first declaration voidsecond(); // Function second declaration int main() { cout<<"I am starting in function main.\n"; first(); // Call function first second(); // Call function second cout << "Back in function main again.\n"; return 0; } void first() { cout << "I am now inside the function first.\n"; } voidsecond() { cout << "I am now inside the function second.\n"; } Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (16)

  19. Sending Data into a Function • When a function is called, the program may send values into the function. • Values that are sent into a function are called arguments. • A parameteris a special variable that holds a value being passed as an argument into a function. By using parameters, you can design your own functions that accept data this way. Here is the definition of a function that uses a parameter: voiddisplayValue(intnum){ // Function definition cout<< "The value is " << num << endl;} • Notice it is not necessary to list the name of the parameter variable inside the parentheses in function declaration . Only the data type of the variable is required. The function declaration could have been written like this: voiddisplayValue(int); // Function declaration Data Structures : Lecture (8&9) page (17) Prepared by : Mohammed Thajeel

  20. Sending Data into a Function cont’d // This program demonstrates a function with a parameter. #include <iostream> voiddisplayValue(intnum); // Function declaration int main(){ // main function cout<< "I am passing 5 to displayValue.\n"; displayValue(5); // Call displayValue with argument 5 cout<< "Now I am back in main.\n"; return 0;} voiddisplayValue(intnum){ cout<< "The value is " << num << endl;} Program Output I am passing 5 to displayValue. The value is 5 Now I am back in main. Data Structures : Lecture (8&9) page (18) Prepared by : Mohammed Thajeel

  21. Sending Data into a Function cont’d // This program demonstrates a function with a parameter. #include <iostream> voiddisplayValue(int ); // Function declaration int main(){ // main function cout<< "I am passing several values to displayValue.\n"; displayValue(5); displayValue(10); displayValue(2); displayValue(16); cout<< "Now I am back in main.\n"; return 0;} voiddisplayValue(intnum){ cout<< "The value is " << num << endl;} Program Output I am passing several values to displayValue. The value is 5 The value is 10 The value is 2 The value is 16 Now I am back in main. Data Structures : Lecture (8&9) page (19) Prepared by : Mohammed Thajeel

  22. Passing Data by Value • When an argument is passed into a parameter by value, only a copy of the argument’s value is passed. Changes to the parameter do not affect the original argument. • Normally when information is passed to a function it is passed by value. This means the parameter receives a copy of the value that is passed to it. If a parameter’s value is changed inside a function, it has no effect on the original argument. The next Program demonstrates this concept. Data Structures : Lecture (8&9) page (20) Prepared by : Mohammed Thajeel

  23. Passing Data by Value cont’d // This program demonstrates that changes to a function parameter have no effect on the original argument. #include <iostream> voidchangeMe(int); // Function declaration int main(){ // main function intnumber = 12; cout<< "In main number is " << number << endl; changeMe(number); cout<< "Back in main again, number is still " << number << endl; return 0;} voidchangeMe(intmyValue){ myValue = 0; cout << "In changeMe, the value has been changed to "<< myValue << endl;} Program Output In main number is 12 In changeMe, the value has been changed to 0 Back in main again, number is still 12 Data Structures : Lecture (8&9) page (21) Prepared by : Mohammed Thajeel

  24. Passing Data by Reference variable • A reference variable is a variable that references the memory location of another variable. Any change made to the reference variable is actually made to the one it references. Reference variables are sometimes used as function parameters. • A reference variable is an alias for another variable. Instead of having its own memory location for storing data, it accesses the memory location of another variable. • Reference variables are defined like regular variables, except there is an ampersand (&) between the data type and the name. For example, the following function definition makes the parameter refVar a reference variable: voiddoubleNum(int &refVar){ // Function definition refVar*= 2;} Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (22)

  25. Passing Data by Reference variable cont’d // This program uses a reference variable as a function parameter. #include <iostream> voiddoubleNum (int &refVar); // Function declaration int main(){ // main function intvalue = 4; cout<< "In main, value is " << value << endl; cout<< "Now calling doubleNum..." << endl; doubleNum(value); cout<< "Now back in main, value is " << value << endl; return 0;} voiddoubleNum (int &refVar) { refVar *= 2;} Program Output In main, value is 4 Now calling doubleNum... Now back in main, value is 8 Data Structures : Lecture (8&9) page (23) Prepared by : Mohammed Thajeel

  26. The return Statement • The return statement causes a function to end immediately. // This program uses a function to perform division. It illustrates the return statement. #include <iostream> voiddivide(double arg1, double arg2); // Function declaration intmain(){ // main function doublenum1, num2; cout<<"Enter two numbers and I will divide the first\n"; cout<< "number by the second number: "; cin>> num1 >> num2; divide(num1, num2); return0;} voiddivide(double arg1, double arg2){ if (arg2 == 0.0){ cout<< "Sorry, I cannot divide by zero.\n"; return;} cout<< "The quotient is " << (arg1 / arg2) << endl; } Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (24)

  27. Returning a Value from a Function • A function may send a value back to the part of the program that called the function. • Functions that return a value are known as value-returning functions. • When you are writing a value-returning function, you must decide what type of value the function will return. • Here is an example of a function that returns an int value: intsum(intnum1, intnum2){ intresult; result= num1 + num2; returnresult; } Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (25)

  28. Calling a Value-Returning Function • When you call a value-returning function, you usually want to do something meaningful with the value it returns. The next program shows a function’s return value being assigned to a variable. // This program uses a function that returns a value. #include <iostream> intsum(int num1, intnum2); // Function declaration intmain() intvalue1 = 20, value2 = 40; // The first and second value inttotal; // Holds the returned total total = sum(value1, value2); // Display the sum of the values. cout<< "The sum of " << value1 << " and "<< value2 << " is " << total << endl; return0; intsum(int num1, intnum2){ returnnum1 + num2; } Program Output The sum of 20 and 40 is 60 Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (26)

  29. Calling a Value-Returning Function cont’d • This is commonly how return values are used, but you can do many other things with them as well. For example, the following code shows a math expression that uses a call to the sum function: intx = 10, y = 15; doubleaverage; average = sum(x, y) / 2.0; • Here is another example: intx = 10, y = 15; cout<< "The sum is " << sum(x, y) << endl; Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (27)

  30. Returning a Boolean Value • Functions may return true or false values. • Frequently there is a need for a function that tests an argument and returns a true or false value indicating whether or not a condition is satisfied. Such a function would return a boolvalue. • The next program shows example of a function whose return type is bool. This program has a function named isEven which returns true if its argument is an even number. Otherwise, the function returns false. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (28)

  31. Returning a Boolean Value cont’d // This program uses a function that returns true or false. #include <iostream> boolisEven(int); intmain(){ intval; // Holds the value to be tested cout<<"Enter an integer and I will tell you if it is even or odd: "; cin>> val; // Get a number from the user if (isEven(val)) cout<< val << " is even.\n"; else cout<< val << " is odd.\n"; return 0;} boolisEven(int number){ if (number % 2 == 0) returntrue; // The number is even if there's no remainder else returnfalse; // Otherwise, the number is odd } Program Output with Example Input Shown in Bold Enter an integer and I will tell you if it is even or odd: 5[Enter] 5 is odd. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (29)

  32. Returning a Boolean Value cont’d • There are several other ways to isEven function could have been written. Let’s compare three different ways to write it. Prepared by : Mohammed Thajeel Data Structures : Lecture (8&9) page (30)

More Related