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

Parameter Passing Techniques

There are two parameter passing techniques in functions: call by value and call by reference. Call by value passes the value of the argument to the function, so any changes to the parameter are not reflected in the original argument. Call by reference passes the reference (address) of the argument, so changes to the parameter are reflected in the original argument. Pointers allow call by reference by passing the address of an argument variable to the function parameter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
273 views5 pages

Parameter Passing Techniques

There are two parameter passing techniques in functions: call by value and call by reference. Call by value passes the value of the argument to the function, so any changes to the parameter are not reflected in the original argument. Call by reference passes the reference (address) of the argument, so changes to the parameter are reflected in the original argument. Pointers allow call by reference by passing the address of an argument variable to the function parameter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Important points to be noted while calling a function:

• Parentheses”( )” are compulsory after the function name.


• The function name in the function call and the function definition must be
same.
• The type, number, and sequence of actual and formal arguments must be
same.
• A semicolon must be used at the end of the statement when a function is
called.
• The number of arguments should be equal to the number of parameters.
• There must be one-to-one mapping between arguments and parameters. i.e.
they should be in the same order and should have same data type.
• Same variables can be used as arguments and parameters.
• The data types of the parameters must match or be closely compatible. It is
very risky to call a function using an actual parameter that is floating point
data if the formal parameter was declared as an integer data type. You can
pass a float to a double, but should not do the opposite. You can also pass a
short int to an int, etc. But you should not pass any type of integer to any
type of floating point data or do the opposite.

Parameter Passing Techniques:

NOTE: The changes made in the function are not transmitted back to the main
function . The moment the function definition encounters a ‘}’ the values created
in the function block are discarded , if the function consists of any return value it
should be written before ‘}’ , this return statement returns a value to the function in
main program.

Parameters are the values which are passed to the function .These can be passed in
two ways depending on the nature of the value sent.

1) Call by value(pass by value)


2) Call by reference (pass by reference)

Call(Pass) By Value : Changes made to formal parameter do not get transmitted


back to the caller. Any modifications to the formal parameter variable inside the
called function or method affect only the separate storage location and will not be
reflected in the actual parameter in the calling environment. This method is also
called as call by value.

#include<stdio.h>
void swapnum(int i, int j);

int main()
{
int a = 10, b = 20;

// passing parameters
swapnum(a, b);//func call

printf("a is %d and b is %d\n", a, b);


return 0;
}
void swapnum(int i, int j)
{
int temp = i;
i = j;
j = temp;
}

Output:

a is 10 and b is 20

Call(Pass) by reference(aliasing) : Changes made to formal parameter do get


transmitted back to the caller through parameter passing. Any changes to the
formal parameter are reflected in the actual parameter in the calling environment as
formal parameter receives a reference (or pointer) to the actual data. This method is
also called as call by reference. This method is efficient in both time and space.

Pointer: A pointer is a variable which stores the address of another variable.

Syntax

int var=10;

int *ptr;//where ptr is the pointer vaiable

*ptr=&var//ptr is a variable which stores the address of var

*ptr=10//represents value at address 2008

‘&’ represents the address and” &var” represents address of var

‘*’ represents the value at a address and “*ptr” represents the value at
address ptr

var=10

*ptr=10(represents value at address 2008)


Variable name Var ptr

value 10 2008

address 2008 6666

#include<stdio.h>
void swapnum(int* i, int * j);

int main(void)
{
int a = 10, b = 20;

// passing parameters
swapnum(&a, &b);

printf("a is %d and b is %d\n", a, b);


return 0;
}
void swapnum(int* i, int* j)
{
int temp = *i;
*i = *j;
*j = temp;
}

Output:

a is 20 and b is 10

Call by Call by reference


value
int a; int a;

Formal parameter is ‘a’


Formal parameter ‘a’ is a local
local reference.
variable.

It cannot change the actual It can change the actual


parameter. parameter.
Actual parameter may be a
Actual parameter must be a
constant, a variable, or an
variable.
expression.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy