Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS WITH ARGUMENTS

Similar presentations


Presentation on theme: "FUNCTIONS WITH ARGUMENTS"— Presentation transcript:

1 FUNCTIONS WITH ARGUMENTS
REVISITED This lecture is a revision on functions

2 1. Reference by-value vs. reference by-address
When the main program calls a function by its value, then the changes are not seen outside the function. This is known as reference by-value. When a program calls a function by its address, then the changes are seen by the whole program outside the function, even the function is of type void. This is known as reference by-address. Arrays are always referred to by addresses. Simple variables may be referred to by values (what we have learnt till now) or by address (to be explained later). In other words, when a function is called, the formal parameters are replaced by either a value or an address. Dr. Soha S. Zaghloul 2

3 The value of r is copied to radius.
2. EXAMPLE (1) #include <stdio.h> // Function prototype double CircleArea(double radius); // FUNCTION PROTOTYPE int main (void) { double circle, r; printf (“Enter circle radius> “); scanf (“%f”, r); circle = CircleArea( r ); // FUNCTION CALL printf (“Area of circle = %f”, circle); return (0); } // end main // start define your functions double CircleArea (double radius) // FUNCTION HEADER AND DEFINITION double area; area = 3.14 * radius * radius; return (area); // RETURN VALUE } // end CircleArea // end of program The value of r is copied to radius. Assume r = 5.0 Every word radius in the function will be replaced by the value of r, which is 5.0 Dr. Soha S. Zaghloul 3

4 The value of r is copied to radius.
2. EXAMPLE (1) – cont’d #include <stdio.h> // Function prototype double CircleArea(double radius); // FUNCTION PROTOTYPE int main (void) { double circle, r; printf (“Enter circle radius> “); scanf (“%f”, r); circle = CircleArea( r ); // FUNCTION CALL printf (“Area of circle = %f”, circle); return (0); } // end main // start define your functions double CircleArea (double radius 5.0 ) // FUNCTION HEADER AND DEFINITION double area; area = 3.14 * radius 5.0 * radius 5.0; return (area); // RETURN VALUE } // end CircleArea // end of program The value of r is copied to radius. Assume r = 5.0 Every word radius in the function will be replaced by the value of r, which is 5.0 area is then calculated. Its value is returned to the calling function (main). Dr. Soha S. Zaghloul 4

5 The value of r is copied to radius.
2. EXAMPLE (1) – cont’d #include <stdio.h> // Function prototype double CircleArea(double radius); // FUNCTION PROTOTYPE int main (void) { double circle, r; printf (“Enter circle radius> “); scanf (“%f”, r); circle = CircleArea( r ); // FUNCTION CALL printf (“Area of circle = %f”, circle); return (0); } // end main // start define your functions double CircleArea (double radius 5.0 ) // FUNCTION HEADER AND DEFINITION double area; area = 3.14 * radius 5.0 * radius 5.0; return (area); // RETURN VALUE } // end CircleArea // end of program Note that the calling function (main) does not recognize the variables of CircleArea, and vice versa. This includes the formal & actual parameters. The value of r is copied to radius. Assume r = 5.0 Every word radius in the function will be replaced by the value of r, which is 5.0 area is then calculated. Its value is returned to the calling function (main). Dr. Soha S. Zaghloul 5

6 3. Example 2 #include <stdio.h> // Function prototype
int power(int num1, int num2); // FUNCTION PROTOTYPE int main (void) { int result; result = power (2, 5); result = power (5, 2); return (0); } // end main // start define your functions int power( int num1, int num2) // formal parameters int i; int product = 1; for (i= 1; i< num2; i++) product *= num1; return product; } // end power // end of program Every word num1 in the function is replaced by 2 Every word num2 in the function is replaced by 5 Dr. Soha S. Zaghloul 6

7 3. Example 2 – cont’d #include <stdio.h> // Function prototype int power(int num1, int num2); // FUNCTION PROTOTYPE int main (void) { int result; result = power (2, 5); result = power (5, 2); return (0); } // end main // start define your functions int power( int num1 2, int num2 5) // formal parameters int i; int product = 1; for (i= 1; i< num2 5; i++) product *= num1 2; return product; } // end power // end of program Note that the calling function does not recognize the variables of power, and vice versa. This includes the formal & actual parameters. Every word num1 in the function is replaced by 2 Every word num2 in the function is replaced by 5 product is then calculated. Its value is returned to the calling function. Dr. Soha S. Zaghloul 7

