0% found this document useful (0 votes)
11 views5 pages

Parameter Passing in C

The document explains parameter passing in C, detailing actual and formal parameters, and the methods of passing parameters: Call by Value and Call by Reference. It also covers the scope of variables, distinguishing between global, local, and formal parameters, along with examples for each concept. The document illustrates how variable scope affects accessibility within functions and the impact of different parameter passing methods on variable values.

Uploaded by

pevahcomputers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Parameter Passing in C

The document explains parameter passing in C, detailing actual and formal parameters, and the methods of passing parameters: Call by Value and Call by Reference. It also covers the scope of variables, distinguishing between global, local, and formal parameters, along with examples for each concept. The document illustrates how variable scope affects accessibility within functions and the impact of different parameter passing methods on variable values.

Uploaded by

pevahcomputers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Parameter Passing in C

When a function gets executed in the program, the execution control


is transferred from calling function to called function and executes
function definition, and finally comes back to the calling function.
When the execution control is transferred from calling function to
called function it may carry one or more number of data values.
These data values are called as parameters.

Parameters are the data values that are passed from calling function
to called function.

there are two types of parameters:

 Actual Parameters
 Formal Parameters

The actual parameters are the parameters that are specified in


calling function. The formal parameters are the parameters that are
declared at called function. When a function gets executed, the copy
of actual parameter values are copied into formal parameters.

There are two methods to pass parameters from calling function to


called function:

 Call by Value
 Call by Reference

Call by Value

In call by value parameter passing method, the copy of actual


parameter values are copied to formal parameters and these formal
parameters are used in called function. The changes made on the
formal parameters does not affect the values of actual parameters.
That means, after the execution control comes back to the calling
function, the actual parameter values remains same.

For example consider the following program.

#include <stdio.h>
int main(){
int num1, num2 ;
void swap(int,int) ; // function declaration

num1 = 10 ;
num2 = 20 ;

printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;

swap(num1, num2) ; // calling function

printf("\nAfter swap: num1 = %d, num2 = %d\n", num1, num2);


}
void swap(int a, int b) // called function
{
int temp ;
temp = a ;
a = b ;
b = temp ;
}

In the above example program, the variables num1 and num2 are called
actual parameters and the variables a and b are called formal
parameters. The value of num1 is copied into a and the value of num2
is copied into b. The changes made on variables a and b does not
effect the values of num1 and num2.

Call by Reference

In Call by Reference parameter passing method, the memory location


address of the actual parameters is copied to formal parameters.
This address is used to access the memory locations of the actual
parameters in called function. In this method of parameter passing,
the formal parameters must be pointer variables.

That means in call by reference parameter passing method, the


address of the actual parameters is passed to the called function
and is received by the formal parameters (pointers). Whenever we use
these formal parameters in called function, they directly access the
memory locations of actual parameters. So the changes made on the
formal parameters effects the values of actual parameters.

For example consider the following program.

#include <stdio.h>

int main(){
int num1, num2 ;
void swap(int *,int *) ; // function declaration

num1 = 10 ;
num2 = 20 ;

printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;


swap(&num1, &num2) ; // calling function

printf("\nAfter swap: num1 = %d, num2 = %d", num1, num2);

}
void swap(int *a, int *b) // called function
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}

In the above example program, the addresses of variables num1 and


num2 are copied to pointer variables a and b. The changes made on
the pointer variables a and b in called function effects the values
of actual parameters num1 and num2 in calling function.

Scope of Variable in C

Scope of a variable is the portion of the program where a defined


variable can be accessed.

The variable scope defines the visibility of variable in the


program. Scope of a variable depends on the position of variable
declaration.

In C programming language, a variable can be declared in three


different positions.

 Before the function definition (Global Declaration)


 Inside the function or block (Local Declaration)
 In the function definition parameters (Formal Parameters)

Before the function definition (Global Declaration)

This is a variable declared before the function definition (outside


the function definition). That global variable can be accessed by
all the functions that are defined after the global declaration.
That means the global variable can be accessed anywhere in the
program after its declaration.

#include <stdio.h>

int num1, num2 ;


int main(){
void addition() ;
void subtraction() ;
void multiplication() ;

num1 = 10 ;//global variable


num2 = 20 ; global variable
printf("num1 = %d, num2 = %d", num1, num2) ;
addition() ;
subtraction() ;
multiplication() ;

}
void addition()
{
int result ;
result = num1 + num2 ;
printf("\naddition = %d", result) ;
}
void subtraction()
{
int result ;
result = num1 - num2 ;
printf("\nsubtraction = %d", result) ;
}
void multiplication()
{
int result ;
result = num1 * num2 ;
printf("\nmultiplication = %d", result) ;
}

In the above example program, the variables num1 and num2 are
declared as global variables. They are declared before the main()
function. So, they can be accessed by function main() and other
functions that are defined after main(). In the above example, the
functions main(), addition(), subtraction() and multiplication() can
access the variables num1 and num2.

Inside the function or block (Local Declaration)

A global variable is a variable declared inside the function or


block. The local variable can be accessed only by the function or
block in which it is declared. That means the local variable can be
accessed only inside the function or block in which it is declared.

#include <stdio.h>

int main(){
void addition() ;

addition() ;

}
void addition()
{
int num1, num2 ;//local variables
int sumResult ;
num1 = 10 ;
num2 = 20 ;
printf("num1 = %d, num2 = %d", num1, num2) ;
sumResult = num1 + num2 ;
printf("\naddition = %d", sumResult) ;
}

In the function definition parameters (Formal Parameters)

The variables declared in function definition as parameters have


local variable scope. These variables behave like local variables in
the function. They can be accessed inside the function but not
outside the function.

#include <stdio.h>

int main(){
void addition(int, int) ;
int num1, num2 ;

num1 = 10 ;
num2 = 20 ;
addition(num1, num2) ;

}
void addition(int a, int b)
{
int sumResult ;
sumResult = a + b ;
printf("\naddition = %d", sumResult) ;
}

You might also like