USER-DEFINED FUNCTIONS
Functions that we define ourselves to do certa
specific task are referred as user-
defined functions.
Advani Sf fined fun
© The program will be easier to understand, maintain and debug.
‘© Reusable codes that can be used in other programs
© Alarge program can be divided into smaller modules. Hence, a large project can
be divided among many programmers.
Three parts ofa user defined functions are:
Function Declaration
A function prototype is simply the declaration of a function that specifies
function's name, parameters and return type. It doesn't contain function body. A
function prototype gives information to the compiler that the function may later be
used in the program.
Syntax
Eunction Definition
A function definition in C programming consists of a function header and a function
body. Function header consist of return type function name,arguments(parameters)
+ Return Type: A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void
+ Function Name: The actual name of the function. The function name and the
parameter list together constitute the function signature.
+ Parameters: A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. Parameters are optional; that is, a function may
contain no parameters.
* Function Body: The function body contains a collection of statements that
define what the function does.
syntax:
return type function name( argument list ) (
body of the function
Function Call
When a program calls a function, the program control i
transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the program
control back to the main program.
Syntax:
functionName(parameter list);‘TYPES OF USER DEFINED FUNCTIONS
ees z
// function to read two numbers and print its sum
void addQ)
{
int absum;
printf("Enter the values of a & b");
scanf("%d%d" &a,&b);
sum = a+b;
printf("Sum=%d",sum);
// function that takes two arguments and print their sum.
void add(int a,int b)
{
int sum;
printf("Sum=%d"sum);
)
eeu i
// function to read two numbers and return its sum
intaddQ
{
int a,b,sum;
printf("Enter the values ofa & b");
scanf("%d%d",8a,&b);
return sum;
// function that takes two arguments and print their sum.
int add(int a,int b)
{
return (a+b);
y
Function that have multiple return values
#include
int even(num)
{
if( num%2==0)
return 1;
else
return 0}
}
void main
{
int sa;
printf("enter a");
scanf("%d",&a);
printf("%d",even(a));RECURSIVE FUNCTION
A function that calls itself is known as a recursive function. And, this techni
known as recursion. While using recursion, programmers need to be careful to define a
termination condition from the function; otherwise it will go into an infinite loop.
‘Termination condition will be the base case where the problem can be solved without
further recursion, Recursion makes program elegant. However, if performance is vital,
use loops instead as recursion is usually much slowe:
| Write a program to find factorial of a number using recursive function
Hinclude
int fact(int m)
{
ifn == 0)
return 1;
else
return n*fact(n-1);
}
void mainQ
{
int n;
printf("Enter the numbe}
scanf("%d",8n);
printf("Factorial=%d",fact(n));
y
output
Enter the number:5
Factorial=120
+ Write a program to find nCr and Pr.
Hinclude
int fact(int n)
{
if(n
else
return n*fact(n-1);
void main
. oureut
float GP;
rine heer wie naambere Enter the numbers:5 3
scani("%d%d",n,8r); rnCr=10,000000
float) fact(n) / fact(n-r)};
E thoatacatny / (act) = eet(a-r) pis soaoony
printiC'nCr=%f\n".C);
printi(nPr=%6f"P);
}
+ Write a recursive function to find the sum of first n natural numbers
winclude=stdio.h=
int sum(int n)
{
f(n<=0)
return 0:
else printf("Sum=%d",sum(n));
return n+ sum(n-1); }
ourpuT
Enter the number:5
Sum=15
void mainPASSING AN ARRAY AS PARAMETER
Like the value of simple variables, it is also possible to pass the values of an array
to a function. To pass a single dimensional array to a function, it is sufficient to list the
name of the array without any subs«
Rules to pass an Array to Function
pts and the size of the array as arguments.
+ The function must be called by passing only the name of the array.
+ In function definition, formal parameter must be an array type; the size of the array
does not need to be specified.
+ The function prototype must show that the argument is an array.
‘+ Write a function to sort an array.
#finclude
int sort(int Af].int n)
€
int ij.temp:
for(i=0;icn-1ii++)
{
for(j=0;jen-i-1;j++)
c
if(AU]>AU+ 1)
(
temp = AU:
ALi] = AUi+1);
Ali+1]= temp:
)
,
)
)
void mainQ)
‘
int A[30];
inti
printf("Enter the limit:");
seanf("%d" 8);
for(i=O;ienii++)
t
printf(“Enter the element:");
seanf("%d".SA(i]);
>
sort(A.n);
printf(*Sorted Array\n’
for(i=Ori
int sort(int A[}.int n)
(
int ij;temp;
for(j=0;jAL+1))
{
temp = Ali]:
A) = AU+1]:
Afje1]= temp;
}
}
?
}
void main()
int A[30] [30];
y
fnter the order of matrix");
Seant("9d%d" m,n):
forG=0;iemsi+s)
«
For(=0;j
Onemitt)
sore(ali.n’
forU=0:jen:j+)
€
prinef("96d\e- ATU):
>
printhe\n"):
oureur
Enter the element:12
Enter the element
Enter the element:25,
Enter the element:7
Enter the element:S
Enter the element:16
4
oe aietizeStructure
Union
struct keyword is used to define a
structure
union keyword is used to define a union
Members do not share memory in a
structure.
Members share the memory space in a
‘Any member can be retrieved at any time
ina structure
Only one member can be accessed at a time
Several members of a structure can be
initialized at once.
Only the first member can be initialized,
Size of the structure is equal to the sum of
size of the each member.
Size of the union is equal to the size of the
largest member.
PASSING 2D ARRAY AS PARAMETER
Note: While passing 2D array as parameter we need to mention the maxi
element of each row that is column.
Write a program to pass a 2D mat
elements.
#include
jum size of
as parameter and find its sum of all the
// function that takes a 2D array and its order
int sum(int Af][30],int m,int n)
t
for(j=0;j
struct Student{
char name[30];
int rolinum;
int mark for_C;
3
void main({
struct Students[30];
intinsum=0;
float avg:
printf("Enter the no of Student:");
scanf("%d",&n);
for(i=0;i