0% found this document useful (0 votes)
86 views

Function Arguments: Actual Arguments Formal Arguments

Formal parameters are variables used in a function definition that specify the type of data passed into the function. Actual parameters are the specific values or expressions passed into a function call. There are two ways to pass data to functions in C - call by value and call by reference. Call by value passes the value directly, while call by reference passes the address of the variable, allowing the function to modify the original variable. Variables can also have local or global scope depending on where they are defined.

Uploaded by

sonali gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Function Arguments: Actual Arguments Formal Arguments

Formal parameters are variables used in a function definition that specify the type of data passed into the function. Actual parameters are the specific values or expressions passed into a function call. There are two ways to pass data to functions in C - call by value and call by reference. Call by value passes the value directly, while call by reference passes the address of the variable, allowing the function to modify the original variable. Variables can also have local or global scope depending on where they are defined.

Uploaded by

sonali gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Function arguments

Basically, there are two types of arguments:

 Actual arguments
 Formal arguments

1. Actual Parameters:
The arguments that are passed in a function call are called actual arguments.
These arguments are defined in the calling function. These are the variables or
expressions referenced in the parameter list of a subprogram call. There is no need
to specify datatype in actual parameter.

2. Formal Parameters:
These are the variables or expressions referenced in the parameter list of a
subprogram specification. The datatype of the receiving value must be defined. The
scope of formal arguments is local to the function definition in which they are used.

Difference between Actual and Formal Parameters:

Actual Parameters Formal Parameters


When a function is called, the values The parameter used in function
(expressions) that are passed in the definition statement which contain data
function call are called the arguments type on its time of declaration is called
or actual parameters. formal parameter.

These are the variables or expressions These are the variables or expressions
referenced in the parameter list of a referenced in the parameter list of a
subprogram call. subprogram specification.

Actual Parameters are the parameters Formal Parameters are the parameters
which are in calling subprogram. which are in called subprogram.

There is no need to specify datatype in The datatype of the receiving value


actual parameter. must be defined.

The parameters are written in function The parameters are written in function
call are known as actual parameters. definition are known as formal
parameters.

Actual Parameters can be constant Formal Parameters can be treated as


values or variable names. local variables of a function in which
they are used in the function header.

Call by Value and Call by Reference in C

On the basis of arguments there are two types of function are available in C language,
they are;
 With argument

 Without argument

If a function take any arguments, it must declare variables that accept the values
as a arguments. These variables are called the formal parameters of the function.
There are two ways to pass value or data to function in C language which is given
below;

 call by value

 call by reference
Call by value

In call by value, original value cannot be changed or modified. In call by value,


when you passed value to the function it is locally stored by the function parameter
in stack memory location. If you change the value of function parameter, it is
changed for the current function only but it not change the value of variable inside
the caller method such as main().
Example:
#include<stdio.h>
#include<conio.h>
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}

void main()
{
int a=100, b=200;
clrscr();
swap(a, b); // passing value to function
printf("\nValue of a: %d",a);
printf("\nValue of b: %d",b);
getch();
}
Output:
Value of a: 200
Value of b: 100

Call by reference

In call by reference, original value is changed or modified because we pass


reference (address). Here, address of the value is passed in the function, so actual
and formal arguments shares the same address space. Hence, any value changed
inside the function, is reflected inside as well as outside the function.
Example:
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int a=100, b=200;
clrscr();
swap(&a, &b); // passing value to function
printf("\nValue of a: %d",a);
printf("\nValue of b: %d",b);
getch();
}
Output
Value of a: 200
Value of b: 100

Difference between call by value and call by reference.

call by value call by reference

This method copy original value into This method copy address of arguments
function as a arguments. into function as a arguments.

Changes made to the parameter inside Changes made to the parameter affect
the function have no effect on the the argument. Because address is used
argument. to access the actual argument.

Actual and formal arguments will be Actual and formal arguments will be
created in different memory location created in same memory location

Note: By default, C uses call by value to pass arguments.


Scope Rules
A scope in any programming is a region of the program where a defined variable can
have its existence and beyond that variable it cannot be accessed. There are three
places where variables can be declared in C programming language −
 Inside a function or a block which is called local variables.
 Outside of all functions which is called global variables.
 In the definition of function parameters which are called formal parameters.
Let us understand what are local and global variables, and formal parameters.

Local Variables

Variables that are declared inside a function or block are called local variables. They
can be used only by statements that are inside that function or block of code. Local
variables are not known to functions outside their own. The following example shows
how local variables are used. Here all the variables a, b, and c are local to main()
function.
#include <stdio.h>

int main () {

/* local variable declaration */


int a, b;
int c;

/* actual initialization */
a = 10;
b = 20;
c = a + b;

printf ("value of a = %d, b = %d and c = %d\n", a, b, c);

return 0;
}

Global Variables

Global variables are defined outside a function, usually on top of the program. Global
variables hold their values throughout the lifetime of your program and they can be
accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration. The following
program show how global variables are used in a program.
#include <stdio.h>

/* global variable declaration */


int g;

int main () {

/* local variable declaration */


int a, b;

/* actual initialization */
a = 10;
b = 20;
g = a + b;

printf ("value of a = %d, b = %d and g = %d\n", a, b, g);

return 0;
}
A program can have same name for local and global variables but the value of local
variable inside a function will take preference. Here is an example −
Live Demo

#include <stdio.h>

/* global variable declaration */


int g = 20;

int main () {

/* local variable declaration */


int g = 10;

printf ("value of g = %d\n", g);

return 0;
}
When the above code is compiled and executed, it produces the following result −
value of g = 10

Formal Parameters

Formal parameters, are treated as local variables with-in a function and they take
precedence over global variables. Following is an example −
#include <stdio.h>

/* global variable declaration */


int a = 20;

int main () {

/* local variable declaration in main function */


int a = 10;
int b = 20;
int c = 0;

printf ("value of a in main() = %d\n", a);


c = sum( a, b);
printf ("value of c in main() = %d\n", c);
return 0;
}

/* function to add two integers */


int sum(int a, int b) {

printf ("value of a in sum() = %d\n", a);


printf ("value of b in sum() = %d\n", b);

return a + b;
}
When the above code is compiled and executed, it produces the following result −
value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30

Initializing Local and Global Variables

When a local variable is defined, it is not initialized by the system, you must initialize
it yourself. Global variables are initialized automatically by the system when you
define them as follows −

Data Type Initial Default Value

int 0

char '\0'

float 0

double 0

pointer NULL

It is a good programming practice to initialize variables properly, otherwise your


program may produce unexpected results, because uninitialized variables will take
some garbage value already available at their memory location.

You might also like