C - ARGUMENT PASSING
ARGUMENT PASSING
CALL BY VALUE
Functions in C pass all arguments by value. It
means the contents of the arguments in the calling functions are not changed,
even if they are changed in the called function. The contents of the variable
are copied to the formal parameters of the function definition, thus preserving
the contents of the argument in the calling function.
EXAMPLE
main()
{
int num = 100;
void modify(int);
printf(“In main, the value of num is %d \n”,
num);
modify(num);
printf(“Back in main, the value of num is %d
\n”, num);
}
void modify(int n)
{
printf(“In function value of num is %d \n”,
n);
n = 200;
printf(“In function changed value of num is %d
\n”, n);
}
OUTPUT
In
main, the value of num is 100
In
function value of num is 100
In
function changed value of num is 200
Back
in main, the value of num is 100
CALL BY REFFRENCE
Instead of passing the value of a variable, we
can pass the memory address of the variable to the function. It is termed as
Call by Reference. We will discuss call by reference when we learn pointers.
EXAMPLE
main()
{
void swap(int *,int *);
int x=5,y=3;
swap(&x,&y);
printf(“X=%d,Y=%d”, x,y);
getch();
}
void modify(int *a,int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
RETURN VALUE OF FUNCTION
When a function gives a value then that type
of function is known as return value of function.
EXAMPLE
main()
{
int total(int a,int b);
int sum;
sum=total(35,36);
printf(“Sum is “,sum);
getch()
}
int total(int a,int b)
{
return(a+b);
}
RECURSION
When a function calls itself repeatedly and
this process is known as recursion and this type of function is called
recursive function. In this process there will be a condition, if it is false
then this function may have infinite loop.
EXAMPLE-
main()
{
printf (“\n Hello BCA \n”);
main();
getch();
}
Output- Sum
is 71
NESTING OF FUNCTION
When a function calls another function then
this concept is known as nested function.
EXAMPLE
main()
{
fun1();
getch();
}
fun1()
{
printf(“Hello\n”);
fun2();
printf(“BCA\n”);
}
fun2()
{
printf(“******”);
}
Hello
******
BCA
RETURN VALUE OF FUNCTION
Like the values of simple variable , it is
also possible to pass the values of an array to a function to pass an array
called function name of an array and size of the array are passed as argument.
EXAMPLE
main()
{
float long(float[],int),x[5],max;
int i;
for(i=0;i<5;i++)
{
printf(“Enter number “);
scanf(“%f”,&x[i]);
}
max=long(x , i);
printf(“Largest=%f”,max);
getch();
}
float long(float [],int m)
{
float m=a[0];
int I;
for(i=0;i<m;i++)
{
if
(a[i] > m )
m=a[i];
return;
}
}
COMMAND LINE ARGUMENTS
Parameters or Values can be passed to a
program from the command line which are received and processed in the main
function. Since the arguments are passed from the command line hence they are
called as command line arguments. This concept is used frequently to create
command files. All commands on the Unix Operating System use this concept.
EXAMPLE
Main(int argc , char *argv[] )
{
int i;
clrscr();
printf(“\nTotal
arguments =%d” , argc);\
for(
i=0;i<argc ;i++)
{
printf(“\n%s”,argc[i]);
}
getch();
}
Alt + C -> Compile the program.
Alt + R -> Run the program.
Save the file and go to file menu and select
OS Shell.
C:\TurboC2>DIR
C:\TurboC2>Filename arg1 arg2 …………..
Comments
Post a Comment