0% found this document useful (0 votes)
10 views

Function

The document provides an overview of functions in C programming, detailing their types, advantages, and syntax for declaration, definition, and calling. It explains the difference between library functions and user-defined functions, as well as the concepts of formal and actual arguments, return types, and function calls by value and by address. Additionally, it includes examples of various functions, including arithmetic operations and recursion.

Uploaded by

rimipo8410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Function

The document provides an overview of functions in C programming, detailing their types, advantages, and syntax for declaration, definition, and calling. It explains the difference between library functions and user-defined functions, as well as the concepts of formal and actual arguments, return types, and function calls by value and by address. Additionally, it includes examples of various functions, including arithmetic operations and recursion.

Uploaded by

rimipo8410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

FUNCTION

• A function is a self contained block of codes


or sub programs with a set of statements that
perform some specific task or coherent task when
it is called.
• Any ‘C’ program contain at least one function i.e
main().
• There are basically two types of function those
are
1. Library function
2. User defined function
• The user defined functions defined by the user
according to its requirement
• System defined function can’t be modified, it can only
read and can be used.These function are supplied with
every C compiler

The basic philosophy of function is divide and conquer


by which a complicated tasks are successively divided
into simpler and more manageable tasks which can be
easily handled. A program can be divided into smaller
subprograms that can be developed and tested
successfully.
There are many advantages in using functions in a program they are:

1. It facilitates top down modular programming.


2. The length of the source program can be reduced by using functions at
appropriate places.
3. A program can be used to avoid rewriting the same sequence of code at two or
more locations in a program. This is especially useful if the code involved is long
or complicated.
4. Programming teams does a large percentage of programming. If the program
is divided into subprograms, each subprogram can be written by one or two team
members of the team rather than having the whole team to work on the complex
program

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>

int sum (int a1, int a2);


int diff(int b1,int b2);
float div(int c1, int c2 );
int main()
{
int a,b,d,s;
float di;
printf("enter the two no");
scanf("%d%d",&a,&b);
s=sum(a,b);
printf("the sum of two number is %d\n",s);
d=diff(a,b);
printf("the difference of two number is %d\n",d);
di=div(a,b);
printf("the division of two number is %f\n",di);
}
int sum(int x1,int y1)
{
int z;
z=x1+y1;
return z;
}
int diff(int p1,int q1)
{
int w;
w=p1-q1;
return w;
}
float div(int c1, int c2 )
{
float r;
r=(float)c1/c2;
return r;
}
/*Program using function to find the greatest number among two numbers*/
#include <stdio.h>
#include <conio.h>
int greater(int, int);
void main()
{
int a, b,x;
printf("\n Enter three numbers:");
scanf("%d %d ",&a, &b);
x=greater(a, b);
printf("\n The greatest number is:%d", x);
getch();
}
int greater(int x, int y)
{
if(x>y)
return x;
else
return y; }
/*Program using function to find the greatest number among three numbers*/
#include <stdio.h>
#include <conio.h>
int greater(int, int);
void main()
{
int a, b, c, d, e;
printf("\n Enter three numbers:");
scanf("%d %d %d",&a,&b,&c);
d=greater(a, b);
e=greater(d, c);
printf("\n The greatest number is:%d", e);
getch();
}
int greater(int x, int y)
{
if(x>y)
return x;
else
return y;
}
TU EXAM QUESTION
• Write and test the following power() function
that returns x raised to the power p, where p can
be any integer:

double power (double x, int p)


