Immanuel Training Institute proudly presents free software courses
To get certificate Contact: iits.certificate@gmail.com
CHAPTER 1 Introduction and structure of C program
CHAPTER 2 Data types, Constants, Variables and I/O Statements
CHAPTER 3 Operators and Expressions
CHAPTER 6 String Manipulations
CHAPTER 8 Structure, Unions and Enumerated Data types
CHAPTER 7
FUNCTIONS
A function is a self contained block
of statements that perform a coherent task of some kind. A function is a set of
instruction that is used to perform specified tasks which repeatedly occurs in
the main program.
TYPES OF FUNCTIONS
1.
User defined
The functions defined by the user
according to the requirements are called user defined function. Users modify
the function according to their requirement.
Elements
Function
definition
It
is the process of specifying and establishing the user defined function by
specifying all of its elements and characteristics.
Function
declaration
Like
the normal variables in a program, the function can also be before they defined
and invoked
Example
Return_type
Function_name(variable_type variable_name,…..)
int
add( int X,int Y);
Function
call
The
function can be called by simply specifying the name of the function , return
value and parameters it presence.
2.
Predefined
Predefined
functions are functions that are built into C Language to perform
some standard operations. Predefined functions in the Standard Function Library are listed based on different
categories. These functions are already defined in header files (files
with . h extensions are called header files such as stdio. h ), so we just call
them whenever there is a need to use them
PARAMETERS
Parameters provide the data
communication between the calling function and called function.
TYPES OF PARAMETERS
Actual parameters
The parameters are written in function call are known as actual
parameters.
Example:
main()
{
…….
..……
Fun1(a,b);
…….
}
Formal parameters
The parameters are written in function definition are known as formal
parameters.
Example:
Fun1(x,y)
{
…….
……..
….....
}
LOCAL AND
GLOBAL VARIABLE
Local variable
It defined variable use inside function
block. It use particular block.
Ex :-
Value (int a,int b)
{
int c,d;
}
Global
variable:-
It declared before the main()
function called global variable. It uses all the functions of the inside of the
program.
Ex
:-
int
m=5,n=10;
main()
{
int
a,b;
}
PROTOTYPE
The functions are
classified into the following types
defending on whether the
arguments are present or not and whether a value is returned or not. These are
also called function prototype.
Types
1.
Function with
no argument and no return value
In this proto type no data
transfer takes place between the calling function and the called function. i.e.
the called program does not receive data from the calling program. And does not
send back any value to the calling program.
Syntax
:-
main()
{
……
fun1();
….
}
fun1();
{
….
…..
}
2.
Function
with argument and no return value
In this prototype data transfer from
calling function to the called function. i.e. the called program receives some data from the calling program. And does
not send back any value to the
calling program.(one way communication)
Syntax
:-
main()
{
……
fun1(a,b);
….
}
fun1(x,y);
{
….
…..
}
- Function
with argument and return value
In this prototype data transfer takes place between the calling
function and the called function. i.e. the called program receives some data
from the calling program. And send back any value to the calling
program.( 2 way communication)
Syntax
:-
main()
{
……
C=fun1(a,b);
….
}
data_type
fun1(x,y);
{
….
…..return(z);
}
- Function
with no argument and return value
In this prototype is one way
communication takes place. i.e. the calling program cannot pass any arguments
to the called program but , the called program may send some return value to
the calling program.
Syntax
:-
main()
{
……
C=fun1;
….
}
data
type fun1();
{
….
…..return(z);
}
EXAMPLE PROGRAMS:
1. Example program for function with no arguments and no return value (add two numbers):
#include<stdio.h>
#include<conio.h>
void add(void);
int a,b,c;
void main()
{
clrscr();
add();
getch();
}
void add(void)
{
printf("Enter any two nos:");
scanf("%d%d",&a,&b);
c=a+b;
printf("The addition of two nos. is %d",c);
}
2. Example program for function with arguments and no return value (add two numbers):
#include<stdio.h>
#include<conio.h>
void add(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter the a,b value:");
scanf("%d%d",&a,&b);
add(a,b);
getch();
}
void add(x,y)
int x,y;
{
int c;
c=x+y;
printf("\n%d+%d=%d",x,y,c);
}
3. Example program for function with no arguments and with return value, function with arguments and with return value:
#include<stdio.h>
#include<conio.h>
int get(void);
float avg(int,int,int);
void main()
{
int a,b,c;
float d;
clrscr();
printf("Enter the a value:");
a=get();
printf("Enter the b value:");
b=get();
printf("Enter the c value:");
c=get();
d=avg(a,b,c);
printf("\nThe average of given three nos. is %f",d);
getch();
}
int get(void)
{
int x;
scanf("%d",&x);
return(x);
}
float avg(int x,int y,int z)
{
float avg;
avg=(x+y+z)/3.00;
return(avg);
}
4. Write a function to find factorial of a given number:
#include<stdio.h>
#include<conio.h>
main()
{
int f,n;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
f=fact(n);
printf("The factorial value of %d is %d",n,f);
getch();
}
fact(int n)
{
if(n==1)
return(1);
else
return(n*fact(n-1));
}
5. Write a function to print fibonacci series:
#include<stdio.h>
#include<conio.h>
int f1=1,f2=1,f3;
void main()
{
int n;
clrscr();
printf("Enter the no. of lines:");
scanf("%d",&n);
if(n==0||n==1)
goto l1;
printf("%d\n%d\n",f1,f2);
fib(n-2);
l1:getch();
}
fib(int n)
{
if(n==0)
return;
else
{
f3=f1+f2;
printf("%d\n",f3);
f1=f2;
f2=f3;
fib(n-1);
}
}
6. Write a function to swap two numbers:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the a,b value:");
scanf("%d%d",&a,&b);
printf("\nBefore swapping:%d\t%d",a,b);
swap(a,b);
printf("\n%d\t%d",a,b);
getch();
}
swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\nAfter swapping :");
printf("%d\t%d",x,y);
return;
}
7. Write a function to swap two numbers(passing address in function call):
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the a,b value:");
scanf("%d%d",&a,&b);
printf("\nBefore swapping:%d\t%d",a,b);
swap(&a,&b);
printf("\nAfter swapping :%d\t%d",a,b);
getch();
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
return;
}
8. Write a program to print numbers in ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the values one by one:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(n,a);
printf("The Ascending order is:\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
getch();
}
sort(m,x)
int m,x[];
{
int i,j,t;
for(i=1;i<=m;i++)
{
for(j=i;j<=m;j++)
{
if(x[i]>x[j])
{
t=x[i];
x[i]=x[j];
x[j]=t;
}
}
}
return;
}
9. Example program for a function call in an another function block:
#include<stdio.h>
#include<conio.h>
float rat(int,int,int);
int diff(int,int);
void main()
{
int x,y,z;
clrscr();
printf("Enter the three values:");
scanf("%d%d%d",&x,&y,&z);
printf("%f",rat(x,y,z));
getch();
}
float rat(int a,int b,int c)
{
if(diff(b,c))
return(a/(b-c));
else
return;
}
int diff(int m,int n)
{
if(m!=n)
return(1);
else
return(0);
}
10. Example program for a function call in an another function call:
#include<stdio.h>
#include<conio.h>
float rat(int,int,int);
int diff(int,int);
void main()
{
clrscr();
printf("The answer is :\n");
printf("\n%f",rat(x(),y(),z()));
getch();
}
x()
{
return(10);
}
y()
{
return(4);
}
z()
{
return(2);
}
float rat(int a,int b,int c)
{
if(diff(b,c))
return(a/(b-c));
else
return(0);
}
int diff(int m,int n)
{
if(m!=n)
return(1);
else
return(0);
}
11. Example for predefined function(math.h)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,x,y,ans,no;
float b,c,d,e,f;
clrscr();
printf("Enter the a,x,y,no value:");
scanf("%d%d%d%d",&a,&x,&y,&no);
printf("Enter the c,d,e,f:");
scanf("%f%f%f%f",&c,&d,&e,&f);
b=sqrt(a);
printf("\nThe square root of the %d is %f",a,b);
ans=pow(x,y);
printf("\nx^y :%d",ans);
printf("\nThe absolute value of a is %d",abs(no));
printf("\nsinx:%f",sin(c));
printf("\ncosx:%f",cos(d));
printf("\ntanx:%f",tan(e));
printf("\nlogx:%f",log(f));
getch();
}
12. Example program for predefined function(ctype.h)
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ans;
clrscr();
printf("Enter the character:");
flushall();
scanf("%c",&ans);
printf("\nThe ascii value of given character is %d",toascii(ans));
printf("\nThe uppercase of the given character is %c",toupper(ans));
printf("\nThe lowercase of the given character is %c",tolower(ans));
printf("\nEnter the character:");
flushall();
scanf("%c",&ans);
if(isalpha(ans))
printf("\nThe given data is character:");
else
printf("\nThe given data is not a character");
if(islower(ans))
printf("\nThe given character is in lowercase");
if(isupper(ans))
printf("\nThe given character is in uppercase");
getch();
}