8 3. Example 2 – cont’d #include <stdio.h> // Function prototype
int power(int num1, int num2); // FUNCTION PROTOTYPE int main (void) { int result; result = power (2, 5); result = power (5, 2); return (0); } // end main // start define your functions int power( int num1, int num2) // formal parameters int i; int product = 1; for (i= 1; i< num2; i++) product *= num1; return product; } // end power // end of program Every word num1 in the function is replaced by 5 Every word num2 in the function is replaced by 2 Dr. Soha S. Zaghloul 8

9 3. Example 2 – cont’d #include <stdio.h> // Function prototype int power(int num1, int num2); // FUNCTION PROTOTYPE int main (void) { int result; result = power (2, 5); result = power (5, 2); return (0); } // end main // start define your functions int power( int num1 5, int num2 2) // formal parameters int i; int product = 1; for (i= 1; i< num2 2; i++) product *= num1 5; return product; } // end power // end of program Note that the calling function does not recognize the variables of power, and vice versa. This includes the formal & actual parameters. Every word num1 in the function is replaced by 2 Every word num2 in the function is replaced by 5 product is then calculated. Its value is returned to the calling function. Dr. Soha S. Zaghloul 9

10 4. array elements as arguments
Array elements are passed as parameters to a function in the same way as simple parameters. Array elements are used as actual parameters (in the calling function). Therefore, they must have a value before calling the desired function. Array elements are NOT used as formal parameters in the function header. Dr. Soha S. Zaghloul 10

11 5. Example 3 #include <stdio.h>
int sumf (int num1, int num2); //prototype int main (void) { int sub, x[100]; int sum = 0; // get the values of the array for (sub = 1; sub <= 100; sub++) { printf (“Enter an integer> “); scanf (“%d”, &x[sub]); sum = sumf (sum, x[sub]); // sum = sum + x[sub] }// end for } //end main int sumf (int num1, int num2) { int total; total = num1 + num2; return (total); } //end sumf The value of sum is copied to num1. Assume it is 500. The value of x[sub] is copied to num2. Assume it is 150. Every word num1 in the function is replaced by the value of sum Every word num2 in the function is replaced by the value of x[sub] Dr. Soha S. Zaghloul 11

12 5. Example 3 – cont’d #include <stdio.h>
int sumf (int num1, int num2); //prototype int main (void) { int sub, x[100]; int sum = 0; // get the values of the array for (sub = 1; sub <= 100; sub++) { printf (“Enter an integer> “); scanf (“%d”, &x[sub]); sum = sumf (sum, x[sub]); // sum = sum + x[sub] }// end for } //end main int sumf (int num , int num ) { int total; total = num num ; return (total); } //end sumf The value of sum is copied to num1. Assume it is 500. The value of x[sub] is copied to num2. Assume it is 150. Every word num1 in the function is replaced by the value of sum Every word num2 in the function is replaced by the value of x[sub] total is then calculated. Its value is returned to the calling function. Dr. Soha S. Zaghloul 12

13 In order to pass an array to a function, we pass its address.
6. arrays as arguments In order to pass an array to a function, we pass its address. Therefore, any changes made to the array inside a function are recorded in the main memory. So, they are seen by the whole program. When we pass an array to a function, we also pass its size. The address of the array is passed to the function, and the value of the size is passed to the function. Dr. Soha S. Zaghloul 13

14 7. example 4 #include <stdio.h>
void initarray(int list[], int size, int n); //prototype int main(void) { int x[10], y[50]; int num; initarray (x, 10, 0); // init array x of size 10 with 0 printf (“Enter the initialization value>”); scanf (“%d”, &num); initarray (y, 50, num); // init array y of size 50 with num } // end main void initarray (int list[] , int size 10, int n 0) { int sub; for (sub = 0; sub < size 10; sub++) list[sub] sub = n 0; } // end initarray Since x is an array, then its address is passed. 10 & 0 are passed as values. Assume the address of x is 1024. Any changes made to list[sub] are performed on the address 1024+sub, which is the address of x. Dr. Soha S. Zaghloul 14

15 7. example 4 – cont’d #include <stdio.h>
void initarray(int list[], int size, int n); //prototype int main(void) { int x[10], y[50], i; int num; initarray (x, 10, 0); // init array x of size 10 with 0 for (i = 0; i < 10; i++) printf (“%d\n”, x[i]);//prints the column under Value } // end main void initarray (int list[] , int size 10, int n 0) { int sub; for (sub = 0; sub < size 10; sub++) list[sub] sub = n 0; } // end initarray Adrs Value Dr. Soha S. Zaghloul 15

