100 likes | 194 Views
Functions. Dr. Sajib Datta CSE@UTA Sep 24, 2014. Multiple value return. In your function, the key word “return” can only return a single value, NOT multiple values. But you can define an array in main function body, and pass it to your function.
E N D
Functions Dr. SajibDatta CSE@UTA Sep 24, 2014
Multiple value return • In your function, the key word “return” can only return a single value, NOT multiple values. • But you can define an array in main function body, and pass it to your function. • In your function, we may store multiple values in that array, which will be visible in the main function. So it is sort of returning multiple values. • See the example in the next slide.
A function to return the min and the max of an array of integers • #include <stdio.h> • void minmax(intmyarr[], intmyminmax [], int s); • intmain(void) • { • intarr[] ={42, 2, 443, 23, 12}, minmaxarr[2], size; • size = sizeof(arr)/sizeof(int); • minmax(arr, minmaxarr, size); • printf("The array has a minimum value of %d, and a maximum value of %d.\n", minmaxarr[0], minmaxarr[1]); • return 0; • } • void minmax(intmyarr[], intmyminmax [], int s) • { • inti; • myminmax[0] = myarr[0];// myminmax[0] for the minimum value • myminmax[1] = myarr[0];// myminmax[1] for the maximum value • for(i = 1; i < s; i ++) • { • if (myminmax[0] > myarr[i]) • { • myminmax[0] = myarr[i]; • } • else if (myminmax[1] < myarr[i]) • { • myminmax[1] = myarr[i]; • } • } • }
Recursion • Recursive Function Definition • Recursive function is a function that contains a call to itself. • Why Recursive Function • Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily. • Note on Using Recursive Function • Recursive function must have at least one exit condition that can be satisfied. • Otherwise, the recursive function will call itself repeatedly into a dead loop until the runtime stack overflows.
Recursion • # include<stdio.h> • intsum(int number) • { • if(number <= 1) • return 1; • return number + sum(number - 1); • } • void main() • { • int x = 5; • printf("The sum of all positive integers (<= %d) is %d.\n", x, sum(x)); • return 0; • } • How to implement factorial?
Fibonacci Sequence • Define a recursive function to implement FS Given n, print out the first n integers in FS • 0,1,1,2,3,5,8,13,21,34,55…..(1st 10 numbers)
#include <stdio.h> • intfibonacci(int n); • int main(void) • { • int input, i; • printf("Please input a positive integer value:"); • scanf("%d", &input); • for(i = 0; i <= input; i++) • printf("%d ", fibonacci(i)); • } • intfibonacci(int n) • { • if(n < 2) • return n; • else • return fibonacci(n-1)+fibonacci(n-2); • }
#include <stdio.h> • #define ARRSIZE 7 • intmain(void) • { • intintarr[ARRSIZE], target, i, left, right, mid; • printf("Please input %d integers which are sorted in the ascending order and a target value.", ARRSIZE); • scanf("%d", &target); • for(i = 0; i<ARRSIZE; i++) • { • scanf("%d", &intarr[i]); • } • left = 0; • right = ARRSIZE-1; • while(left <= right) • { • mid = (left+right)/2; • if(intarr[mid] == target) • { • printf("The index of the target in the array is %d.\n", mid); • break; • } • else if (target > intarr[mid]) • { • left = mid + 1; • } • else • { • right = mid - 1; • } • } • if(left > right) • printf("The target is not in the array.\n"); • return 0; • }
Binary Search- Recursive • #include<stdio.h> • #define ARR_SIZE 10 • void binarySearch(intarr[], int left, int right, int t); • intmain(void) • { • intintarr[ARR_SIZE] = {-12, -3, 0, 5, 11, 15, 27, 33, 49, 59}, target; • puts("Please input a target value:"); • scanf("%d", &target); • binarySearch(intarr, 0, ARR_SIZE-1, target); • return 0; • } • void binarySearch(intarr[], int left, int right, int t) • { • int mid; • printf("The current range is %d, %d.\n", left, right); • if(left<=right) • { • mid = (left+right)/2; • if(arr[mid] == t) • printf("The index of the target is %d.\n.", mid); • else if(arr[mid] > t) • binarySearch(arr, left, mid-1, t); • else • binarySearch(arr, mid+1, right, t); • } • else • printf("The target is not found."); • }