SlideShare a Scribd company logo
1 of 72
Unit IV
Strings and Functions
L.NIVETHA AP /CSE - KNCET
Strings – Character Arrays – Reading
String input – String Library Functions – List
of Strings – Command Line Arguments –
Functions: Types – Declaration – Definition –
Function Call – Pass by Value – Pass by
Reference – Passing arrays to functions –
Recursion – Pointer to function.
L.NIVETHA AP /CSE - KNCET
STRINGS and its Operations
Presented by,
L.Nivetha,AP/CSE,
KNCET
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
(AUTONOMOUS)
NAMAKKAL - TRICHY MAIN ROAD, THOTTIAM, TRICHY-621 215
L.NIVETHA AP/CSE,KNCET
Strings
strings are defined as sequence of character
enclosed with in double quotes.
every string literal constant is automatically
terminated by null character. ie : ‘0’.
string literal also store into memory like normal
variable.
Here additional one byte needed for storing null
character
Example:
“hai” h a i 0
L.NIVETHA AP /CSE - KNCET
The length of the string is calculated based on
the number character present in the string,
The null character not counted with in the
string length
L.NIVETHA AP /CSE - KNCET
Character array
Character variable can be store character
constant ex: char a=‘s’
if we store string constant we need to
declare the character variable as array. Ex:
char a[10]=“ram”
Syntax:
char identifier[size_specifier]=“string_literal”
L.NIVETHA AP /CSE - KNCET
To declare the string variable into character
array
To initialize the string constant into two ways:
1. By using string literal constant
Here we assign the string constant into
character array variable using double quotes
Ex:char a[10]=“RAM”;
2. By using initializing the list
Here to initialize the character array by using
list of character intilizer.
Ex: char a[10]={’r , ’a’ , ’m’ , ’0’};
L.NIVETHA AP /CSE - KNCET
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
char name[5]=“ram”;
char initial=‘s’;
printf(“%c.%s”, initial,name);
}
Output: s.ram
L.NIVETHA AP /CSE - KNCET
Reading string input:
 To read the string input by using scanf()
statement with %s control string.
Example:
#include<stdio.h>
void main()
{
char name[10];
printf("Enter the name ");
scanf("%s", &name);
printf("name is:%s", name);
}
L.NIVETHA AP /CSE - KNCET
STRING OPERATIONS
STRING HANDLING FUCTION’S:
1. strlen()
2. Strcmp()
3. strcpy()
4. Strncpy()
5. Strcat()
6. Strdup()
7. Strrev()
8. Strlwr()
9. Strupr()
10.Strpbrk() L.NIVETHA AP /CSE - KNCET
STRING HANDLING FUCTION’S
1. strlen() - It is a predefined function ,used to find the length
of the string. It will return integer output.
syntax:
variable=strlen(string);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30]=“sriram”;
int a;
clrscr();
a=strlen(name);
printf(“%d”,a);
} o/p: 6
L.NIVETHA AP /CSE - KNCET
Strcmp() function –
It is a predefined string function.
it is used to compare character of two strings ,
if any difference it provide how many
character to be differed , if no difference to
give 0 as output.
Syntax:
variable=strcmp(string1 , string2);
L.NIVETHA AP /CSE - KNCET
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“sriram” , name2[30]= “balram”;
int a,b;
clrscr();
a=strcmp(name1, “sriram”);
b=strcmp(name1 , name2);
printf(“%d%d”, a,b);
}
o/p: 0 3
L.NIVETHA AP /CSE - KNCET
strcpy() function – it is a predefined string
function. used to copy the content of one
string to another.
syntax:
strcpy(string1,string2);
string1 – destination string
string2 - source string
L.NIVETHA AP /CSE - KNCET
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“sriram” , name2[30];
clrscr();
strcpy(name2 , name1);
printf(“%s”, name2);
}
o/p:
sriram L.NIVETHA AP /CSE - KNCET
Strncpy() function – used to copy first n characters
from one string into another.
Syntax:
strncpy(string1 , string2 ,n);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“sriram” , name2[30];
clrscr();
strncpy(name2 , name1 , 3);
printf(“%s”, name2);
}
o/p:
sri L.NIVETHA AP /CSE - KNCET
Strcat() function – it is used to combine two strings ,it is also
called as string concatenation.
Syntax:
strcat(string1 , string2);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“sriram” , name2[30]= “Balram”;
clrscr();
strcat(name1 , name2);
printf(“%s”, name1);
}
o/p:
sriramBalram L.NIVETHA AP /CSE - KNCET
Strdup() function: It is used same as well as strcpy() , here it will create
a duplicate copy of source string .
Syntax:
strdup(string);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“sriram”;
clrscr();
char *name2=strdup(name1);
printf(“%s”, name2);
}
o/p: sriram
L.NIVETHA AP /CSE - KNCET
Strrev() function – it is a predefined string function, it is
used to reverse the given string .
Syntax:
strrev(string);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“sriram”;
clrscr();
strrev(name1);
printf(“%s”, name1);
}
o/p: marirs L.NIVETHA AP /CSE - KNCET
Strlwr() function – it is a predefined string function , it is used
to convert the given strings into lower case.
Syntax:
strlwr(string);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“SRIRAM”;
clrscr();
strlwr(name1);
printf(“%s”, name1);
}
o/p: sriram
L.NIVETHA AP /CSE - KNCET
Strupr() function – it is a predefined string function , it is used
to convert the given strings into upper case.
Syntax:
strupr(string);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]= “sriram”;
clrscr();
strupr(name1);
printf(“%s”, name1);
}
o/p: SRIRAM
L.NIVETHA AP /CSE - KNCET
Strpbrk() function - it is a predefined string function , it break
the given source string into small sub string based on
pointer break
Syntax:
strpbrk(string1, string2);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[30]=“welcome”, *p;
clrscr();
p=strpbrk(name1, ‘l’);
printf(“%s”, p);
}
o/p: come L.NIVETHA AP /CSE - KNCET
List of strings
 List of strings is defined as collection of
