Function
Function
We already know that C support the use of library functions and user defined
functions. The library functions are used to carry out a number of commonly
used operations or calculations. The user-defined functions are written by the
programmer to carry out various individual tasks
Advantage of function
By using function large and difficult program can be
divided in to sub programs and solved. When we want to
perform some task repeatedly or some code is to be used
more than once at different place in the program, then
function avoids this repeatition or rewritten over and over.
Due to reducing size, modular function it is easy to modify
and test
Notes:-
C program is a collection of one or more function.
A function is get called when function is followed by the
semicolon.
A function is defined when a function name followed by a
pair of curly braces
• User defined function
• Syntax:-
• Return type name of function (type 1 arg 1, type2 arg2, type3 arg3)
Return type function name argument list of the above syntax
So when user gets his own function three thing he has to know, these are.
• Function declaration
• Function definition
• Function call
These three things are represented like
int function(int, int, int); /*function declaration*/
main()
{
function(arg1,arg2,arg3); /* calling function*/
}
int function(type 1 arg 1,type2 arg2,type3, arg3) /*function definition/*
{
Local variable declaration;
Statement;
Return value;
}
Function declaration:-
• Function declaration is also known as function prototype. It
inform the compiler about three thing, those are name of the
function, number and type of argument received
• While declaring the name of the argument is optional and the
function prototype always terminated by the semicolon.
Function definition:-
Function definition consists of the whole description and
code of the function. It tells about what function is doing
what are its inputs and what are its out put. It consists of two
parts function header and function body.
Syntax:-
return type function(type 1 arg1, type2 arg2, type3 arg3) /*function header*/
{
Local variable declaration;
Statement 1;
Statement 2;
Return value
}
The return type denotes the type of the value that function will return and it is
optional and if it is omitted, it is assumed to be int by default. The body of the
function is the compound statements or block which consists of local variable
declaration statement and optional return statement.
• The local variable declared inside a function is
local to that function only. It can’t be used
anywhere in the program and its existence is only
within this function.
• The arguments of the function definition are
known as formal arguments.
Function Call
When the function get called by the calling
function then that is called, function call. The
compiler execute these functions when the
semicolon is followed by the function name.
Example:-
function(arg1,arg2,arg3);
The argument that are used inside the function call
are called actual argument.
Ex:-
int S=sum(a, b); //actual arguments
The arguments which are mentioned or used inside
the function call is knows as actual argument and
these are the original values and copy of these are
actually sent to the called function.
It can be written as constant, expression or any
function call like
Function (x);
Function (20, 30);
Function (a*b, c*d);
Function(2,3,sum(a, b));
Formal Arguments
• The arguments which are mentioned in function definition are called formal
arguments. These arguments are used to just hold the copied of the values that
are sent by the calling function through the function call.
The basic difference between the formal argument and the actual argument are
1) The formal argument are declared inside the parenthesis where as the
local variable declared at the beginning of the function block.
2). The formal argument are automatically initialized when the copy of actual
arguments are passed while other local variable are assigned values through the
statements.
Order number and type of actual arguments in the function call should be match
with the order number and type of the formal arguments.
Return type
It is used to return value to the calling function.
It can be used in two way as
return
Or return(expression);
Ex:- return (a);
return (a*b);
return (a*b+c);
The return statement serves two purposes:
1. It immediately transfers the control back to the calling function
(i.e. no statements within the function body after the return
statement are executed).
2. It returns the value to the calling function.
Syntax:
return (expression);
where expression, is optional and, if present, it must
valuate to a value of the data type specified in the function
header for the return_type.
/*summation of two values*/
int sum (int a1, int a2);
main()
{
int a,b,s;
printf(“enter two no”);
scanf(“%d%d”,&a,&b);
s=sum(a,b);
printf(“summation is = %d”,s);
}
int sum(intx1,int y1)
{
int z
z=x1+y1;
return z;
}
Calling a function through main()…
• Note:
• In function call, there must be one actual argument
for each formal argument. This is because the value
of actual argument is transferred into the function
and assigned to the corresponding formal argument.
• If a function returns a value, the returned value can
be assigned to a variable of data type same as return
type of the function to the calling function.
• —
When a function is called, the program control is
passed to the function and once the function
completes its task, the program control is transferred
back to the calling function.
Write functions to add, subtract, multiply and
divide two numbers a and b.
#include<stdio.h>
#include<conio.h>
}
#include<stdio.h>
#include<conio.h>
void array_pass(int p, int q, int arr[][q]);
void main()
{
int i,m,j,n,a[3][3];
printf("enter the row of array\n");
scanf("%d",&m);
printf("enter the column of array\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
array_pass(m,n,a);
getch();
}
void array_pass(int p, int q, int arr[][q])
{
int j,i,sum=0;
printf("the elements of array are\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
}
PASSING STRINGS TO FUNCTIONS
Since strings are character arrays, the rules for passing strings to
functions are similar to those for passing arrays to functions.
Rules
1.The string to be passed must be declared as a formal argument of the
function definition in the function header.
void display(char item_name[])
{
……………
}
2.The function prototype must show that the argument is a string.
void display(char str[]);
3.A call to the function must have a string name without subscripts as its
actual argument.
display(name);
where, name is a properly declared string in the calling function.
Note: Like arrays, strings are passed by address.
TU EXAM QUESTION
Write a function to add, subtract, multiply and divide two complex numbers (x + i*y)
and
(a + i*b).
#include <stdio.h>
#include <conio.h>
void add(int x, int y, int a, int b)
{
printf("\n The addition of the complex numbers is:%d+i%d",x+a,y+b);
}
void subtract(int x, int y, int a, int b)
{
printf("\n The subtaction of the complex numbers is:%d+i%d",x-a,y-b);
}
void multiply(int x, int y, int a, int b)
{
int real, img;
real=(a*x-b*y);
img=(a*y+b*x);
printf("\n The multiplication of the complex numbers is:%d+i%d", real, img);
}
void main()
{
int x, y, a, b;
printf("\n Enter Ist complex number of the form
(x+iy):");
scanf("%d+i%d", &x, &y);
printf("\n Enter 2nd complex number of the form
(a+ib):");
scanf("%d+i%d", &a, &b);
add(x, y, a, b);
subtract(x, y, a, b);
multiply(x, y, a, b);
getch();