Functions
A User-Defined Function (UDF) is a function created by the programmer to perform a
specific task. While built-in functions are provided by C libraries, user-defined functions let
us to customize the program, reuse code, and make it more organized and modular.
simply says "A user-defined function is a block of code that performs a specific job and can
be called multiple times in a program."
Structure Of the Function
C functions are made up of four key parts:
Function Declaration (Prototype)
Function Definition (Body)
Function Call
Return Statement (optional)
returnValue FunctionName(Arguments/Parameter)
Classifications
Functions without Arguments and without Return value
Functions with Arguments and without Return value
Functions without Arguments and with Return value
Functions with Arguments and with Return value
Examples of Built in Functions with Classification
1. Function with no arguments and no return value
-Executes a task, doesn’t take input or return output.
2. Function with arguments but no return value
-Takes input, performs a task, doesn’t return result.
3. Function with no arguments but return value
-Doesn’t take input, but returns a result.
4. Function with arguments and return value
-Takes input and returns output.
Function Declaration or Function Prototyping
return type functionName(argumentType1,argumentType2,argumentType3,...)
in Main Function
function Calling
functionName(actualArg1,Actual Arg2,...)
Function Definition
outside the main Function
returnType FunctionName(datatype Formal argument1,datatype Formal argument2,..)
{
function body
}
Note : formal function argument also known as dummy argument
Example1
#include<stdio.h>
void Add();
main()
{
printf("\n1. Functions without Arguments and without Return value");
Add();
printf("\nFunction Called Successfully");
Add();
}
void Add()
{
int n1,n2;
printf("\nEnter the value for n1");
scanf("%d",&n1);
printf("\nEnter the value for n2");
scanf("%d",&n2);
printf("\nThe Addition Result is : %d",(n1+n2));
}
Example2
#include<stdio.h>
void Add(int,int);
main()
{
int n1,n2;
printf("\n1. Functions with Arguments and without Return value");
printf("\nEnter the value for n1:");
scanf("%d",&n1);
printf("\nEnter the value for n2:");
scanf("%d",&n2);
Add(n1,n2);
printf("\nFunction Called Successfully");
Add(25,55);
}
void Add(int a,int b)
{
printf("\nThe Addition Result is : %d",(a+b));
}
Example3
#include<stdio.h>
int Add();
main()
{
int ans=0;
printf("\[Link] without Arguments and with Return value");
ans=Add();
printf("\nThe Addition Result is : %d",ans);
printf("\nThe Addition Result is : %d",Add());
}
int Add()
{
int n1,n2;
printf("\nEnter the value for n1:");
scanf("%d",&n1);
printf("\nEnter the value for n2:");
scanf("%d",&n2);
return (n1+n2);
}
Example4
#include<stdio.h>
int Add(int,int);
main()
{
int n1,n2,ans=0;
printf("\[Link] with Arguments and with Return value");
printf("\nEnter the value for n1:");
scanf("%d",&n1);
printf("\nEnter the value for n2:");
scanf("%d",&n2);
ans=Add(n1,n2);
printf("\nThe Addition Result is : %d",ans);
printf("\nThe Addition Result is : %d",Add(100,500));
}
int Add(int a,int b)
{
return (a+b);
}
Example 5
#include<stdio.h>
int add(int a,int b)
{
return(a+b);
}
main()
{
int n1,n2;
int ans=0;
printf("\[Link] with Arguments and with Return value");
printf("\nEnter the value for n1");
scanf("%d",&n1);
printf("\nEnter the value for n2");
scanf("%d",&n2);
ans=Add(n1,n2);
printf("\nThe Addition Result is : %d",ans);
printf("\nThe Addition Result is : %d",Add(100,500));
Nested Function
Example 1
#include<stdio.h>
void Calc(int,int,char);
int Add(int,int);
int Sub(int,int);
int Multi(int,int);
float Div(int,int);
main()
{
int n1,n2;
char ch;
printf("\n\t\t\tCalculator");
printf("\n\t\t\t============");
printf("\[Link] =>A");
printf("\[Link] =>S");
printf("\[Link] =>M");
printf("\[Link] =>D");
printf("\nPlease enter the Choice[A/S/D/M]");
scanf("%c",&ch);
printf("\nEnter the number 1:");
scanf("%d",&n1);
printf("\nEnter the number 2:");
scanf("%d",&n2);
Calc(n1,n2,ch);
}
void Calc(int m1,int m2,char choice)
{
switch(choice)
{
case 'A':
case 'a':
{
printf("\nAddition Result is : %d",Add(m1,m2));
break;
}
case 'S':
case 's':
{
printf("\nSubtraction Result is : %d",Sub(m1,m2));
break;
}
case 'M':
case 'm':
{
printf("\nMultiplication Result is : %d",Mul(m1,m2));
break;
}
case 'D':
case 'd':
{
printf("\nDivision Result is : %f",Div(m1,m2));
break;
}
default:
{
printf("\nInvalid choice");
}
}
}
int Add(int v1,int v2)
{
return (v1+v2);
}
int Sub(int v1,int v2)
{
return (v1-v2);
}
int Mul(int v1,int v2)
{
return (v1*v2);
}
float Div(int v1,int v2)
{
float res=(float)v1/(float)v2;
return (res);
}
Recursive Function
A function calls itself is known as Recursive function.
Example 1
#include<stdio.h>
void Num(int);
main()
{
int n;
printf("\nEnter the n value to print a Series");
scanf("%d",&n);
Num(n);
}
void Num(int n1)
{
if(n1>0)
{
printf("\n%d",n1);
Num(--n1);
}
}
Call By Value
Example 1
#include<stdio.h>
void Show(int);
main()
{
int n;
printf("\nEnter the value for n:");
scanf("%d",&n);
printf("\nThe value of n (before Function call): %d",n);
Show(n);
printf("\nThe value of n (After Function call): %d",n);
}
void Show(int a)
{
a+=100;
printf("\nThe value of a is %d",a);
}
Call by Reference
Example 1
#include<stdio.h>
void Show(int*);
main()
{
int n;
printf("\nEnter the value for n:");
scanf("%d",&n);
printf("\nThe value of n (before Function call): %d",n);
Show(&n);
printf("\nThe value of n (After Function call: %d",n);
}
void Show(int *a)
{
*a+=100;
printf("\nThe value of a is %d",*a);
}
Passing an array as argument
Examble 1:
#include<stdio.h>
void printArray(int m1[], int m2[])
{
int i;
printf("\nArray1 Values are\n");
for(i=0;i<5;i++)
printf("%d ",m1[i]);
printf("\nArray2 Values are\n");
for(i=0;i<5;i++)
printf("%d ",m2[i]);
main()
{
int n1[5],n2[5],i;
printf("\nEnter the value for Array1\n");
for(i=0;i<5;i++)
scanf("%d",&n1[i]);
printf("\nEnter the value for Array2\n");
for(i=0;i<5;i++)
scanf("%d",&n2[i]);
printArray(n1,n2);
}
Example 2
#include<stdio.h>
void printData(int sno,char *name,int m1,int m2,int m3,int m4, int m5)
{
int total=m1+m2+m3+m4+m5;
float avg=(float)total/5.0;
printf("\n\t\t\tSTUDENT INFORMATION");
printf("\n\t\t\t###########################");
printf("\nStudent RegNo\t:%d",sno);
printf("\nStudent Name\t:%s",name);
printf("\nStudent Mark1\t:%d",m1);
printf("\nStudent Mark2\t:%d",m2);
printf("\nStudent Mark3\t:%d",m3);
printf("\nStudent Mark4\t:%d",m4);
printf("\nStudent Mark5\t:%d",m5);
printf("\n----------------------------------------------------------");
printf("\nTotal\t: %d Average:\t %.2f Result\t: ",total,avg);
((m1>=35)&&(m2>=35)&&(m3>=35)&&(m4>=35)&&(m5>=35))?
printf("Pass"):printf("Fail");
printf("\n----------------------------------------------------------");
main()
{
int sno, m1,m2,m3,m4,m5;
char name[25];
printf("\nEnter the Sno:\n");
scanf("%d",&sno);
printf("\nEnter the Name:\n");
fflush(stdin);
gets(name);
printf("\nEnter the Mark1:\n");
scanf("%d",&m1);
printf("\nEnter the Mark2:\n");
scanf("%d",&m2);
printf("\nEnter the Mark3:\n");
scanf("%d",&m3);
printf("\nEnter the Mark4:\n");
scanf("%d",&m4);
printf("\nEnter the Mark5:\n");
scanf("%d",&m5);
printData(sno,name,m1,m2,m3,m4,m5);
Solve The Following Using User Defined Function (without using library
function)
1. Write a program to check the given String is Palindrome
2. Write a program to count Number of words in a given String
3. Write a program to Find number of Vowels in a given string
4. Write a program to find Number of Lowercase letters and Number of Uppercase letters in a
String
5. Write a program to Print a Triangle pattern of a given string
6. Write a program to count number occurrence of a word from given string
7. Write a program to Change the Case the given string with below listed Option using string
functions
[Link] case
[Link] Case
[Link] case
[Link] case
[Link] case
8. Write a program to Sort the given Names in a 2 Dimension char Array
9. Write a program to merge two strings without using String function
10. Write a program to Find the Maximum occurrence Vowel and consonants in a given
string
11. Write a program to Reverse the String using Function