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.