strings stored in a character.
 you must declare those character variable by
using two dimensional array.
A list of strings stored in two ways:
1.Using an array of strings
2.Using an array of character pointer
L.NIVETHA AP /CSE - KNCET
Array of strings:
If an application requires to store multiple
strings , an array of strings can be used to
store them.
Here we can store multiple strings using
two dimensional array
L.NIVETHA AP /CSE - KNCET
Declaration of array of string:
The general form of array of strings is:
<storage class specifier> <Type modifier>
<Type Qualifier> char identifier[<Row_
Specifier>][<Coloumn_Specifier>]=
<Initialization of list of string >
Here the content specified in angular
bracket are optional <>, content in bold letter
are mandatory (char identifier name [][])
L.NIVETHA AP /CSE - KNCET
Initialization of array of string :
Array of string initialized in two ways:
1. Using string literal constant:
using string literal constant array of string
can be initialized as
char str[][]= {
“ram”,
“mani”
“ramesh”
};
L.NIVETHA AP /CSE - KNCET
Using list of character initializes:
Using list of character initializer,
an array of string is initialized as.
char str[][]={
{‘r’,’a’,’m’,’0’},
{‘m’,’a’,’n’,’i’,’0’}
};
L.NIVETHA AP /CSE - KNCET
Reading list of string from the terminal
Here we read list of strings by using scanf()
statement.
Example:
char name[10][20];
for(i=0;i<5;i++)
{
scanf(“%s”, &name[i]);
}
L.NIVETHA AP /CSE - KNCET
Array of character pointer
array of string also stored in array of
character pointer.
Example:
char *language[]={“java” ,”python”, ”c++”};
Here we store the string by using array of
character pointer ,
also you cannot specify the array size also
not need to declare two dimensional array.
L.NIVETHA AP /CSE - KNCET
Command Line Argument
 Here the inputs are given to the function by means of
argument.
 by using main function to give the inputs.
 Inputs of the main function given by making use of
special argument known as command line argument.
 In header of the function main , having two parameter.
