Functions
Functions
Functions
14 Types of Functions
i. Library Functions/Pre-Defined/ Built-in Functions
which perform some standard and
VC Library ofCCompiler has a collection of various functions
defined tasks.
as Library functions/Pre-Defined/Bui
V These functions written by designers of C Compilers are called
functions.
Ex:sqrt(n)- computes square root ofn.
pow(x,y)- computes x.
printf)- used to print the data on the screen.
scanf()- used to read the data from the keyboard.
abs(x)- computes absolute value of x.
1. Call by Value
V In this method, the called function creates new variables to store the value of the argument:
passed to it. Therefore, the called function uses a copy of the actual arguments to perform it
intended task.
V If the called function is supposed to modify the value of the parameters passed to it, then th
change will be reflected only in the called function. In the calling function, no change willb
made to the value of the variables. This is because all the changes are made to the copy of th
variables and not to the actual variables.
int sum;
sum =a + b:
Output:
return sum: Enter the values of a and b: 4 5
result =9
void main()
2. Call by Reference
V When the calling function passes arguments to the called function using the call-by-value
method, the only way to return the modified value of the argument to the caller is explicitly
using the return statement. A better option is to pass arguments using the call-by-reference
technique.
V In this method, we declare the function parameters as references rather than normal variables.
When this is done, any changes made by the function to the arguments it received are also
visible in the calling function.
V To indicate that an argument is passed using call by reference, an asterisk (*) is placed after the type
in the parameter list. Hence, in the call-by-reference method, a function receives an implicit
reference to the argument, rather than a copy of its value. Therefore, the function can modify the
value of the variable and that change will be reflected in the calling function as well.
Example:
1. Write a C program to add two numbers using call by reference.
#include<stdio.h>
int add (int *a,int *b)
int sum;
sum = *a + *b:
return sum;
void main()
{
int a,b, res; Output:
printf("Enter the values of a and b:"); Enter the values of aand b: 45
result =9
scanf("%d%d",&a, &b);
res = add(&a, &b);
printf("result -%dn", res);
2. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b-temp;
void main()
int a,b;
printf(Enter the values of a and b:"); Enter the values of aand b: 10 20
scanf("%d%d",&a,&b); Before swapping: a-10 b-20
printf("Be fore swapping: a-%d\tb-%d", a, b); After swapping: aF20 b=10
swap(&a,&b);
printf(After swapping: a-%ditb=%d", a, b)
Output:
Enter the values of aand b: 10 20
Before swapping: a=10 b=20
After swapping: a=20 b=10
Advantages
V Since arguments are not copied into the new variables, it provides greater time and space efficiency.
The function can change the value of the argument and the change is reflected in the calling
function.
A function can return only one value. In case we need to return multiple values, we can pass
those
arguments by reference, so that the modified values are visible in the calling function.
Disadvantages
However, the drawback of using this technique is that if inadvertent changes are caused to variables
in called function then these changes would be reflected in calling function as
would have been overwritten.
original values