SlideShare ist ein Scribd-Unternehmen logo
1 von 19
STUDY C LANGUAGE
III
WITH
ARAFAT BIN REZA
FUNCTIONS
A function is a block of code that performs a particular task.
There are times when we need to write a particular block of code for more than once in our program. This may lead
to bugs and irritation for the programmer. C language provides an approach in which you need to declare and define
a group of statements once and that can be called and used whenever required. This saves both time and space.
C functions can be classified into two categories,
1. Library functions
2. User-defined functions
TYPES-OF-FUNCTION
Library functions are those functions which are defined by C
library, example printf(), scanf(), strcat() etc. You just need to
include appropriate header files to use these functions. These are
already declared and defined in C libraries.
User-defined functions are those functions which are defined by
the user at the time of writing program. Functions are made for
code reusability and for saving time and space.
BENEFITS OF USING FUNCTIONS
1. It provides modularity to the program.
2. Easy code Reusability. You just have to call the function by its name to use it.
3. In case of large programs with thousands of code lines, debugging and editing becomes easier if you use
functions.
FUNCTION DECLARATION
General syntax of function declaration is,
return-type function-name (parameter-list) ;
Like variable and an array, a function must also be declared before its called. A function declaration tells the
compiler about a function name and how to call the function. The actual body of the function can be defined
separately. A function declaration consist of 4 parts.
1. return-type
2. function name
3. parameter list
4. terminating semicolon
FUNCTION DEFINITION SYNTAX
General syntax of function definition is,
return-type function-name (parameter-list)
{
function-body ;
}
The first line return-type function-name(parameter) is known as function header and the statement within curly
braces is called function body.
1. return-type : return type specifies the type of value(int,float,char,double) that function is expected to return to
the program calling the function.
2. function-name : function name specifies the name of the function. The function name is any valid C identifier
and therefore must follow the same rule of formation as other variables in C.
3. parameter-list : The parameter list declares the variables that will receive the data sent by calling program.
They often referred to as formal parameters. These parameters are also used to send values to calling program.
FUNCTION DEFINITION SYNTAX
4. function-body: The function body contains the declarations and the statement(algorithm) necessary for
performing the required task. The body is enclosed within curly braces { } and consists of three parts.
A. local variable declaration.
B. function statement that performs the tasks of the function.
C. a return statement that return the value evaluated by the function.
FUNCTIONS AND ARGUMENTS
Arguments are the values specified during the function call, for which the formal parameters are declared in
the function.
CALLING A FUNCTION
While creating a C function, you give a definition of what the function has to do. To use a function, you will have
to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called function. A called function
performs a defined task and when its return statement is executed or when its function-ending closing brace is
reached, it returns the program control back to the main program.
/* calling a function to get value */
variable = function-name (parameter-list);
CALL TYPE & DESCRIPTION
1. Call by value:
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the
parameter inside the function have no effect on the argument.
2. Call by reference:
This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the
actual argument used in the call. This means that changes made to the parameter affect the argument.
SCOPE RULES
A scope in any programming is a region of the program where a defined variable can have its existence and
beyond that variable it cannot be accessed.
There are three places where variables can be declared in C programming language −
1. Inside a function or a block which is called local variables.
2. Outside of all functions which is called global variables.
3. In the definition of function parameters which are called formal parameters.
SCOPE RULES
1. Local Variables:
Variables that are declared inside a function or block are called local variables. They can be used only by statements
that are inside that function or block of code. Local variables are not known to functions outside their own.
2. Global Variables:
Global variables are defined outside a function, usually on top of the program. Global variables hold their values
throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
A global variable can be accessed by any function.
3. Formal Parameters:
Formal parameters, are treated as local variables with-in a function and they take precedence over global variables.
ARRAYS
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type.
Declaring Arrays:
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by
an array as follows −
type arrayName [ arraySize ];
This is called a single-dimensional array.
Initializing Arrays:
You can initialize an array in C either one by one or using a single statement as follows −
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements that we declare for the
array between square brackets [ ].
ARRAYS
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write −
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
Following is an example to assign a single element of the array −
balance[4] = 50.0;
Accessing Array Elements:
An element is accessed by indexing the array name. This is done by placing the index of the element within square
brackets after the name of the array. For example −
double salary = balance[9];
ARRAYS IN DETAIL
Concept & Description:
1 Multi-dimensional arrays:
C supports multidimensional arrays. The simplest form of the multidimensional array is the two-
dimensional array.
2 Passing arrays to functions:
You can pass to the function a pointer to an array by specifying the array's name without an index.
3 Return array from a function:
C allows a function to return an array.
4 Pointer to an array:
You can generate a pointer to the first element of an array by simply specifying the array name, without
any index.
STRING AND CHARACTER ARRAY
Strings are actually one-dimensional array of characters terminated by a null character '0'.
Remember that C language does not support strings as a data type. These are often used to create
meaningful and readable programs.
Declaring and Initializing a string variables:
There are different ways to initialize a character array variable.
char name[11]=“Programmers"; //valid character array initialization
char name[10]={'L','e','s','s','o','n','s','0'}; //valid initialization
Remember that when you initialize a character array by listings all its characters separately then you must
supply the '0' character explicitly.
STRING INPUT AND OUTPUT
Input function scanf() can be used with %s format specifier to read a string input from the terminal. But
there is one problem with scanf() function, it terminates its input on first white space it encounters.
Therefore if you try to read an input string "Hello World" using scanf() function, it will only read Hello and
terminate after encountering white spaces.
However, C supports a format specification known as the edit set conversion code %[..] that can be used to
read a line containing a variety of characters, including white spaces.
Another method to read character string with white spaces from terminal is gets() function.
METHOD & DESCRIPTION
1. strcat() - It is used to concatenate(combine) two string.
2. strlen() - It is used to show length of a string.
3. strrev() - It is used to show reverse of a string.
4. strcpy() - Copies one string into another.
5. strcmp() - It is used to compare two string.
6. strchr ( ) - Returns pointer to first occurrence of char in str1.
7. strrchr ( ) - last occurrence of given character in a string is found.
8. strstr ( ) - Returns pointer to first occurrence of str2 in str1.
METHOD & DESCRIPTION
9. strrstr ( ) - Returns pointer to last occurrence of str2 in str1.
10. strcmpi ( ) - Same as strcmp() function. But, this function negotiates case. “A” and “a” are treated as
same.
11. strlwr ( ) - Converts string to lowercase.
12. strupr ( ) - Converts string to uppercase.
strcmpi( ) function in C is same as strcmp() function. But, strcmpi( ) function is not case sensitive. i.e, “A”
and “a” are treated as same characters. Where as, strcmp() function treats “A” and “a” as different
characters.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
Shanks
ShanksShanks
Shanks
 