16 7. example 4 – cont’d #include <stdio.h>
void initarray(int list[], int size, int n); //prototype int main(void) { int x[10], y[50]; int num; initarray (x, 10, 0); // init array x of size 10 with 0 printf (“Enter the initialization value>”); scanf (“%d”, &num); initarray (y, 50, num); // init array y of size 50 with num } // end main void initarray (int list[] , int size 50, int n 7) { int sub; for (sub = 0; sub < size 50; sub++) list[sub] sub = n 7; } // end initarray Since y is an array, then its address is passed. 50 & num are passed as values. Assume the address of y is 2000. Assume the value of num is 7 Any changes made to list[sub] are performed on the address 2000+sub, which is the address of y. Dr. Soha S. Zaghloul 16

17 7. example 4 – cont’d Adrs Value 2000 +0 7 2000 +1 2000 +2 2000 +3
#include <stdio.h> void initarray(int list[], int size, int n); //prototype int main(void) { int x[10], y[50], i; int num; initarray (x, 10, 0); // init array x of size 10 with 0 printf (“Enter the initialization value>”); scanf (“%d”, &num); initarray (y, 50, num); // init array y of size 50 with num for (i = 0; i < 50; i++) printf (“%d\n”, y[i]); // prints the Value column } // end main void initarray (int list[] , int size 50, int n 7) { int sub; for (sub = 0; sub < size 50; sub++) list[sub] sub = n 7; } // end initarray Adrs Value 7 ….. …. Dr. Soha S. Zaghloul 17

18 7. example 4 – cont’d Adrs Value 2000 +0 7 2000 +1 2000 +2 2000 +3
#include <stdio.h> void initarray(int list[], int size, int n); //prototype int main(void) { int x[10], y[50], i; int num; initarray (x, 10, 0); // init array x of size 10 with 0 printf (“Enter the initialization value>”); scanf (“%d”, &num); initarray (y, 50, num); // init array y of size 50 with num } // end main void initarray (int list[] , int size 50, int n 7) { int sub, i; for (sub = 0; sub < size 50; sub++) list[sub] sub = n 7; for (i = 0; i < 50; i++) printf (“%d\n”, list[i]); // prints the Value column } // end initarray Adrs Value 7 …… Dr. Soha S. Zaghloul 18

19 8. Example 5 #include <stdio.h> #define SIZE 100
int search(const double list[], int size, double element); //prototype int main (void) { double numbers[SIZE], num; int sub, index; //fill the array for (sub = 0; sub < SIZE; sub++) { printf (“Enter array element> “); scanf (“%f”, &numbers[sub]); } // end for printf (“Enter element you are searching for> “); scanf (“%f”, &num); index = search(numbers, SIZE, num); if (index == -1) printf (“Element not found\n”); else printf (“Element found at index = %d”, index); } // end main int search(const double list[], int size, double element) { int found = 0, i = 0; while (!found && i < size) { if (list[i] == element) found = 1; else i++; } // end while if (found) return i; else return -1; } // end search Dr. Soha S. Zaghloul 19

20 9. Example 6 #include <stdio.h> #define SIZE 100
double getmax (const double list[], int size); //prototype int main (void) { double numbers[SIZE], num; int sub; //fill the array for (sub = 0; sub < SIZE; sub++) { printf (“Enter array element> “); scanf (“%f”, &numbers[sub]); } // end for num = getmax(numbers, SIZE); printf (“The maximum element = %f”, num); } // end main double getmax(const double list[], int size) { double max; max = list[0]; for (sub = 0; sub < size; sub++) if (list[sub] > max) max = list[sub]; return max; } // end getmax Dr. Soha S. Zaghloul 20

21 10. Example 7 #include <stdio.h> #define SIZE 100
void add2arrays (const int arr1[], const int arr2[], int sum[], int size); //prototype int main (void) { int array1[SIZE], array2[SIZE], array3[SIZE]; int sub; //fill the arrays for (sub = 0; sub < SIZE; sub++) { printf (“Enter array1 element> “); scanf (“%d”, &array1[sub]); printf (“Enter array2 element> “); scanf (“%d”, &array2[sub]); } // end for add2arrays (array1, array2, array3, SIZE); printf (“The summation array is: \n”) printf (“%d \t”, array3[sub]); } // end main void add2arrays(const int arr1[], const int arr2[], int sum[], int size) { int sub; for (sub = 0; sub < size; sub++) sum[sub] = arr1[sub] + arr2[sub]; } // end add2arrays Dr. Soha S. Zaghloul 21


Download ppt "FUNCTIONS WITH ARGUMENTS"

Similar presentations


Ads by Google