double power(double x, int p)
void main()
{
int p;
double x, z;
printf("Enter x and p:\t");
scanf("%lf %d", &x, &p);
z=power(x, p);
printf("\n x raised to power n is:%lf", z);
getch();
}
double power(double x, int p) //x=2 and p=3
{
double a=1;
if(p==0)
return 1;
else if(p>0)
{
while(p!=0)
{
a=a*x; //a=1*5=5, a=5*5=25, a=25*5=125;
p--;
}
return a;
}
else
{
while(p!=0)
{
a=a/x; // a=a*1/x a=1*1/5=1/5, a=1/5*1/5=1/25, a=1/25*1/5=1/125
p++;
}
return a;
}
}
TYPES OF FUNCTIONS
1)Functions with no arguments and no return values
2)Functions with arguments but no return values
3)Functions with arguments and return values
WITH NO ARGUMENTS AND NO RETURN VALUES
• When a function has no arguments, it does not
receive any data from the calling function.
• When a function does not return a value, the calling
function does not receive any data from the called
function.
• —
Thus, there is no data transfer between the calling
function and the called function.
Syntax: void function_name()
{
/* body of function */
}
#include <stdio.h>
#include <conio.h>
void add() ;
void main()
{
add();
getch();
}
void add()
{
int a, b, sum;
printf("\n Enter two numbers:");
scanf("%d %d",&a,&b);
sum = a + b;
printf("\n The sum is:%d", sum);
}
FUNCTIONS WITH ARGUMENTS BUT NO RETURN VALUE
• This type of function has arguments and receives
the data from the calling function.
• —
But after the function completes its task, it
doesnot return any values to the calling function.
Syntax:
void function_name(argument_list)
{
/* body of function */
}
EXAMPLE
#include <stdio.h>
#include <conio.h>
void(int a, int b);
void main()
{
int a,b;
printf("\n Enter two numbers:");
scanf("%d %d",&a,&b);
add(a, b);
getch();
}
void add(int a, int b)
{
int sum;
sum=a + b;
printf("\n The sum is:%d", sum);
}
FUNCTIONS WITH ARGUMENTS AND RETURN VALUES
• This type of function has arguments and receives the
data from the calling function.
• However, after the task of the function is complete, it
returns the result to the calling function via return
statement.
• —
So, there is data transfer between called function and
calling function using return values and arguments.
Syntax:
return_type function_name(argument_list)
{
/* body of function */
}
//EXAMPLE
#include <stdio.h>
#include <conio.h>
int add(int a, int b);
void main()
{
int a, b, x;
printf("\n Enter two numbers:");
scanf("%d %d", &a, &b);
x=add(a, b);
printf("\n The sum is:%d", x);
getch();
}
int add(int a, int b)
{
int sum;
sum=a + b;
return sum;
}
TYPES OF FUNCTION CALLS
Function calls are divided in two ways according
to how arguments are passed in the function.
• 1.Pass arguments by value (Function call by Value)
• 2.Pass arguments by address (Function call by
Reference)
PASSING BY VALUE
• When values of actual arguments are passed
to the function as arguments, it is known as
passing by value.
• Here, the value of each actual argument is
copied into corresponding formal argument of
the function definition.
• —
Note: The contents of the actual arguments in
the calling function are not changed, even if
they are changed in the called function.
#include <stdio.h>
#include <conio.h>
void swap(int, int);
void main()
{
int a=50, b=100;
printf("\n Before swap function call: a=%d and b=%d", a, b);
swap(a,b);
printf("\n After swap function call: a=%d and b=%d", a, b);
getch();
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("\n Values within swap: x=%d and y=%d", x, y);
PASSING BY ADDRESS
• When addresses of actual arguments are passed to
the function as arguments (instead of values of
actual arguments), it is known as passing by
address.
• Here, the address of each actual argument is copied

into corresponding formal argument of the function
definition.
• In this case, the formal arguments must be of type

pointers.
• Note: The values contained in addresses of the

actual arguments in the calling function are
changed, if they are changed in the called function.
#include<stdio.h>
#include<conio.h>
void change_variable(int *p); a p
int main() 10 2000
{ 2000 3000
int a;
printf("enter the value of a\n");
scanf("%d",&a);
printf("before calling the function, a is %d\n",a);
change_value(&a);
printf("after calling the function, a is %d\n",a);
}
void change_value(int *p)
{
*p=(*p)+10;
printf("the value of a in function changes is %d\n",*p);
}
#include <stdio.h>
#include <conio.h>
void swap(int *, int *);
void main()
{
int a=50, b=100;
printf("\n Before swap function call: a=%d and b=%d", a, b);
swap(&a, &b);
printf("\n After swap function call: a=%d and b=%d", a, b);
getch();
}
void swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("\n Values within swap: x=%d and y=%d", *x, *y);
 Write a program to find the area of circle
(using function).
 Write a program to find the factorial of any
numbers (using function).
 Write a program to print any number in reverse
order( using function ).
RECURSIVE FUNCTION
When a function calls itself directly or indirectly, it is
called recursive function and the process of calling
itself is called recursion.

E.g.
#include <stdio.h>
#include <conio.h>
void main()
{
printf("Hey! This is direct recursion.\n");
main();
getch();
}
#include <stdio.h>
#include <conio.h>
void printline();
void main()
{
printf("Hey! This is not direct recursion.\n");
printline();
getch();
}
void printline()
{
printf("Indirect Recursion\n");
main();
}
RECURSIVE FUNCTION
• To solve a problem using recursive method, two
conditions must be satisfied:
1)Problem should be written or defined in terms of
its previous result.
2)Problem statement must include a terminating
condition, otherwise the function will never
terminate. This means that there must be an if
statement somewhere in the recursive function to
force the function to return without the recursive
call being executed
Suppose x=3
f(3)=3+f(3-1)=3+f(2)
f(2)=2+f(1)
f(1)=1;
.
Suppose x=3
f(3)=3+f(3-1)=3+f(2)
f(2)=2+f(1)
f(1)=1;

