FUNCTION
Bijan Paul
Senior Lecturer
Department of CSE
University of Liberal Arts Bangladesh
Topics
• Functions
– Parameters
– Return Types
– Call by value
– Call by reference
Function inside Function example
#include<stdio.h>
void print_square( int a );
int square( int a );
int main() {
int i;
for( i = 1; i <= 10; i++ )
print_square( i );
return 0;
void print_square( int a ) {
printf( "The square of %d is: %d\n", a, square(a) );
}
int square( int a ) {
return( a * a );
}
CALL C FUNCTIONS
• There are two ways that a C function can be called from a program.
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.
Note:
• Actual parameter – This is the argument which is used in function call.
• Formal parameter – This is the argument which is used in function definition
EXAMPLE :CALL BY VALUE
#include<stdio.h>
// function declaration • In this program, the values of the variables
void swap(int a, int b); “m” and “n” are passed to the function
“swap”.
• These values are copied to formal
int main()
parameters “a” and “b” in swap function and
{ used.
int m = 22, n = 44;
// calling swap function by value
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
return 0;
}
values before swap m = 22
and n = 44
void swap(int a, int b)
values after swap m = 44
{
and n = 22
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}
CALL BY VALUE : no changes in actual values,
changed inside the function only
#include <stdio.h>
/* swap function definition */
void swap(int x, int y); /* function declaration */
void swap(int x, int y) {
int temp;
int main () {
temp = x; /* save value of x */
int a = 100, b = 200;
x = y; /* put y into x */
y = temp; /* put temp into y */
printf("Before swap, value of a : %d\n", a );
}
printf("Before swap, value of b : %d\n", b );
/* calling a function to swap the values */
swap(a, b);
Before swap, value of a :100
printf("After swap, value of a : %d\n", a );
Before swap, value of b :200
printf("After swap, value of b : %d\n", b );
After swap, value of a :100
After swap, value of b :200
return 0;
}
Example
#include <stdio.h>
int running_total( void );
int main() {
int i, total=0;
for( i = 1; i <= 5; i++ ) Output:
total += running_total ( ); Enter a score: 1
printf("Total: %d\n", total); Enter a score: 2
return 0; Enter a score: 3
} Enter a score: 4
Enter a score: 5
int running_total( void ) {
Total: 15
int score;
printf( "Enter a score: " );
scanf( "%d", &score );
return score;
}
Function call by reference
• The call by reference method of passing arguments to a
function copies the address of an argument into the
formal parameter.
• Inside the function, the address is used to access the
actual argument used in the call. It means the changes
made to the parameter affect the passed argument.
• To pass a value by reference, argument pointers are
passed to the functions just like any other value.
• So accordingly you need to declare the function
parameters as pointer types.
• Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
EXAMPLE: CALL BY REFERENCE
#include <stdio.h> void swap(int *x, int *y)
void swap(int *x, int *y); {
int main () { int temp;
int a = 100, b = 200; temp = *x; *save value at address x */
*x = *y; /* put y into x */
printf("Before swap, value of a : %d\n", a ); *y = temp; /* put temp into y */
printf("Before swap, value of b : %d\n", b ); }
/* calling a function to swap the values . &a
indicates variable a address, &b indicates
variable b address .
*/
swap(&a, &b);
Before swap, value of a :100
printf("After swap, value of a : %d\n", a ); Before swap, value of b :200
printf("After swap, value of b : %d\n", b );
After swap, value of a :200
return 0;
After swap, value of b :100
}
End