Calling The C Function
Calling The C Function
Topics Covered: Function that return a value, Parameters and use of arguments to pass data to a
function, Formal/ Actual parameters, Passing values between functions, Formal/ Actual
parameters
Function definition
A function definition in C programming consists of following parts–
syntax
Function Name
There are two ways that a C function can be called from a program. They are
1. Call by value
2. Call by reference
1. Call by value:
In call by value method, the value of the variable is passed to the
function as parameter.
The value of the actual parameter can not be modified by formal
parameter.
Different Memory is allocated for both actual and formal parameters.
Because, value of actual parameter is copied to formal parameter.
Example
#include<stdio.h>
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
}
Output:
values before swap m = 22
and n = 44
values after swap m = 44
and n = 22
Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore
2. Call by reference:
In call by reference method, the address of the variable is passed to
the function as parameter.
The value of the actual parameter can be modified by formal
parameter.
Same memory is used for both actual and formal parameters since
only address is used by both parameters.
Example
#include<stdio.h>
void swap(int *a, int *b);
int main()
{
int m = 22, n = 44;
printf("values before swap m = %d \n and n = %d",m,n);
swap(&m, &n);
}
Output:
values before swap m = 22
and n = 44
values after swap a = 44
and b = 22
Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore
Programming Fundamentals [C Language] by: SHAH NAWAZ,UoG,Lahore
Example
#include <stdio.h>
#include <conio.h>
}
int myfunction(int a, int b)
{
int c;
c=a+b;
return c;
}
Output: