Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS149D Elements of Computer Science

Similar presentations


Presentation on theme: "CS149D Elements of Computer Science"— Presentation transcript:

1 CS149D Elements of Computer Science
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 23: 11/21/2002 Lecture 23: 11/21/2002 CS149D Fall 2002

2 Outline Modular Programming with Functions (Chapter 4, Section 5.2)
Lecture 23: 11/21/2002 CS149D Fall 2002

3 Library Functions Examples of library functions we saw before
sqrt  calculates the square root sqrt is a value-returning function which takes one parameter x = sqrt(y) We did not have to implement the sqrt function ourselves, but had to include “math.h” for its definition We could reuse over and over again the sqrt function, while we are sure we will get the correct result Lecture 23: 11/21/2002 CS149D Fall 2002

4 Programmer-defined Functions
For longer complex problems we write programs that use the main function and additional functions instead of using one long main function What are the advantages? A function can be written and tested separately from the rest of the program (we can divide writing large programming projects between a group of programmers) An individual solution is smaller than the complete program (remember the Divide and Conquer approach) Once a function has been tested, it can be reused in new programs without being retested (reusability) We can think of our functions as black boxes that have specific input and that compute specific information and maybe return a value (abstraction) Lecture 23: 11/21/2002 CS149D Fall 2002

5 Function definition return_type function_name (parameters declaration) //Note that there is no ; here { //Function body //if return_type is the word void, this function is a void function, it does not return a value } void printHello () { cout << “*************************”; cout << “ Hello World”; } return; // this function computes x to the nth power. // n >= 0 and result <= maximum integer number int power (int x, int n) { int result = 1; while (n > 0) result = result *x; n--; } return result; Lecture 23: 11/21/2002 CS149D Fall 2002

6 Function Call from within Program1/2
void main () { int x = 10, n = 3,y; int power (int x,int n); //Function prototype, note the ; at the end of the statement y = power (x,n); //Function call x and y are called arguments (actual parameters) cout << y; //can just write cout << power(x,n); if value need not be stored in y } // this function computes x to the nth power. int power (int x, int n) // x and n are called parameters (formal parameters) int result = 1; // result is a local variable to the function power while (n > 0) result = result *x; n--; return result; Lecture 23: 11/21/2002 CS149D Fall 2002

7 Function Call from within Program2/2
void main () { …… …….. y = power (x,n); //Function call cout << y; ……… } // this function computes x to the nth power. int power (int x, int n) ……. return result; Function executes Lecture 23: 11/21/2002 CS149D Fall 2002

8 Call by Value // this function computes x to the nth power.
int power (int x, int n) { int result = 1; // result is a local variable to the function power while (n > 0) result = result *x; n--; } return result; Question: Can a function change the value of its arguments? This type of function call is a call by value, which means the function can not change the value of its arguments, even if it attempted to. The change in the value of n in the function is not visible to the caller function (main) An exception is when the parameter is an array Lecture 23: 11/21/2002 CS149D Fall 2002

9 Arrays as Function Parameters1/2
//Function prototype. Note you do not need to specify the size of the array double average_Array (double A[], int number); //Function prototype //main void main () { const int N = 100; double array[N],average; …….. average = averageArray(100,N) cout << average; } // this function computes the average of an array of doubles double average_Array (double A[], int number) double sum; for (int k = 0; k < number ; k++) sum = sum + A[k]; return sum/number; Lecture 23: 11/21/2002 CS149D Fall 2002

10 Arrays as Function Parameters2/2
When a simple variable is used as a parameter, the value is passed to the formal parameter in the function, and thus the original value can not be changed. When an array is passed as a parameter to a function, the memory address of the array (the address of the first element in the array) is passed to the function, instead of the entire set of values The function references values in the original array (call by reference), which means the function can change the values of the elements if desired void FillArray (int A[], int N); //Function prototype void main () { int my_array[100]; FillArray(my_array,100); //Print array elements } void FillArray (int A[], int N) for (int k = 0; k < N ; k++) A[k] = k; Lecture 23: 11/21/2002 CS149D Fall 2002


Download ppt "CS149D Elements of Computer Science"

Similar presentations


Ads by Google