CP Functions
CP Functions
Rathod
C Programming Functions
Loop are used when we need to repeat group of instructions where as function is
contrast (which may consist loops used for evaluating tasks) can be called more
than once at a any part of program wherever required
Types of C functions
• Library function
Library function
For example:
main() The execution of every C program starts from this main() function.
in that program: one for finding factorial and other for checking whether it is prime
or not.
1. User defined functions helps to decompose the large program into small
segments which makes programmer easy to understand, maintain and debug.
function_name(argument(1),....argument(n));
1. Function declarator:
Function declarator is the first line of function definition. When a function is
called, control of the program is transferred to function declarator.
Syntax of function declaration and declarator are almost same except, there is no
semicolon at the end of declarator and function declarator is followed by
function body.
[204]
Structured Programming Approach Prof: S. Rathod
2. Function body
Function declarator is followed by body of function inside braces.
Arguments that are passed in function call and arguments that are accepted in
function definition should have same data type.
For example:
If argument num1 was of int type and num2 was of float type then, argument
variable a should be of type int and b should be of type float,i.e., type of argument
during function call and function definition should be same.
A function can be called with or without an argument.
Return Statement
Return statement is used for returning a value from function definition to calling
function.
[205]
Structured Programming Approach Prof: S. Rathod
For example:
return a;
return (a+b);
In above example, value of variable add in add() function is returned and that value
is stored in variable sum in main() function. The data type of expression in return
statement should also match the return type of function
Write a C program to add two integers. Make a function add to add integers and
display sum in main() function.
#include <stdio.h>
int main(){
int num1,num2,sum;
scanf("%d %d",&num1,&num2);
printf("sum=%d",sum);
return 0;
int add;
add=a+b;
#include<stdio.h>
#include<conio.h>
void add();
int main()
add();
getch();
return 0;
int a,b,sum;
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“sum=%d”,sum);
[208]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
int add();
int main()
int sum;
sum=add();
printf(“Sum is %d”,sum);
getch();
return 0;
int a,b,c;
scanf(“%d%d”,&a,&b);
c=a+b;
[209]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
int main()
int a,b;
scanf(“%d%d”,&a,&b);
getch();
return 0;
int sum;
sum=x+y;
printf(“sum is=%d”,sum);
[210]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
int main()
int a,b,sum;
scanf(“%d%d”,&a,&b);
sum=add(a,b);
printf(“Sum is %d”,sum);
getch();
return 0;
int z;
z=x+y;
[211]
Structured Programming Approach Prof: S. Rathod
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
display(n);
getch();
int i;
for(i=1;i<=n;i++)
printf("%d\n",i);
[212]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
void main()
int n1,n2,n3;
clrscr();
scanf("%d %d %d",&n1,&n2,&n3);
avg(n1,n2,n3);
getch();
float average;
average=(a+b+c)/3.0;
printf("Average=%f",average);
[213]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
void main()
int no,factorial;
clrscr();
printf("Enter a number:");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial=%d",factorial);
getch();
int i,ans;
for(i=1,ans=1;i<=no;i++)
ans=ans*i;
return ans;
[214]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
void main()
int i,n;
clrscr();
scanf("%d",&n);
mul(n);
getch();
void mul(int n)
int i;
for(i=1;i<=10;i++)
printf("%d X %d = %d\n",n,i,(n*i));
[215]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf("%d",&n);
odd(n);
getch();
void odd(int n)
int i;
for(i=0;i<=n-1;i++)
[216]
Structured Programming Approach Prof: S. Rathod
printf("%d\n",2*i+1);
Write a program to check whether the entered number is prime or not Using
Function.
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
prime(n);
getch();
void prime(int n)
int i=2;
while(n%i!=0)
[217]
Structured Programming Approach Prof: S. Rathod
i++;
if(n==i)
printf("Prime Number");
else
gcd = GCD(no1,no2);
if(gcd==1)
[218]
Structured Programming Approach Prof: S. Rathod
lcm = LCM(no1,no2);
printf("\nLCM = %d",lcm);
getch();
}
int GCD (int no1, int no2)
{
int gcd;
if(no1<no2)
gcd=no1;
else
gcd=no2;
while(no1%gcd!=0 || no2%gcd!=0)
{
gcd--;
}
return gcd;
}
int LCM (int no1, int no2)
{
int lcm;
if(no1>no2)
lcm=no1;
else
lcm=no2;
while(lcm%no1!=0 || lcm%no2!=0)
{
lcm++;
}
return lcm;
[219]
Structured Programming Approach Prof: S. Rathod
}
/* OUTPUT :
Enter two numbers : 16 24
GCD = 8
LCM = 48
*/
int i,ans;
for(i=1,ans=1;i<=no;i++)
[220]
Structured Programming Approach Prof: S. Rathod
ans=ans*i;
return ans;
/* OUTPUT :
Enter the values of n and r : 8 2
nCr = 28
nPr = 56
Call by value
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
[221]
Structured Programming Approach Prof: S. Rathod
int a=5,b=7;
getch()
return 0;
int temp;
temp=p;
p=q;
q=temp;
OUTPUT:
[222]
Structured Programming Approach Prof: S. Rathod
Call by reference
1) While passing parameter using call by address scheme , we are passing the
actual address of the variable to the called function.
2) Any updates made inside the called function will modify the original copy
since we are directly modifying the content of the exact memory location.
Example:
#include<stdio.h>
#include<conio.h>
int main()
int a=5,b=7;
getch()
return 0;
int temp;
temp=*p;
*p=*q;
[223]
Structured Programming Approach Prof: S. Rathod
*q=temp;
OUTPUT:
The name of array in fact itself is a pointer which holds the address of
first location of an array. This is base address
Example:int x[5];
x is name of array it hold first address called base address i.e x[0]
x=&x[0];
#include<stdio.h>
#include<conio.h>
int main()
{
[224]
Structured Programming Approach Prof: S. Rathod
int a[]={3,9,6,12,10};
int result;
clrscr();
getch();
return 0;
int c=0,i;
for(i=0;i<5;i++)
c=c+x[i];
return c;
OUTPUT:
Sum of element=40
[225]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
int max(int,int);
int main()
int a,b,c,d,g;
clrscr();
scanf(“%d%d%d%d”,&a,&b,&c,&d);
g=max(max(a,b),max(c,d));
getch();
return 0;
return (m>n)?m:n;
7,9,15,12
Largest number is 15
[226]
Structured Programming Approach Prof: S. Rathod
C Programming Recursion
A function that calls itself is known as recursive function and this technique is
known as recursion in C programming.
Note: Positive integers are known as natural number i.e. 1,2, 3....n
#include <stdio.h>
int main()
int num,add;
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
int sum(int n)
if(n==0)
return n
else
[227]
Structured Programming Approach Prof: S. Rathod
Output
15
In, this simple C program, sum() function is invoked from the same function. If n
is not equal to 0 then, the function calls itself passing argument 1 less than the
previous argument it was called with. Suppose, n is 5 initially. Then, during next
function calls, 4 is passed to function and the value of argument decreases by 1 in
each recursive call. When, n becomes equal to 0, the value of n is returned
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
[228]
Structured Programming Approach Prof: S. Rathod
=5+4+6
=5+10
=15
Every recursive function must be provided with a way to end the recursion. In this
example when, n is equal to 0, there is no recursive call and recursion ends.
Recursion is more elegant and requires few variables which make program clean.
Recursion can be used to replace complex nesting code by dividing the problem
into same problem of its sub-type.
In other hand, it is hard to think the logic of a recursive function. It is also difficult
to debug the code containing recursion.
#include<stdio.h>
#include<conio.h>
int main()
int n;
[229]
Structured Programming Approach Prof: S. Rathod
scanf("%d",&n);
return 0;
int factorial(int n)
if(n!=1)
return n*factorial(n-1);
Output:
Factorial of 6 = 72
Algorithm:
main()
Step 1: START
Step 3: INPUT n
Step 5: INITIALIZE i = 1
[230]
Structured Programming Approach Prof: S. Rathod
fibo(int f)
Step 1: START
Step 3: RETURN 0.
Step 5: RETURN 1.
main()
[231]
Structured Programming Approach Prof: S. Rathod
fibo (int f)
[232]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
void main()
{ int n, i, term;
clrscr();
scanf("%d",&n);
printf("\nFibonacci Series");
{ term = fibo(i);
getch();
int next = 0 ;
if(f == 1)
return 0;
else if(f == 2)
return 1;
else
return next;
OUTPUT:
Fibonacci Series:
011235
[234]
Structured Programming Approach Prof: S. Rathod
#include<stdio.h>
#include<conio.h>
void main()
int p,x,y;
clrscr();
scanf(“%d%d”,&x,&y);
p=power(x,y);
getch();
if(y==0)
return 1;
else if(y==1)
return x;
else
OUTPUT:
enter x and y 3 4
TYPES OF VARIABLE:
variable is just a named area of storage that can hold a single value (numeric or
character). The C language demands that you declare the name of each variable
that you are going to use and its type, or class, before you actually try to do
anything with it.
The Programming language C has two main variable types
● Local Variables
● Global Variables
Local Variables:
Global variable :
● Global variable is defined at the top of the program file and it can be visible
and modified by any function that may reference it.
● Global variables are initialized automatically by the system when you define
them!
[236]
Structured Programming Approach Prof: S. Rathod
int 0
char '\0'
float 0
pointer NULL
STORAGE CLASSES
A storage class defines the scope (visibility) and life-time of variables and/or
functions within a C Program. They precede the type that they modify. We have
four different storage classes in a C program −
Auto
Register
Static
extern
Automatic variables:
[237]
Structured Programming Approach Prof: S. Rathod
Example:
void main()
{
int detail;
or
auto int detail; //Both are same
}
Register variables:
Keyword to declare register variable
register
Example
register int a;
● Register variables are similar to automatic variable and exists inside that
particular function only.
● If the compiler encounters register variable, it tries to store variable in
microprocessor's register rather than memory.
● Value stored in register are much faster than that of memory.
● Register variable not store the address of variable
Static variables:
A static variable tells the compiler to persist the variable until the end of program.
Instead of creating and destroying a variable every time when it comes into and
goes out of scope, static is initialized only once and remains into existence till the
end of program. A static variable can either be internal or external depending upon
the place of declaration. Scope of internal static variable remains inside the
function in which it is defined. External static variables remain restricted to scope
of file in each they are declared.
They are assigned 0 (zero) as default value by the compiler.
[238]
Structured Programming Approach Prof: S. Rathod
main()
{
test();
test();
test();
}
void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%d\t",a);
}
output :
1 2 3
extern variables:
● External variable can be accessed by any function. They are also known as
global variables. Variables declared outside every function are external
variables.
● In case of large program, containing more than one file, if the global variable
is declared in file 1 and that variable is used in file 2 then, compiler will
show error. To solve this problem, keyword extern is used in file 2 to
indicate that, the variable specified is global variable and declared in another
file.
#include<stdio.h>
void Check();
int a=5; /* a is global variable because it is outside every function */
int main()
{
a+=4;
Check();
[239]
Structured Programming Approach Prof: S. Rathod
return 0;
}
void Check()
{
++a;
printf("a=%d\n",a);
}
Output:
a=10
[240]