Functions
Department of
Computer Science & Engineering
[Link]
Function
A function is a self contained block of code
that performs a particular task.
•Any function can call any other function.
• It can call itself.
•A ‘called function’ can also call another
function.
•A function can be called more than once.
This is called multi-function program.
Department of Computer Science & Engineering [Link]
Elements of User defined function
Function declaration Function definition
•Function definition
•Function call void add()
#include<stdio.h> {
•Function declaration void add(); int a,b, sum ;
void main() a=70;
{ b=20;
add(); sum=a+b;
} printf(“Addition of two
numbers =%d”, sum);
Function call }
Department of Computer Science & Engineering [Link]
Category of Functions
A function, depending on whether arguments are present or not and whether a value
is returned or not, may belong to the following categories:
Category 1: Functions with no arguments and no return values
Category 2: Functions with arguments and no return values
Category 3: Functions with arguments and one return value
Category 4: Functions with no arguments but a return value
Department of Computer Science & Engineering [Link]
Differences between actual and formal
Parameters
Actual Parameters Formal Parameters
Used in calling function Used in called function
May be variable names, Must be variable name
expressions or constants
add(5,10); int add(int a, int b)
add(a,b);
add(a+3,b+2);
Department of Computer Science & Engineering [Link]
Parameter Passing techniques
The technique used to pass data from one function to another is known
as parameter passing.
1. Pass by value (also known as call by value) .
2. Pass by Pointers (also known as call by pointers, call by reference).
Department of Computer Science & Engineering [Link]
Passing Arrays to Functions
Passing One Dimensional Arrays to Functions
int a[4]={11,12,13,14};
The name of the array represents the address of its first element. By
passing the array name, we are, in fact, passing the address of the array to
the called function.
Ex: display(a);
Mention the size of the array like:
display(a,4);
Department of Computer Science & Engineering [Link]
Passing One Dimensional Arrays to Functions
The array in the called function refers to the same array stored in the memory.
Therefore, any changes in the array in the called function will be reflected in
the original array.
1. The function must be called by passing only the name of the array.
2. In the function definition, the formal parameter must be an array type; the
size of the array does
not need to be specified (optional).
3. The function prototype must show that the argument is an array.
Department of Computer Science & Engineering [Link]
Example 1
void display(int a[], int n); void display(int a[], int n)
void main() {
{ int i;
int i,a[10], n; printf(“ array elements”);
printf(“enter n”); for(i=0;i<n;i++)
scanf(“%d”,&n); printf(“%d \t”, a[i]);
printf(“enter elements”); }
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
display(a,n);
}
Department of Computer Science & Engineering [Link]
Example 2
void bubblesort(int a[], int n); void bubblesort(int a[ ],int n)
void main() {
{ inti, j, t;
int i,a[10]; for(i=0;i<n-1;i++)
printf(“enter n”); {
scanf(“%d”,&n); for(j=0;j<n-1-i;j++)
printf(“enter elements”); {
if(a[j] >a[j+1])
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
t=a[j];
printf("before sorting\n");
a[j]=a[j+1];
for(i = 0; i< n; i++) a[j+1] = t;
printf("%d\t ",a[i]); }
bubblesort (a,n); }
printf("after sorting\n"); }
for(i = 0; i<n; i++)
printf("%d\t",a[i]); } [Link]
Passing Two Dimensional Arrays to Functions
•The functions must be called by passing only the array name.
•In the function definition, we must indicate that the array has two-
dimensions by including two sets of brackets.
•The size of the second dimension must be specified (compulsory), whereas
the size of the first dimension is optional.
•The prototype declaration should be similar to the function header.
Department of Computer Science & Engineering [Link]
Example
void display(int a[][], int m, int n)
void main() {
{
int m,n,i,j,a[10[10];
int I ,j;
printf(“ array elements are”);
printf(“enter the row size and column size”); for(i=0;i<m;i++)
scanf(“%d%d”,&m,&n);
printf(“enter elements”);
{
for(i=0;i<m;i++) printf(“\n”);
{ for(j=0;j<n;j++)
for(j=0;j<n;j++)
{ {
scanf(“%d”,&a[i][j]); printf(“%d \t”, a[i][j]);
} }
}
display(a,m,n); }
} }
Department of Computer Science & Engineering [Link]
Passing Strings to functions
Basic rules:
[Link] string to be passed must be declared as a formal argument of the function when it is
defined. The size of the dimension is optional.
void display(char name[])
{
…
}
2. The function prototype may show that the argument is a string. The size of the dimension is
optional.
For the above definition, the prototype can be written as
void display(char name[]);
3. A call to the function must have a sting name without subscripts as its actual argument.
display(sname); where name is a properly declared string array in the calling function.
Department of Computer Science & Engineering [Link]
Example
void display(char name[]); void display(char name[])
void main() {
{ printf(“the string is %s”,name);
char sname[20]; }
printf(“enter name”);
gets(sname);
display(sname);
}
Department of Computer Science & Engineering [Link]
Thank you
Department of Computer Science & Engineering [Link]