Handout#04
Handout#04Handout#04
Handout#04
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
LISP:Program structure in lisp
LISP:Program structure in lispLISP:Program structure in lisp
LISP:Program structure in lisp
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C fundamental
C fundamentalC fundamental
C fundamental
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Class 10
Class 10Class 10
Class 10
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Ch13
Ch13Ch13
Ch13
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Handout#06
Handout#06Handout#06
Handout#06
 
structure and union
structure and unionstructure and union
structure and union
 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
 
Clanguage
ClanguageClanguage
Clanguage
 

Ähnlich wie C language 3

Ähnlich wie C language 3 (20)

Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
4. function
4. function4. function
4. function
 
qb unit2 solve eem201.pdf
qb unit2 solve eem201.pdfqb unit2 solve eem201.pdf
qb unit2 solve eem201.pdf
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 
Functions
FunctionsFunctions
Functions
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
C language presentation
C language presentationC language presentation
C language presentation
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 

Mehr von Arafat Bin Reza

Mehr von Arafat Bin Reza (9)

C# Class Introduction.pptx
C# Class Introduction.pptxC# Class Introduction.pptx
C# Class Introduction.pptx
 
C# Class Introduction
C# Class IntroductionC# Class Introduction
C# Class Introduction
 
Inventory music shop management
Inventory music shop managementInventory music shop management
Inventory music shop management
 
C language 2
C language 2C language 2
C language 2
 
C language updated
C language updatedC language updated
C language updated
 
C language
C languageC language
C language
 
Sudoku solve rmain
Sudoku solve rmainSudoku solve rmain
Sudoku solve rmain
 
string , pointer
string , pointerstring , pointer
string , pointer
 
final presentation of sudoku solver project
final presentation of sudoku solver projectfinal presentation of sudoku solver project
final presentation of sudoku solver project
 

Kürzlich hochgeladen

Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
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
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 

Kürzlich hochgeladen (20)

Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
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...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 

