8may

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Function

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

● The value of the actual parameters is copied into the formal


parameters.
● We can not modify the value of the actual parameter by the formal
parameter.
● Different memory is allocated for actual and formal parameters since
the value of the actual parameter is copied into the formal parameter.
● The actual parameter is the argument which is used in the function
call whereas formal parameter is the argument which is used in the
function definition.
Program1
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() { OUTPUT:
int x=100; Before function call x=100
printf("Before function call x=%d \n", x); Before adding value inside
change(x);//passing value in function function num=100
After adding value inside function
printf("After function call x=%d \n", x);
num=200
return 0; After function call x=100
}
Call by Address
● Call By Address is also known as Call By Pointers. In this method, the programmer
passes the addresses of the actual arguments to the formal parameters. Then, the
function uses the addresses to access the actual arguments.
● In other words, the changes made to the formal parameters affect the actual
arguments. To pass the value by pointer, the argument pointers are passed to the
functions similar to any other value.
● The call to the functions pass variable’s address to the called function. The actual
argument is not copied to the formal argument.
● The addresses of the actual arguments (parameters) are pass to the formal parameters.
Hence an operation performed by function on formal arguments affects actual
parameters.
Program2 int main(){

// user defined swap() int x, y;

#include <stdio.h> printf("Enter Value of x ");

// This function swaps values pointed by xp and yp scanf("%d", &x);

void swap(int *xp, int *yp){ printf("\nEnter Value of y ");

int temp = *xp; scanf("%d", &y);

*xp = *yp; swap(&x, &y);


printf("\nAfter Swapping: x = %d, y = %d", x, y);
*yp = temp;
return 0;
}
}
Output: Enter Value of x 12

Enter Value of y 14

After Swapping: x = 14, y = 12


Difference between Call by Value and Address
Call by Value Call by Address
● When a function is called, the ● Both actual and formal parameters
value of the actual parameters is
indirectly share the same variable.
copied into formal parameters.
● ● Pointer variables are used as
Both the actual and formal
parameters have their own formal parameters.
copies of values, therefore any ● The formal pointer variable holds
change in one of the types of the address of the actual
parameters will not be reflected parameter, hence changes done by
by the other. the formal parameter reflected in
the actual parameter.
Passing Array to Function in C
Passing Array to Function in C

● 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

function_name(arrayname); //passing array

Methods to declare a function that receives an array as an argument


● There are 3 ways to declare the function which is intended to receive an array as an
argument.
○ return_type function(type arrayname[])

(Declaring blank subscript notation [] is the widely used technique.)

○ return_type function(type arrayname[SIZE])

(Optionally, we can define size in subscript notation [])

○ return_type function(type *arrayname)

(You can also use the concept of a pointer.)


C language passing an array to function example

#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* Bubble_Sort(int[]); { int i, j,temp;

void main () { for(i = 0; i<10; i++) {

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;
}

/* call factorial function*/


int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
Assignment
● WAP to find factorial of a number with input from user.
● WAP to print Fibonacci series using recursion.
● WAP to write a function which accepts integer arrays as arguments and finds the average of the
array and returns the average value.
● WAP to develop a calculator for following
○ Sum
○ Difference
○ Product
○ Division

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

Output: a=3, b=1, c=2


● WAP in C to where a positive integer is entered through the keyboard. Write a
function to obtain the prime factors of this number. (For example, prime
factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.)

You might also like