Formal arguments & Actual arguments
In F’n declaratory (or) in f’n header, whatever the variables we are creating those are called formal arguments or parameters.
- In f’n calling statement, whatever the data we are passing, those are called actual arguments or “Arguments” in C
- In order to call any f’n, if it is required specific num of parameters then we can’t invoke the f’n with less than or more than required no.of arguments.
Where the implementation part of the f’n is available , it is called f’n definition.
- Always f’n definition contains body of the f’n
- In f’n definition, first line is called f’n declarator or f’n header.
- Where we are invoking the logic of the f’n , it is called f’n “ calling statement “
Calling conversions : –
- As a programmer, it is our responsibility to indicate the parameter creation sequence at the time of calling the f’n.
- Always calling conversions will decides that in which sequence, parameters need to be created i.e left to right or right to left
- In ‘c’ programming language, there are ‘2’ types of calling conversions are available e
- cdecl (- cdecl)
- pascal (- pascal)
Cdecl calling conversion :
- When we are working with cdecl calling conversions, then parameters will constructed towards from right to left.
- By default, any f’n calling conversion is “cdecl” only
- When we are working with “cdecl” calling conversion, then recommended to place the f’n name in lower case
2)Pascal calling conversion :
- In pascal calling conversion, Arguments will pass towards from left to right
- Generally pascal calling conversions are used in database programming.
- When we are working with pascal calling conversion, then recommended to place f’n name in upper case.
- Always formal arguments or parameters are initialized with actual arguments or arguments data at the time of calling the f’n.
void cdecl abc(int x, int y)
printf(“\n x=%d y=%d”, x,y);
o/p : x=12 y=10 a=12
void pascal xyz(int x, int y)
printf(“\n x=%d Y=%d”, x,y);
o/p : x=10; y=12
Parameter passing techniques:-
In ‘c’ programming language there are two types of parameters passing techniques are available i.e
- call by value or pass by value
- call by address or pass by address.
CALL by value : –
- When ever, we are calling a f’n by sending value type data, then it is called call by value.
- in call by value, actual arguments and formal arguments both are value type variable only.
- if any modifications occur on formal arguments in C, then those changes will not affect on actual arguments.
eg: printf(), pow(), sqrt(), cos(), delay(), text color()
Call BY Address :
- When we are calling a f’n by sending address type data , then it is called call by address.
- when we are working with call by address, then actual arguments are address, type, formal arguments are pointer type.
- If any modifications occur on formal arguments, then those changes will pass to actual arguments also.
eg: scanf(), strcpy(),strrev(),strupr(),settime()
printf(“\n a=%d b=%d ”,a,b);
o/p : error
undefined symbol a,b
- According to storage classes of ‘c’ , a&b are auto variables which is constructed within the body of main f’n, so we can’t access in swap f’n
- In order to call the swap f’n it doesn’t require any parameters but we are calling the f’n by using two arguments, so, it is not possible.
void swap (inta, intb)
printf(“\n data in swap a=%d b=%d ”,a,b);
printf(“\n data in main a=%d b=%d ”,a,b);
o/p : data in swap a=20 b-10
data in main a=10 b=20
- in above program, swap fin will works according to call by value mechanism. That’s why no any modification of swap f’n will carry back to main f’n
- In implementation, when we are expecting the modification, then recommended to create the program by using call by address mechanism.
void main(int*p, int*b)
printf(“\n data in swap a=%d b=%d, *p1,*p2”);
- The above program swap f’n is working with the help of call by address mechanism, that why all the modification of the swap f’n will pass back to main f’n
Note : ‘c’ programming language does not supports call by reference.
- Call by reference is a oops feature which works with the help of reference variable.
- ‘c’ language doesn’t having any reference variables, so call by reference is not possible.
|int a=10 b=20 int a=10,b=20,c=30 inta=10, b=20,c=30,d-40a=a+b; a=a+b+c; a=a+b+c+db=a-b; b=a-(b+c); b=a-(b+c+d);a=a-b; a=a-(b+c); c=a-(b+c+d);
o/p a=20;b=10 a=a-(b+c); d=a-(b+c+d);
| Program to Call by reference
- “returns” is a keyword, by using return keyword, we can pass the control back to the calling place with arguments or without arguments
- In a f’n, we are place any number of return statements, but at even any point of time only one return statement executes.
- By using return, statement , it is possible to return only one value by using f’n return type also, it is possible to return only one value.
- In implementation, when we need to return more than one value from f’n , then recommended to go for call by address
- By using call by address it is not possible to return multiple values but possible to collect multiple values.
int max(int x, int y)
printf(“\n max value is %d”,m);\
o/p : max value is =20
int abc(int *p,int *p2)
printf(“\ni=%d j=%d k=%d”,i,j.k);
o/p : i=20; j=2; k=10
Enter a value =5
5 fact value is =120
// int fact(int); declaration
printf(“Enter a value”);
printf(“\n %d fact value is : %d”,n,f);
int fact (int n)
- According to K and RC standard above prog is valid because whenever the f’n return type is an integer parameter type is other than float then doesn’t required to go for forward declaration.
- According to ANSI-C standard, the above program is error because forward declaration of a f’n must be required whenever we are defining a f’n after calling.