C language 3

  • 2. FUNCTIONS A function is a block of code that performs a particular task. There are times when we need to write a particular block of code for more than once in our program. This may lead to bugs and irritation for the programmer. C language provides an approach in which you need to declare and define a group of statements once and that can be called and used whenever required. This saves both time and space. C functions can be classified into two categories, 1. Library functions 2. User-defined functions
  • 3. TYPES-OF-FUNCTION Library functions are those functions which are defined by C library, example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries. User-defined functions are those functions which are defined by the user at the time of writing program. Functions are made for code reusability and for saving time and space.
  • 4. BENEFITS OF USING FUNCTIONS 1. It provides modularity to the program. 2. Easy code Reusability. You just have to call the function by its name to use it. 3. In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.
  • 5. FUNCTION DECLARATION General syntax of function declaration is, return-type function-name (parameter-list) ; Like variable and an array, a function must also be declared before its called. A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration consist of 4 parts. 1. return-type 2. function name 3. parameter list 4. terminating semicolon
  • 6. FUNCTION DEFINITION SYNTAX General syntax of function definition is, return-type function-name (parameter-list) { function-body ; } The first line return-type function-name(parameter) is known as function header and the statement within curly braces is called function body. 1. return-type : return type specifies the type of value(int,float,char,double) that function is expected to return to the program calling the function. 2. function-name : function name specifies the name of the function. The function name is any valid C identifier and therefore must follow the same rule of formation as other variables in C. 3. parameter-list : The parameter list declares the variables that will receive the data sent by calling program. They often referred to as formal parameters. These parameters are also used to send values to calling program.
  • 7. FUNCTION DEFINITION SYNTAX 4. function-body: The function body contains the declarations and the statement(algorithm) necessary for performing the required task. The body is enclosed within curly braces { } and consists of three parts. A. local variable declaration. B. function statement that performs the tasks of the function. C. a return statement that return the value evaluated by the function.
  • 8. FUNCTIONS AND ARGUMENTS Arguments are the values specified during the function call, for which the formal parameters are declared in the function.
  • 9. CALLING A FUNCTION While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task. When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program. /* calling a function to get value */ variable = function-name (parameter-list);
  • 10. CALL TYPE & DESCRIPTION 1. Call by value: This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. 2. Call by reference: This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
  • 11. SCOPE RULES A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language − 1. Inside a function or a block which is called local variables. 2. Outside of all functions which is called global variables. 3. In the definition of function parameters which are called formal parameters.
  • 12. SCOPE RULES 1. Local Variables: Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. 2. Global Variables: Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. A global variable can be accessed by any function. 3. Formal Parameters: Formal parameters, are treated as local variables with-in a function and they take precedence over global variables.
  • 13. ARRAYS Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. Declaring Arrays: To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows − type arrayName [ arraySize ]; This is called a single-dimensional array. Initializing Arrays: You can initialize an array in C either one by one or using a single statement as follows − double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
  • 14. ARRAYS If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write − double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0}; Following is an example to assign a single element of the array − balance[4] = 50.0; Accessing Array Elements: An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example − double salary = balance[9];
  • 15. ARRAYS IN DETAIL Concept & Description: 1 Multi-dimensional arrays: C supports multidimensional arrays. The simplest form of the multidimensional array is the two- dimensional array. 2 Passing arrays to functions: You can pass to the function a pointer to an array by specifying the array's name without an index. 3 Return array from a function: C allows a function to return an array. 4 Pointer to an array: You can generate a pointer to the first element of an array by simply specifying the array name, without any index.
  • 16. STRING AND CHARACTER ARRAY Strings are actually one-dimensional array of characters terminated by a null character '0'. Remember that C language does not support strings as a data type. These are often used to create meaningful and readable programs. Declaring and Initializing a string variables: There are different ways to initialize a character array variable. char name[11]=“Programmers"; //valid character array initialization char name[10]={'L','e','s','s','o','n','s','0'}; //valid initialization Remember that when you initialize a character array by listings all its characters separately then you must supply the '0' character explicitly.
  • 17. STRING INPUT AND OUTPUT Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on first white space it encounters. Therefore if you try to read an input string "Hello World" using scanf() function, it will only read Hello and terminate after encountering white spaces. However, C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including white spaces. Another method to read character string with white spaces from terminal is gets() function.
  • 18. METHOD & DESCRIPTION 1. strcat() - It is used to concatenate(combine) two string. 2. strlen() - It is used to show length of a string. 3. strrev() - It is used to show reverse of a string. 4. strcpy() - Copies one string into another. 5. strcmp() - It is used to compare two string. 6. strchr ( ) - Returns pointer to first occurrence of char in str1. 7. strrchr ( ) - last occurrence of given character in a string is found. 8. strstr ( ) - Returns pointer to first occurrence of str2 in str1.
  • 19. METHOD & DESCRIPTION 9. strrstr ( ) - Returns pointer to last occurrence of str2 in str1. 10. strcmpi ( ) - Same as strcmp() function. But, this function negotiates case. “A” and “a” are treated as same. 11. strlwr ( ) - Converts string to lowercase. 12. strupr ( ) - Converts string to uppercase. strcmpi( ) function in C is same as strcmp() function. But, strcmpi( ) function is not case sensitive. i.e, “A” and “a” are treated as same characters. Where as, strcmp() function treats “A” and “a” as different characters.