FUNCTIONS WITH ARGUMENTS

Slides:



Advertisements
Similar presentations
Kernighan/Ritchie: Kelley/Pohl:
Advertisements

CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Nested LOOPS.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
ARRAYS.
User-Written Functions
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Hassan Khosravi / Geoffrey Tien
C Programming Tutorial – Part I
Functions Dr. Sajib Datta
Functions Separate Compilation
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Arrays and Records.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
C Passing arrays to a Function
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Local Variables variables which are declared within a
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Functions I Creating a programming with small logical units of code.
Pointers  Week 10.
Dale Roberts, Lecturer IUPUI
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Functions.
EKT150 : Computer Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
1) C program development 2) Selection structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pointers Call-by-Reference CSCI 230
Lec 14 Oct 23, 02.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
A function with one argument
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions with arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Relational, Logical, and Equality Operators
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays I Handling lists of data.
Functions, Part 1 of 3 Topics Using Predefined Functions
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
CS150 Introduction to Computer Science 1
1-6 Midterm Review.
EECE.2160 ECE Application Programming
Functions Extra Examples.
Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Predefined Functions Revisited
Programming in C Pointers and Arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions I Creating a programming with small logical units of code.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 42 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

FUNCTIONS WITH ARGUMENTS REVISITED This lecture is a revision on functions

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

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

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

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

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

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

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

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

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

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

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 num1 500, int num2 150) { int total; total = num1 500 + num2 150; 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

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

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[] 1024, int size 10, int n 0) { int sub; for (sub = 0; sub < size 10; sub++) list[sub] 1024 + 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

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[] 1024, int size 10, int n 0) { int sub; for (sub = 0; sub < size 10; sub++) list[sub] 1024 + sub = n 0; } // end initarray Adrs Value 1024 +0 1024 +1 1024 +2 1024 +3 1024 +4 1024 +5 1024 +6 1024 +7 1024 +8 1024 +9 Dr. Soha S. Zaghloul 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]; 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[] 2000, int size 50, int n 7) { int sub; for (sub = 0; sub < size 50; sub++) list[sub] 2000 + 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

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[] 2000, int size 50, int n 7) { int sub; for (sub = 0; sub < size 50; sub++) list[sub] 2000 + sub = n 7; } // end initarray Adrs Value 2000 +0 7 2000 +1 2000 +2 2000 +3 2000 +4 2000 +5 2000 +6 2000 +7 2000 +8 2000 +9 ….. …. Dr. Soha S. Zaghloul 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 } // end main void initarray (int list[] 2000, int size 50, int n 7) { int sub, i; for (sub = 0; sub < size 50; sub++) list[sub] 2000 + sub = n 7; for (i = 0; i < 50; i++) printf (“%d\n”, list[i]); // prints the Value column } // end initarray Adrs Value 2000 +0 7 2000 +1 2000 +2 2000 +3 2000 +4 2000 +5 2000 +6 2000 +7 2000 +8 2000 +9 …… Dr. Soha S. Zaghloul 18

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

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

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