Example:
main(int argc, char *argv[])
{
statement
…………..
}
L.NIVETHA AP /CSE - KNCET
argc – the parameter argc sends argument
count , it is in integer type
argv- the parameter argv stands for argument
vector and it is in array of character pointer.
the command line argument are used in the
applications that involve file handling.
L.NIVETHA AP /CSE - KNCET
Example:comline.c
#include<stdio.h>
int main(int argc , char** argv[])
{
int i=0;
printf("The number of argument are %d", argc);
printf("The argument are ");
for(i=0;i<argc;i++)
printf("%s", argv[i]);
}
L.NIVETHA AP /CSE - KNCET
Command prompt
C:tcbin>comline source.txt dest.txt
The number of argument are 3
Argument are
C:tcbin>comline.exe
Source.txt
Dest.txt
L.NIVETHA AP /CSE - KNCET
Function
A function is a set of instructions that are
used to perform specified tasks.
By using function we can divide complex
task into manageable tasks.
Also helps to avoid duplication of work .
Easy to debug and Testing & also improve
maintainability.
L.NIVETHA AP /CSE - KNCET
Two types of function:
1. User defined function.
2. Pre-defined function. – The library function are
predefined functions. Those functionality has already
been defined. user will use those function but cannot
modify them.
1. User defined function:
The function defined by users according to their
requirement those functions are called user defined
function.
Elements of user defined function:
1.Function declaration
2. Function call
3. Function definition
L.NIVETHA AP /CSE - KNCET
Function declaration
All the function need to be declared before
they are used
The general form of declaration is :
[return data type] function name(parameters
types );
Here return data type is optional , the
terms in bold letter is mandatory (function
name with () and end with semicolon)
L.NIVETHA AP /CSE - KNCET
Function declaration
Parameter list type is separated by comma.
The parameter type can be any type(int , float,
*int,….)
also the parameter is optional one. based on
the prototype we specify the parameter.
To specify the parameter combination of
complete parameter declaration and abstract
parameter declaration is allowed.
Example:
int add( int, int );
int add(int x, int y );
int add(int , int y);
L.NIVETHA AP /CSE - KNCET
Function call
function call is the process of calling the
well defined function
General form of function call is:
function_name(actual parameter);
Here function name with paranthesis ()
and end with semicolon is mandatory.
parameter is optional. based on the
parameter present in the function declaration
to give parameter value with in the function
call.
L.NIVETHA AP /CSE - KNCET
Example:
#include<stdio.h>
Void main()
{
int add(int , int); function declaration
………………..
………………..
add(10, 20); function call
}
L.NIVETHA AP /CSE - KNCET
Function definition
function definition is also known as function
implementation.
Every function definition consist of :
1.Header of the function
2.Body of the function
L.NIVETHA AP /CSE - KNCET
Header of the function:
General form of header of the function is:
[return data type] function name([parameter
list])
Here the terms enclosed in square bracket
is optional, the terms shown in bold letter is
mandatory.
the header function can have only
complete parameter declaration.
here we cannot have abstract parameter
declaration.
L.NIVETHA AP /CSE - KNCET
Body of the function:
The body of the function consist of set of
statement enclosed with in braces.
The body of the function can have
executable and non-executable statement .
The non-executable statement present
before the executable statement.
Here only to implement logic of the
program.
L.NIVETHA AP /CSE - KNCET
Function definition
Syntax:
return data type function name(parameter)
{
body of the function;
……………….
…………
return statement;
}
L.NIVETHA AP /CSE - KNCET
#include<stdio.h>
#include<conio.h>
void main()
{
int add(int , int);
int k;
clrscr();
k=add(10,20); // function call
printf(“The value of k is %d”, k);
getch();
}
Int add(int x , int y) //function definition
{
int z;
z= x+y;
return(z);
}
o/p:
The value of k is 30
L.NIVETHA AP /CSE - KNCET
Types of parameter:
Parameter are called variable or value passed by
the function, there are two types:
Actual and formal parameter.
Actual parameter:
Actual parameter is present in the function call, it
may be value or reference of the variable
Example:
main()
{
int a , b;
int add(int , int ); function declaration
……. Actual parameter
…….
add(a,b); function call
} L.NIVETHA AP /CSE - KNCET
Formal parameter:
It is present in the function definition
header.it is also called as dummy parameter.
Function definition: Formal parameter
int add(int x , int y)
{
body of the statement;
……………..
……………….
return statement ;
}
L.NIVETHA AP /CSE - KNCET
Function call/function invocation
depending upon the input and output it is
classified as.
1.Function with no input and no output.
2.Function with input and output.
3.Function with input and no output.
4.Function no input and with output.
L.NIVETHA AP /CSE - KNCET
Function with no argument and no return value
Syntax:
void main()
{
Void function_name(void);
statement
……….
function_name ();
}
void function_name ()
{
………..
statement
}
L.NIVETHA AP /CSE - KNCET
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
void message(void); function declaration
message( ); function call
printf(“welcome”);
}
void message() function definition
{
printf(“hello”);
}
o/p: hello welcomeL.NIVETHA AP /CSE - KNCET
Function with argument and with return value
Syntax:
void main()
{
int function_name (int , int );
statement;
……….
c=function_name (v1,v2);
…….
}
int function_name (int v1,int v2)
{
………..
return(z);
}
L.NIVETHA AP /CSE - KNCET
#include<stdio.h>
#include<conio.h>
void main()
{
int add(int , int);
int k;
clrscr();
k=add(10,20);
printf(“The value of k is %d”, k);
}
Int add(int x , int y)
{
int z;
z= x+y;
return(z);
}
o/p:
The value of k is 30
L.NIVETHA AP /CSE - KNCET
Function with argument and no return value
Syntax:
void main()
{
void function_name(int, int );
………
……….
function_name (v1,v2);
…….
}
void function_name (int v1,int v2)
{
………..
statement
}
L.NIVETHA AP /CSE - KNCET
#include<stdio.h>
#include<conio.h>
void main()
{
void add(int , int);
clrscr();
add(10,20);
}
void add(int x , int y)
{
int z;
z= x+y;
printf(“Result is: %d”, z);
}
o/p:
Result is: 30
L.NIVETHA AP /CSE - KNCET
Function with out argument and with return value:
Syntax:
void main()
{
int function_name(void);
………
……….
c=function_name (void);
…….
}
int function_name ()
{
………..
statement
return();
} L.NIVETHA AP /CSE - KNCET
#include<stdio.h>
#include<conio.h>
void main()
{
int add(void);
int c;
clrscr();
c=add();
printf(“The value of c is %d”, c);
getch();
}
int add()
{
int x=10 , y=20 , z;
z= x+y;
return(z);
}
o/p:
The value of c is 30 L.NIVETHA AP /CSE - KNCET
Parameter passing method in function
If a function is called ,the calling function
to pass some values to the called function.
There are two ways to pass the parameter :
1.Call by value (or) pass by value
This will pass the values from calling
function to called function
Here it will copy the actual parameter
value into formal parameter.
2. Call by reference (or) pass by reference
L.NIVETHA AP /CSE - KNCET
Call by value (or) pass by value
#include<stdio.h>
#include<conio.h>
Void main()
{
void swap(int , int );
int a , b ;
clrscr();
a=10;
b=20;
printf ( “Before swap values are
%d%d” , a, b );
swap(a,b) ;
printf ( “After swap values are
%d%d” , a, b );
getch();
}
void swap(int x , int
y)
{
int temp;
temp = x;
x = y;
y = temp;
printf ( “In swap
function values are
%d%d” , x , y );
}
o/p:
10 , 20
20 , 10
10 , 20
L.NIVETHA AP /CSE - KNCET
2. Call by reference (or) pass by reference
This will pass the parameter address or
reference from calling function to called
function
Here it will copy the actual parameter
address into formal parameter.
If the arguments are passed through
reference the changes made in formal
parameter it will affect actual parameter.
L.NIVETHA AP /CSE - KNCET
Call by reference (or) pass by
reference
#include<stdio.h>
#include<conio.h>
Void main()
{
int swap(int * , int *);
int a , b ;
clrscr();
a=10;
b=20;
printf ( “Before swap
values are %d%d” ,
a, b );
swap( &a, &b) ;
printf ( “After swap
values are %d%d” , a, b );
getch();
int swap(int *x , int * y)
{
int temp;
temp = * x;
* x = * y;
* y = temp;
printf ( “In swap function values
are %d%d” , * x , * y );
}
o/p:
10 , 20
20 , 10
20 , 10
L.NIVETHA AP /CSE - KNCET
Recursion
Recursion is a process by which a function
calls itself repeatedly until some specified
condition has been specified.
Recursion can be used to solve the similar
problem of smaller size.
Recursion is a technique that breaks a
problem into one or more sub problems that
are similar to the original problem.
L.NIVETHA AP /CSE - KNCET
Recursion is classified as
1. Direct and indirect recursion
2. Tail and non tail recursion.
Direct recursion:
If a function is directly recursive then it is
calls itself.
Indirect recursion.
Indirect recursion occurs when a function
calls another function
L.NIVETHA AP /CSE - KNCET
#include<stdio.h>
#include<conio.h>
Void main()
{
int fact (int);
int n = 5 , re ;
clrscr();
re = fact(n);
printf(“%d ”, re);
}
int fact( int x)
{
if( x==1)
return(1);
else
return( x* fact(x-1));
}
L.NIVETHA AP /CSE - KNCET
2. Tail recursion / non tail.
Tail recursion:
In tail recursion last operation of a function
is a recursively called again and again, here
no pending operation to be performed from
the recursive call
L.NIVETHA AP /CSE - KNCET
#include<stdio.h>
#include<conio.h>
Void main()
{
int fact (int , int );
int n =5 , re;
clrscr();
re = fact( n , 1 );
printf(“%d ”, re);
}
int fact( int x , int result)
{
if( x==1)
return (result) ;
else
return( fact( x-1 , x*result));
}
L.NIVETHA AP /CSE - KNCET
Non Tail recursion:
In non tail recursion it is like normal
recursive function it is called itself again and
again, here pending operation to be
performed from the recursive call
L.NIVETHA AP /CSE - KNCET
Passing arrays to functions
like simple variable , arrays can also be
passed to functions
There are two types:
1.Passing individual element of an array one by
one.
2. Passing the entire array at a time.
L.NIVETHA AP /CSE - KNCET
Passing individual element of an array:
 Individual elements in an array can be passed
by value or by reference,
 However this way of passing values not
preferable.
 because the elements in an array is large,
then we will pass entire element will take
large number of function calls.
 here we will pass individual element in each
function call.
L.NIVETHA AP /CSE - KNCET
Passing one dimensional array to function:
To pass the one dimensional array by one
by one or entire value by using reference.
L.NIVETHA AP /CSE - KNCET
#include<stdio.h> //pass by value
void main()
{
int sum_array(int, int );
int arr[10], n,i,sum=0;
n=5;
printf("enter array element ");
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
for(i=0;i<n;i++)
{
sum=sum_array(arr[i],sum);
}
printf("sum is %d", sum);
}
6 35
int sum_array(int element , int sum)
{
return((sum+element));
}
35+6 L.NIVETHA AP /CSE - KNCET
#include<stdio.h> //pass by reference
void main()
{
int sum_array(int, int );
int arr[10], n,i,sum=0;
n=5;
printf("enter array element ");
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
for(i=0;i<n;i++)
{
sum=sum_array(&arr[i],sum);
}
printf("sum is %d", sum);
}
int sum_array(int *element,int sum)
{
return((sum + *element));
}
L.NIVETHA AP /CSE - KNCET
Passing the entire array at a time
passing entire array at a time is a preferred
way of passing arrays to functions,
Here we pass the entire array by using
reference or value.
L.NIVETHA AP /CSE - KNCET
#include<stdio.h>
void main()
{
void display(int[]);
int a[10],i;
printf("Enter the array elementn");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
display(a);
}
void display(int a[])
{
int i;
printf("the array elements are");
for(i=0;i<5;i++)
{
printf("%dn",a[i]);
}
}
L.NIVETHA AP /CSE - KNCET

More Related Content

Similar to U4.ppt

Strings in c
Strings in cStrings in c
Strings in cvampugani
 
Variadic functions
Variadic functionsVariadic functions
Variadic functionsramyaranjith
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
STRING FUNCTION - Programming in C.pptx
STRING FUNCTION -  Programming in C.pptxSTRING FUNCTION -  Programming in C.pptx
STRING FUNCTION - Programming in C.pptxIndhu Periys
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsRai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-aneebkmct
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsRai University
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and stringsRai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsRai University
 

Similar to U4.ppt (20)

[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
 
Array &strings
Array &stringsArray &strings
Array &strings
 
Strings in c
Strings in cStrings in c
Strings in c
 
Lecture 05 2017
Lecture 05 2017Lecture 05 2017
Lecture 05 2017
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
STRING FUNCTION - Programming in C.pptx
STRING FUNCTION -  Programming in C.pptxSTRING FUNCTION -  Programming in C.pptx
STRING FUNCTION - Programming in C.pptx
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Unit 2
Unit 2Unit 2
Unit 2
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Strings
StringsStrings
Strings
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 

More from Kongunadu College of Engineering and Technology (18)

Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
UNIT 4.pptx
UNIT 4.pptxUNIT 4.pptx
UNIT 4.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
UNIT 3.pptx
UNIT 3.pptxUNIT 3.pptx
UNIT 3.pptx
 
Unit - 1.ppt
Unit - 1.pptUnit - 1.ppt
Unit - 1.ppt
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
Unit - 2.ppt
Unit - 2.pptUnit - 2.ppt
Unit - 2.ppt
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
 
Unit - 4.ppt
Unit - 4.pptUnit - 4.ppt
Unit - 4.ppt
 
U1.ppt
U1.pptU1.ppt
U1.ppt
 
U2.ppt
U2.pptU2.ppt
U2.ppt
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 

Recently uploaded

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 

U4.ppt

  • 1. Unit IV Strings and Functions L.NIVETHA AP /CSE - KNCET
  • 2. Strings – Character Arrays – Reading String input – String Library Functions – List of Strings – Command Line Arguments – Functions: Types – Declaration – Definition – Function Call – Pass by Value – Pass by Reference – Passing arrays to functions – Recursion – Pointer to function. L.NIVETHA AP /CSE - KNCET
  • 3. STRINGS and its Operations Presented by, L.Nivetha,AP/CSE, KNCET KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY (AUTONOMOUS) NAMAKKAL - TRICHY MAIN ROAD, THOTTIAM, TRICHY-621 215 L.NIVETHA AP/CSE,KNCET
  • 4. Strings strings are defined as sequence of character enclosed with in double quotes. every string literal constant is automatically terminated by null character. ie : ‘0’. string literal also store into memory like normal variable. Here additional one byte needed for storing null character Example: “hai” h a i 0 L.NIVETHA AP /CSE - KNCET
  • 5. The length of the string is calculated based on the number character present in the string, The null character not counted with in the string length L.NIVETHA AP /CSE - KNCET
  • 6. Character array Character variable can be store character constant ex: char a=‘s’ if we store string constant we need to declare the character variable as array. Ex: char a[10]=“ram” Syntax: char identifier[size_specifier]=“string_literal” L.NIVETHA AP /CSE - KNCET
  • 7. To declare the string variable into character array To initialize the string constant into two ways: 1. By using string literal constant Here we assign the string constant into character array variable using double quotes Ex:char a[10]=“RAM”; 2. By using initializing the list Here to initialize the character array by using list of character intilizer. Ex: char a[10]={’r , ’a’ , ’m’ , ’0’}; L.NIVETHA AP /CSE - KNCET
  • 8. Example: #include<stdio.h> #include<conio.h> Void main() { char name[5]=“ram”; char initial=‘s’; printf(“%c.%s”, initial,name); } Output: s.ram L.NIVETHA AP /CSE - KNCET
  • 9. Reading string input:  To read the string input by using scanf() statement with %s control string. Example: #include<stdio.h> void main() { char name[10]; printf("Enter the name "); scanf("%s", &name); printf("name is:%s", name); } L.NIVETHA AP /CSE - KNCET
  • 10. STRING OPERATIONS STRING HANDLING FUCTION’S: 1. strlen() 2. Strcmp() 3. strcpy() 4. Strncpy() 5. Strcat() 6. Strdup() 7. Strrev() 8. Strlwr() 9. Strupr() 10.Strpbrk() L.NIVETHA AP /CSE - KNCET
  • 11. STRING HANDLING FUCTION’S 1. strlen() - It is a predefined function ,used to find the length of the string. It will return integer output. syntax: variable=strlen(string); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[30]=“sriram”; int a; clrscr(); a=strlen(name); printf(“%d”,a); } o/p: 6 L.NIVETHA AP /CSE - KNCET
  • 12. Strcmp() function – It is a predefined string function. it is used to compare character of two strings , if any difference it provide how many character to be differed , if no difference to give 0 as output. Syntax: variable=strcmp(string1 , string2); L.NIVETHA AP /CSE - KNCET
  • 13. Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“sriram” , name2[30]= “balram”; int a,b; clrscr(); a=strcmp(name1, “sriram”); b=strcmp(name1 , name2); printf(“%d%d”, a,b); } o/p: 0 3 L.NIVETHA AP /CSE - KNCET
  • 14. strcpy() function – it is a predefined string function. used to copy the content of one string to another. syntax: strcpy(string1,string2); string1 – destination string string2 - source string L.NIVETHA AP /CSE - KNCET
  • 15. Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“sriram” , name2[30]; clrscr(); strcpy(name2 , name1); printf(“%s”, name2); } o/p: sriram L.NIVETHA AP /CSE - KNCET
  • 16. Strncpy() function – used to copy first n characters from one string into another. Syntax: strncpy(string1 , string2 ,n); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“sriram” , name2[30]; clrscr(); strncpy(name2 , name1 , 3); printf(“%s”, name2); } o/p: sri L.NIVETHA AP /CSE - KNCET
  • 17. Strcat() function – it is used to combine two strings ,it is also called as string concatenation. Syntax: strcat(string1 , string2); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“sriram” , name2[30]= “Balram”; clrscr(); strcat(name1 , name2); printf(“%s”, name1); } o/p: sriramBalram L.NIVETHA AP /CSE - KNCET
  • 18. Strdup() function: It is used same as well as strcpy() , here it will create a duplicate copy of source string . Syntax: strdup(string); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“sriram”; clrscr(); char *name2=strdup(name1); printf(“%s”, name2); } o/p: sriram L.NIVETHA AP /CSE - KNCET
  • 19. Strrev() function – it is a predefined string function, it is used to reverse the given string . Syntax: strrev(string); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“sriram”; clrscr(); strrev(name1); printf(“%s”, name1); } o/p: marirs L.NIVETHA AP /CSE - KNCET
  • 20. Strlwr() function – it is a predefined string function , it is used to convert the given strings into lower case. Syntax: strlwr(string); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“SRIRAM”; clrscr(); strlwr(name1); printf(“%s”, name1); } o/p: sriram L.NIVETHA AP /CSE - KNCET
  • 21. Strupr() function – it is a predefined string function , it is used to convert the given strings into upper case. Syntax: strupr(string); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]= “sriram”; clrscr(); strupr(name1); printf(“%s”, name1); } o/p: SRIRAM L.NIVETHA AP /CSE - KNCET
  • 22. Strpbrk() function - it is a predefined string function , it break the given source string into small sub string based on pointer break Syntax: strpbrk(string1, string2); Example: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[30]=“welcome”, *p; clrscr(); p=strpbrk(name1, ‘l’); printf(“%s”, p); } o/p: come L.NIVETHA AP /CSE - KNCET
  • 23. List of strings  List of strings is defined as collection of strings stored in a character.  you must declare those character variable by using two dimensional array. A list of strings stored in two ways: 1.Using an array of strings 2.Using an array of character pointer L.NIVETHA AP /CSE - KNCET
  • 24. Array of strings: If an application requires to store multiple strings , an array of strings can be used to store them. Here we can store multiple strings using two dimensional array L.NIVETHA AP /CSE - KNCET
  • 25. Declaration of array of string: The general form of array of strings is: <storage class specifier> <Type modifier> <Type Qualifier> char identifier[<Row_ Specifier>][<Coloumn_Specifier>]= <Initialization of list of string > Here the content specified in angular bracket are optional <>, content in bold letter are mandatory (char identifier name [][]) L.NIVETHA AP /CSE - KNCET
  • 26. Initialization of array of string : Array of string initialized in two ways: 1. Using string literal constant: using string literal constant array of string can be initialized as char str[][]= { “ram”, “mani” “ramesh” }; L.NIVETHA AP /CSE - KNCET
  • 27. Using list of character initializes: Using list of character initializer, an array of string is initialized as. char str[][]={ {‘r’,’a’,’m’,’0’}, {‘m’,’a’,’n’,’i’,’0’} }; L.NIVETHA AP /CSE - KNCET
  • 28. Reading list of string from the terminal Here we read list of strings by using scanf() statement. Example: char name[10][20]; for(i=0;i<5;i++) { scanf(“%s”, &name[i]); } L.NIVETHA AP /CSE - KNCET
  • 29. Array of character pointer array of string also stored in array of character pointer. Example: char *language[]={“java” ,”python”, ”c++”}; Here we store the string by using array of character pointer , also you cannot specify the array size also not need to declare two dimensional array. L.NIVETHA AP /CSE - KNCET
  • 30. Command Line Argument  Here the inputs are given to the function by means of argument.  by using main function to give the inputs.  Inputs of the main function given by making use of special argument known as command line argument.  In header of the function main , having two parameter. Example: main(int argc, char *argv[]) { statement ………….. } L.NIVETHA AP /CSE - KNCET
  • 31. argc – the parameter argc sends argument count , it is in integer type argv- the parameter argv stands for argument vector and it is in array of character pointer. the command line argument are used in the applications that involve file handling. L.NIVETHA AP /CSE - KNCET
  • 32. Example:comline.c #include<stdio.h> int main(int argc , char** argv[]) { int i=0; printf("The number of argument are %d", argc); printf("The argument are "); for(i=0;i<argc;i++) printf("%s", argv[i]); } L.NIVETHA AP /CSE - KNCET
  • 33. Command prompt C:tcbin>comline source.txt dest.txt The number of argument are 3 Argument are C:tcbin>comline.exe Source.txt Dest.txt L.NIVETHA AP /CSE - KNCET
  • 34. Function A function is a set of instructions that are used to perform specified tasks. By using function we can divide complex task into manageable tasks. Also helps to avoid duplication of work . Easy to debug and Testing & also improve maintainability. L.NIVETHA AP /CSE - KNCET
  • 35. Two types of function: 1. User defined function. 2. Pre-defined function. – The library function are predefined functions. Those functionality has already been defined. user will use those function but cannot modify them. 1. User defined function: The function defined by users according to their requirement those functions are called user defined function. Elements of user defined function: 1.Function declaration 2. Function call 3. Function definition L.NIVETHA AP /CSE - KNCET
  • 36. Function declaration All the function need to be declared before they are used The general form of declaration is : [return data type] function name(parameters types ); Here return data type is optional , the terms in bold letter is mandatory (function name with () and end with semicolon) L.NIVETHA AP /CSE - KNCET
  • 37. Function declaration Parameter list type is separated by comma. The parameter type can be any type(int , float, *int,….) also the parameter is optional one. based on the prototype we specify the parameter. To specify the parameter combination of complete parameter declaration and abstract parameter declaration is allowed. Example: int add( int, int ); int add(int x, int y ); int add(int , int y); L.NIVETHA AP /CSE - KNCET
  • 38. Function call function call is the process of calling the well defined function General form of function call is: function_name(actual parameter); Here function name with paranthesis () and end with semicolon is mandatory. parameter is optional. based on the parameter present in the function declaration to give parameter value with in the function call. L.NIVETHA AP /CSE - KNCET
  • 39. Example: #include<stdio.h> Void main() { int add(int , int); function declaration ……………….. ……………….. add(10, 20); function call } L.NIVETHA AP /CSE - KNCET
  • 40. Function definition function definition is also known as function implementation. Every function definition consist of : 1.Header of the function 2.Body of the function L.NIVETHA AP /CSE - KNCET
  • 41. Header of the function: General form of header of the function is: [return data type] function name([parameter list]) Here the terms enclosed in square bracket is optional, the terms shown in bold letter is mandatory. the header function can have only complete parameter declaration. here we cannot have abstract parameter declaration. L.NIVETHA AP /CSE - KNCET
  • 42. Body of the function: The body of the function consist of set of statement enclosed with in braces. The body of the function can have executable and non-executable statement . The non-executable statement present before the executable statement. Here only to implement logic of the program. L.NIVETHA AP /CSE - KNCET
  • 43. Function definition Syntax: return data type function name(parameter) { body of the function; ………………. ………… return statement; } L.NIVETHA AP /CSE - KNCET
  • 44. #include<stdio.h> #include<conio.h> void main() { int add(int , int); int k; clrscr(); k=add(10,20); // function call printf(“The value of k is %d”, k); getch(); } Int add(int x , int y) //function definition { int z; z= x+y; return(z); } o/p: The value of k is 30 L.NIVETHA AP /CSE - KNCET
  • 45. Types of parameter: Parameter are called variable or value passed by the function, there are two types: Actual and formal parameter. Actual parameter: Actual parameter is present in the function call, it may be value or reference of the variable Example: main() { int a , b; int add(int , int ); function declaration ……. Actual parameter ……. add(a,b); function call } L.NIVETHA AP /CSE - KNCET
  • 46. Formal parameter: It is present in the function definition header.it is also called as dummy parameter. Function definition: Formal parameter int add(int x , int y) { body of the statement; …………….. ………………. return statement ; } L.NIVETHA AP /CSE - KNCET
  • 47. Function call/function invocation depending upon the input and output it is classified as. 1.Function with no input and no output. 2.Function with input and output. 3.Function with input and no output. 4.Function no input and with output. L.NIVETHA AP /CSE - KNCET
  • 48. Function with no argument and no return value Syntax: void main() { Void function_name(void); statement ………. function_name (); } void function_name () { ……….. statement } L.NIVETHA AP /CSE - KNCET
  • 49. Example: #include<stdio.h> #include<conio.h> void main() { void message(void); function declaration message( ); function call printf(“welcome”); } void message() function definition { printf(“hello”); } o/p: hello welcomeL.NIVETHA AP /CSE - KNCET
  • 50. Function with argument and with return value Syntax: void main() { int function_name (int , int ); statement; ………. c=function_name (v1,v2); ……. } int function_name (int v1,int v2) { ……….. return(z); } L.NIVETHA AP /CSE - KNCET
  • 51. #include<stdio.h> #include<conio.h> void main() { int add(int , int); int k; clrscr(); k=add(10,20); printf(“The value of k is %d”, k); } Int add(int x , int y) { int z; z= x+y; return(z); } o/p: The value of k is 30 L.NIVETHA AP /CSE - KNCET
  • 52. Function with argument and no return value Syntax: void main() { void function_name(int, int ); ……… ………. function_name (v1,v2); ……. } void function_name (int v1,int v2) { ……….. statement } L.NIVETHA AP /CSE - KNCET
  • 53. #include<stdio.h> #include<conio.h> void main() { void add(int , int); clrscr(); add(10,20); } void add(int x , int y) { int z; z= x+y; printf(“Result is: %d”, z); } o/p: Result is: 30 L.NIVETHA AP /CSE - KNCET
  • 54. Function with out argument and with return value: Syntax: void main() { int function_name(void); ……… ………. c=function_name (void); ……. } int function_name () { ……….. statement return(); } L.NIVETHA AP /CSE - KNCET
  • 55. #include<stdio.h> #include<conio.h> void main() { int add(void); int c; clrscr(); c=add(); printf(“The value of c is %d”, c); getch(); } int add() { int x=10 , y=20 , z; z= x+y; return(z); } o/p: The value of c is 30 L.NIVETHA AP /CSE - KNCET
  • 56. Parameter passing method in function If a function is called ,the calling function to pass some values to the called function. There are two ways to pass the parameter : 1.Call by value (or) pass by value This will pass the values from calling function to called function Here it will copy the actual parameter value into formal parameter. 2. Call by reference (or) pass by reference L.NIVETHA AP /CSE - KNCET
  • 57. Call by value (or) pass by value #include<stdio.h> #include<conio.h> Void main() { void swap(int , int ); int a , b ; clrscr(); a=10; b=20; printf ( “Before swap values are %d%d” , a, b ); swap(a,b) ; printf ( “After swap values are %d%d” , a, b ); getch(); } void swap(int x , int y) { int temp; temp = x; x = y; y = temp; printf ( “In swap function values are %d%d” , x , y ); } o/p: 10 , 20 20 , 10 10 , 20 L.NIVETHA AP /CSE - KNCET
  • 58. 2. Call by reference (or) pass by reference This will pass the parameter address or reference from calling function to called function Here it will copy the actual parameter address into formal parameter. If the arguments are passed through reference the changes made in formal parameter it will affect actual parameter. L.NIVETHA AP /CSE - KNCET
  • 59. Call by reference (or) pass by reference #include<stdio.h> #include<conio.h> Void main() { int swap(int * , int *); int a , b ; clrscr(); a=10; b=20; printf ( “Before swap values are %d%d” , a, b ); swap( &a, &b) ; printf ( “After swap values are %d%d” , a, b ); getch(); int swap(int *x , int * y) { int temp; temp = * x; * x = * y; * y = temp; printf ( “In swap function values are %d%d” , * x , * y ); } o/p: 10 , 20 20 , 10 20 , 10 L.NIVETHA AP /CSE - KNCET
  • 60. Recursion Recursion is a process by which a function calls itself repeatedly until some specified condition has been specified. Recursion can be used to solve the similar problem of smaller size. Recursion is a technique that breaks a problem into one or more sub problems that are similar to the original problem. L.NIVETHA AP /CSE - KNCET
  • 61. Recursion is classified as 1. Direct and indirect recursion 2. Tail and non tail recursion. Direct recursion: If a function is directly recursive then it is calls itself. Indirect recursion. Indirect recursion occurs when a function calls another function L.NIVETHA AP /CSE - KNCET
  • 62. #include<stdio.h> #include<conio.h> Void main() { int fact (int); int n = 5 , re ; clrscr(); re = fact(n); printf(“%d ”, re); } int fact( int x) { if( x==1) return(1); else return( x* fact(x-1)); } L.NIVETHA AP /CSE - KNCET
  • 63. 2. Tail recursion / non tail. Tail recursion: In tail recursion last operation of a function is a recursively called again and again, here no pending operation to be performed from the recursive call L.NIVETHA AP /CSE - KNCET
  • 64. #include<stdio.h> #include<conio.h> Void main() { int fact (int , int ); int n =5 , re; clrscr(); re = fact( n , 1 ); printf(“%d ”, re); } int fact( int x , int result) { if( x==1) return (result) ; else return( fact( x-1 , x*result)); } L.NIVETHA AP /CSE - KNCET
  • 65. Non Tail recursion: In non tail recursion it is like normal recursive function it is called itself again and again, here pending operation to be performed from the recursive call L.NIVETHA AP /CSE - KNCET
  • 66. Passing arrays to functions like simple variable , arrays can also be passed to functions There are two types: 1.Passing individual element of an array one by one. 2. Passing the entire array at a time. L.NIVETHA AP /CSE - KNCET
  • 67. Passing individual element of an array:  Individual elements in an array can be passed by value or by reference,  However this way of passing values not preferable.  because the elements in an array is large, then we will pass entire element will take large number of function calls.  here we will pass individual element in each function call. L.NIVETHA AP /CSE - KNCET
  • 68. Passing one dimensional array to function: To pass the one dimensional array by one by one or entire value by using reference. L.NIVETHA AP /CSE - KNCET
  • 69. #include<stdio.h> //pass by value void main() { int sum_array(int, int ); int arr[10], n,i,sum=0; n=5; printf("enter array element "); for(i=0;i<n;i++) scanf("%d", &arr[i]); for(i=0;i<n;i++) { sum=sum_array(arr[i],sum); } printf("sum is %d", sum); } 6 35 int sum_array(int element , int sum) { return((sum+element)); } 35+6 L.NIVETHA AP /CSE - KNCET
  • 70. #include<stdio.h> //pass by reference void main() { int sum_array(int, int ); int arr[10], n,i,sum=0; n=5; printf("enter array element "); for(i=0;i<n;i++) scanf("%d", &arr[i]); for(i=0;i<n;i++) { sum=sum_array(&arr[i],sum); } printf("sum is %d", sum); } int sum_array(int *element,int sum) { return((sum + *element)); } L.NIVETHA AP /CSE - KNCET
  • 71. Passing the entire array at a time passing entire array at a time is a preferred way of passing arrays to functions, Here we pass the entire array by using reference or value. L.NIVETHA AP /CSE - KNCET
  • 72. #include<stdio.h> void main() { void display(int[]); int a[10],i; printf("Enter the array elementn"); for(i=0;i<5;i++) scanf("%d",&a[i]); display(a); } void display(int a[]) { int i; printf("the array elements are"); for(i=0;i<5;i++) { printf("%dn",a[i]); } } L.NIVETHA AP /CSE - KNCET