0% found this document useful (0 votes)
104 views18 pages

Function & Contrast The Use of Call by Value & Call by Reference

This document discusses and provides examples of call by value and call by reference parameter passing mechanisms in C functions. It defines functions, parameters, and how values are passed differently between call by value and call by reference. In call by value, a copy of the actual parameter is passed, so changes made inside the function are not reflected outside. In call by reference, the address of the actual parameter is passed, so any changes made inside the function affect the argument externally. Examples demonstrate how call by value keeps original argument values unchanged, while call by reference swaps the values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views18 pages

Function & Contrast The Use of Call by Value & Call by Reference

This document discusses and provides examples of call by value and call by reference parameter passing mechanisms in C functions. It defines functions, parameters, and how values are passed differently between call by value and call by reference. In call by value, a copy of the actual parameter is passed, so changes made inside the function are not reflected outside. In call by reference, the address of the actual parameter is passed, so any changes made inside the function affect the argument externally. Examples demonstrate how call by value keeps original argument values unchanged, while call by reference swaps the values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Function & Contrast the use of call

by value & call by reference

1
Functions
 A function is a group of statements that together perform a task. Every C program has
at least one function, which is main().
 Function are of two types: Library and User Define Function.
 Library Function: are not required to be written by us.

Example: printf () , scanf()


 User Define Function: A function is a block of code that performs a specific
task. allows you to define functions according to your need. These functions are
known as user-defined functions. For example: Suppose, you need to create a circle
and color it depending upon the radius and color.

2
The Three components associated with
Functions
 Declaration
 Function Definition
 Calling Statement
 Function Declaration: A function declaration tells the compiler about a function name
and how to call the function. The actual body of the function can be defined separately.
 A function declaration has the following parts −
 return_type function_name( parameter list );
Example: int max(int num1, int num2);

Parameter names are not important in function declaration only their type is required, so the
following is also a valid declaration −
Example: int max(int, int);
3
Function Definition
The general form of a function definition in C programming language is as follows −
 return_type function_name( parameter list ) {

body of the function }


 Function-name: any valid identifier
 Return-value-type: data type of the result (default int)
 void – indicates that the function returns nothing
 Parameter-list: comma separated list, declares parameters
 A type must be listed explicitly for each parameter unless, the parameter is
of type int

4
Example of Function Definition

/* function returning the max between two numbers */

int max(int num1, int num2)

int result;

if (num1 > num2)

result = num1;

else

result = num2;

return (result);

}
5
Calling of a Function
#include <stdio.h>
#include <conio.h>
int max(int num1, int num2); /* function declaration */
void main () {
int a = 100, b = 200, ret; /* local variable definition */
ret = max(a, b); /* calling a function to get max value */
printf( "Max value is : %d\n", ret );
getch();
}
int max(int num1, int num2) { /* function returning the max between two numbers */
int result; /* local variable declaration */
if (num1 > num2)
result = num1; Output:
else Max value is : 200
result = num2;
return(result); }
6
Types of User Defined Functions
 For better understanding of arguments and return value from the function, user-defined
functions can be categorized as:
 Function with no arguments and no return value
 Function with no arguments and a return value
 Function with arguments and no return value
 Function with arguments and a return value.
 From the above, 1 and 3 types does not return any value when the function is called so,
We use void return type while defining the function.
 When we call the function 2 and 4 types will return some value so, We have to use the
appropriate data type (int, float, double etc) as return type while defining the function.
We use return keyword inside the function to return some value. 7
Example-1: Function with no
arguments and no return value
#include <stdio.h>
#include <conio.h>
void Addition();     /* function declaration */
void main ()
{
Addition();  /* calling a function */
getch();
}
Output:
void Addition()
Sum of a and b is = 30
{
  int Sum, a = 10, b = 20;  
  Sum = a + b;
   printf("\n Sum of a and b is = %d", Sum);
}

8
Example-2: Function with no
arguments and with return value
#include <stdio.h>
#include <conio.h>
int Multiplication();     /* function declaration */
void main ()
{
int Multi;
Multi = Multiplication(); /* calling a function */
 printf("\n Multiplication of a and b is = %d \n", Multi );  
getch();
}
int Multiplication()
Output:
{
Multiplication of a and b is = 800
int Multi, a = 20, b = 40;  
Multi = a * b; 
return(Multi); /* return a value*/
}
9
Example-3: Function with arguments
and no return value
#include <stdio.h>
#include <conio.h>
void Addition(int, int);        /* function declaration */
void main () {
int a, b; 
printf("\nEnter two integer values :\n");
scanf("%d %d", &a, &b); 
Addition(a, b);  //Calling the function
getch();
} Output:
void Addition(int a, int b) Enter two integer values: 5 10
{ Addition of 5 and 10 is = 15
  int Sum;   
  Sum = a + b;
  printf("\n Addition of %d and %d is = %d \n", a, b, Sum);
}
10
Example-4: Function with arguments
and with return value
#include <stdio.h>
#include <conio.h>
int Addition(int, int);        /* function declaration */
void main () {
int a, b, Sum; 
printf("\nEnter two integer values :\n");
scanf("%d %d", &a, &b); 
Sum=Addition(a, b);  //Calling the function
 printf("\n Addition of %d and %d is = %d \n", a, b, Sum);
getch();
}
int Addition(int a, int b) Output:
{ Enter two integer values: 5 10
  int Sum;    Addition of 5 and 10 is = 15
  Sum = a + b;
return(Sum); }
11
Actual and Formal Parameters

12
Parameter Passing Mechanism
(call by value and call by reference )
 There are two ways to pass arguments/parameters to function calls -- call by
value and call by reference.
 The major difference between call by value and call by reference is that in call by
value a copy of actual arguments is passed to respective formal arguments.
 While, in call by reference the location (address) of actual arguments is passed to
formal arguments, hence any change made to formal arguments will also reflect in
actual arguments.

13
Difference between call by value and
call by reference

14
Contd…

15
Example using Call by Value
#include <stdio.h>
#include <conio.h>
void swapByValue(int, int);        /* function declaration */
void main ()
{
int n1 = 10, n2 = 20;  
swapByValue(n1, n2); /* actual arguments will be as it is */
printf("n1= %d, n2= %d\n", n1, n2);
getch();
}
void swapByValue(int a, int b)
{ Output:
a = a+b; n1= 10, n2= 20
b= a-b;
a=a-b;
}
16
Example using Call by Reference
#include <stdio.h>
#include <conio.h>
void swapByReference(int*, int*);        /* function declaration */
void main ()
{
int n1 = 10, n2 = 20;  
swapByReference(&n1, &n2); /* actual arguments will be altered */
printf("n1= %d, n2= %d\n", n1, n2);
getch();
}
void swapByReference(int *a, int *b)
{ Output:
*a = *a+*b; n1= 20, n2= 10
*b= *a-*b;
*a=*a-*b;

17
THANK YOU!!!

18

You might also like