8may
8may
8may
By
Mr. Balaram Pal
Dept. of CSE, NIT Rourkela
Today’s Outline
● Call by Value
● Call by Address
● Passing array to Function in C
● Recursion
Call by Value
Enter Value of y 14
● There are various general problems which requires passing more than one
variable of the same type to a function.
● For example, consider a function which sorts the 10 elements in ascending order.
Such a function requires 10 numbers to be passed as the actual parameters from
the main function. Here, instead of declaring 10 different numbers and then
passing into the function, we can declare and initialize an array and pass that into
the function.
● The array_name contains the address of the first element. Here, we must notice
that we need to pass only the name of the array in the function.
● The array defined as the formal parameter will automatically refer to the array
specified by the array name defined as an actual parameter.
Syntax to pass an array to the function
#include<stdio.h>
int main(){
int minarray(int arr[],int size){
int i=0,min=0;
int min=arr[0];
int numbers[]={4,5,7,3,8,9}; //declaration of array
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){ min=minarray(numbers,6); //passing array with size
min=arr[i];
printf("minimum number is %d \n",min);
}
return 0; }
} //end of for
return min; OUTPUT:
minimum number is 3
} //end of function
C function to sort the array
void Bubble_Sort(int a[]) //array a[] points to arr.
#include<stdio.h> { int i, j,temp;
void Bubble_Sort(int[]); for(i = 0; i<10; i++) {
void main () { for(j = i+1; j<10; j++) {
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; if(a[j] < a[i]) {
temp = a[i];
Bubble_Sort(arr);
a[i] = a[j];
}
OUTPUT: a[j] = temp;
Printing Sorted Element List ...
7 } } }
9
10
12 printf("Printing Sorted Element List ...\n");
23
23 for(i = 0; i<10; i++) {
34
44 printf("%d\n",a[i]);
78
101
} }
Returning array from the function
● A function can not return more than one value. However, if we return
statement as return a, b, c; to return three values (a,b,c), the function will
return the last mentioned value which is c.
● In some problems, we may need to return multiple values from a function. In
such cases, an array is returned from the function.
● Returning an array is similar to passing the array into the function. The name
of the array is returned from the function.
● To make a function returning an array, the following syntax is used.
int * Function_name() {
//some statements;
return array_type; }
Program
#include<stdio.h> int* Bubble_Sort(int a[]) //array a[] points to arr.
int arr[10] = { 10, 9, 7, 101, 23, 44, for(j = i+1; j<10; j++) {
12, 78, 34, 23}; if(a[j] < a[i]) {
int *p = Bubble_Sort(arr), i; temp = a[i]; Output
Printing Sorted Element List
printf("printing sorted elements ...\ a[i] = a[j]; 7
n"); 9
a[j] = temp; 10
for(i=0;i<10;i++) { 12
}}} 23
printf("%d\n",*(p+i)); 23
return a; 34
} } 44
} 78
101
Pass Multidimensional Arrays to a Function
To pass multidimensional arrays to a function, only the name of the array is passed to the function
(similar to one-dimensional arrays).
#include <stdio.h>
void displayNumbers(int num[2][2]) {
void displayNumbers(int num[2][2]);
printf("Displaying:\n");
int main() {
for (int i = 0; i < 2; ++i) {
int num[2][2];
for (int j = 0; j < 2; ++j) {
printf("Enter 4 numbers:\n");
printf("%d\n", num[i][j]);
for (int i = 0; i < 2; ++i) {
}
for (int j = 0; j < 2; ++j) { OUTPUT: Enter 4 numbers:
}
2
scanf("%d", &num[i][j]);
} 3
} } 4
5
// pass multi-dimensional array to a function Displaying:
2
displayNumbers(num); 3
4
return 0; } 5
Recursion
Recursion
● Recursion is a process in which function calls itself
● For recursion
○ We should be able to define a problem in terms of similar problem
○ There should be a termination condition to stop recursion
NOTE: Recursive function must have at least one terminating condition that can
be satisfied. Otherwise, the recursive function will call itself repeatedly.
A very simple example is
main(){
printf(“recursion through main”);
main();
}
Program for recursion
/* compute factorial */
void main() OUTPUT
{
int number; Enter a number: 4
int fact; Factorial of 4 is 24
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %d\n", number, fact);
return 0;
}
The program should display a menu for taking choice from the user. All operations must be in form of
functions
● WAP in C to perform cyclic swap of three numbers using pass by reference.
Input : a=1, b=2, c =3