User Defined Functions
User Defined Functions
1. Library functions
2. User-defined functions
There are many situations where we might need to write same line
of code for more than once in a program. This may lead to
unnecessary repetition of code, bugs and even becomes boring for
the programmer. So, C language provides an approach in which
you can declare and define a group of statements once in the form
of a function and it can be called and used whenever required.
1. Function Declaration
2. Function Definition
3. Function call
Function Declaration
Example :-
Defining a Function
The general form of a function definition in C programming language
is as follows
return result;
}
Function call
When a function is called, control of the program gets transferred to
the function.
functionName(argument1, argument2,...);
Example : - add(a,b);
categories of functions in c
To understand the above four categories we will see the example of addition
Function with no arguments and no Function with no arguments and a return
return value. value
int Add()
void Add() {
{ int Sum, a = 10, b = 20;
int Sum, a = 10, b = 20; Sum = a + b;
Sum = a + b;
return (sum);
printf("%d",Sum); }
}
void main()
void main() {
{ int z;
Add(); // Function call z=Add(); // Function call
} printf(“%d”,z);
}
Function with arguments and no return Function with arguments and a return
value. value.
int Add(int x, int y )
void Add(int x, int y ) {
{ int Sum;
int Sum; Sum = x + y;
Sum = x + y;
return (Sum);
printf("%d",Sum); }
}
void main()
void main() {
{ int z, a = 10, b = 20;
int a = 10, b = 20 z=Add(a,b); // Function call
Add(a,b); // Function call printf(“%d”,z);
} }
Example :-
Printing n Natural numbers (Identify the category of function ? )
#include< stdio.h>
#include< conio.h>
void nat( int);
void main()
{
int n;
clrscr();
printf("\n Enter n value:");
scanf("%d",&n);
nat(n);
getch();
}
void nat(int n)
{
int i;
for(i=1;i<=n;i++)
printf("%d\t",i);
}
Output:
Enter n value: 5
12345
Example :-
#include< stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n;
clrscr();
printf("\n Enter n:");
scanf("%d",&n);
printf("\n Factorial of the number : %d", fact(n));
getch();
}
int fact(int n)
{
int i,f;
for(i=1,f=1;i<=n;i++)
f=f*i;
return(f);
}
#include< stdio.h>
#include< conio.h>
int sum();
void main()
{
int s;
clrscr();
printf("\n Enter number of elements to be added :");
s=sum();
printf("\n Sum of the elements :%d",p);
getch();
}
int sum()
{
int a[20], i, s=0,n;
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i< n; i++)
scanf("%d",& a[i]);
for(i=0;i< n; i++)
s=s+a[i];
return s;
}
Write a Program That print answer of following series using nested
function :-
12 + 22 +32 …………….n2
Recursion :-
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
}
Factorial of a Number Using Recursion
#include <stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
passing array to the function in c
Example :1
#include <stdio.h>
void display(int age1, int age2)
{
printf("%d\n", age1);
printf("%d\n", age2);
}
int main()
{
int ageArray[] = {2, 8, 4, 12};
// Passing second and third elements to display()
display(ageArray[1], ageArray[2]);
return 0;
}
Passing arrays to functions
#include <stdio.h>
float calculateSum(float age[]);
int main()
{
float result, age[ ] = {23.4, 55, 22.6, 3, 40.5, 18};
// age array is passed to calculateSum()
result = calculateSum(age);
printf("Result = %f", result);
return 0;
}
float calculateSum(float age[ ])
{
float sum = 0.0;
for (int i = 0; i < 6; ++i)
{
sum += age[i];
}
return sum;
}
Passing two-dimensional arrays
#include <stdio.h>
void displayNumbers(int num[2][2]);
int main()
{
int num[2][2], i, j;
printf("Enter 4 numbers:\n");
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
scanf("%d", &num[i][j]);
// passing multi-dimensional array to displayNumbers function
displayNumbers(num);
return 0;
}
Enter 4 numbers:
Displaying:
5
Passing Strings to Functions
Strings can be passed to a function in a similar way as
arrays.
#include <stdio.h>
void displayString(char str[]);
int main()
{
char str[50];
printf("Enter string: ");
gets(str);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output: ");
puts(str);
}
Storage Classes in C
auto: This is the default storage class for all the variables declared inside a function or a block.
Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can
be only accessed within the block/function they have been declared and not outside them (which
defines their scope).
extern: Extern storage class simply tells us that the variable is defined elsewhere and not
within the same block where it is used. Basically, the value is assigned to it in a different block
and this can be overwritten/changed in a different block as well. So an extern variable is nothing
but a global variable initialized with a legal value where it is declared in order to be used elsewhere
static: This storage class is used to declare static variables which are popularly used while
writing programs in C language. Static variables have a property of preserving their value even
after they are out of their scope! Hence, static variables preserve the value of their last use in their
scope. So we can say that they are initialized only once and exist till the termination of the
program.
register: This storage class declares register variables which have the same functionality as
that of the auto variables. The only difference is that the compiler tries to store these variables in
the register of the microprocessor if a free register is available. This makes the use of register
variables to be much faster than that of the variables stored in the memory during the runtime of
the program. If a free register is not available, these are then stored in the memory only.
// A C program to demonstrate different storage
// classes
#include <stdio.h>
void autoStorageClass()
{
printf("--------------------------------");
}
void registerStorageClass()
{
printf("--------------------------------");
}
void externStorageClass()
{
printf("--------------------------------");
}
void staticStorageClass()
{
int i = 0;
printf("\nLoop started:\n");
printf("\nLoop ended:\n");
printf("--------------------------------");
}
int main()
{
// exiting
printf("\n\nStorage Classes demonstrated");
return 0;
}