long int function(int x)


{
if(x==1)
return 1;
else
return (x+function(x-1));
}
#include <stdio.h>
#include <conio.h>
long int function(int n);
void main()
{
int number;
long int x;
printf("Enter a number :\t");
scanf("%d", &number);
x=function(number);
printf("\n The value of f(x) is :%ld", x);
getch();
}
long int function(int n)
{
if(n= =1)
return 1;
else
return (n+function(n-1)); //3+function(2)//3+2+fun(1)//3+2+1;
}
//Factorial of a number using recursion
#include <stdio.h>
#include <conio.h>
long int factorial(int n);
void main()
{
int number;
long int x;
printf("Enter a number whose factorial is needed:\t");
scanf("%d", &number);
x=factorial(number);
printf("\n The factorial is:%ld", x);
getch();
}
long int factorial(int n)
{
if(n==1)
return 1;
else
return (n*factorial(n-1));
}
PASSING ARRAY TO A FUNCTION
• It is possible to pass the value of an array
element or an entire array as an argument to a
function.
• Passing array elements is similar to passing
variables except that array subscripts are
needed in function call.
//Passing elements of an array
#include <stdio.h>
#include <conio.h>
void display(int, int);
void main()
{
int nums[5]={1,2,3,4,5};
printf("\n The array elements passed are:\n");
display(nums[3], nums[4]);
getch();
}
void display(int n, int m)
{
printf("%d\t %d", n, m);
}
PASSING 1-D ARRAY TO A FUNCTION…
• To pass an entire array to a function, the array name is given
without square brackets or subscripts, as an actual argument
in function call statement.
• In the function definition, the corresponding formal
argument must declare an array with a pair of empty square
brackets (without specifying the size of the array, although
it doesn’t matter) for a One-Dimensional array.
• —
The function prototype must show that the argument is an
array.
• Syntax for function call:
function_name(array_name);
• Syntax for function prototype:
return_type function_name(data_type array_name[]);
//PASSING 1-D ARRAY TO A FUNCTION…
#include<stdio.h>
#include<conio.h>
void array_pass(int arr[]);
int main()
{
int a[5]={1,2,3,4,5};
array_pass(a);
getch();
return 0;
}
void array_pass(int arr[])
{
int j,sum=0;
printf("the elements of array are\n");
for(j=0;j<5;j++)
{
printf("%d\n",arr[j]);
sum=sum+arr[j];
}
printf("the sum of array elements is %d\n",sum);
}
#include<stdio.h>
void array_pass(int arr[],int m);
void main()
{ int i,n,a[20];
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
array_pass(a,n);
getch();
}
void array_pass(int arr[],int m)
{
int j,sum=0;
printf("the elements of array are\n");
for(j=0;j<m;j++)
{
printf("%d\n",arr[j]);
sum=sum+arr[j];
}
printf("the sum of array elements is %d\n",sum);
}
#include<stdio.h>
#include<conio.h>
void array_pass(int arr[][3]);
void main()
{
int i,j,a[3][3];
printf("enter the array elements\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
array_pass(a);
getch();}
void array_pass(int arr[][3])
{
int j,i,sum=0;
printf("the elements of array are\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}

}
#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();

You might also like