Unit 3
Unit 3
Functions:
Generally, while writing the programs, the entire logic is divided into smaller parts and each
part is treated as a function.
This type of approach for solving the given problems is known as Top-Down approach.
The top-down approach of solving the given problem can be seen below:
Generally, a function can be imagined like a Black Box, which accepts data input and
transforms the data into output results.
The user knows only about the inputs and outputs.
User has no knowledge of the process going on inside the box which converts the given
input into output.
This can be seen diagrammatically as shown below:
Types of Functions
Based on the nature of functions, they can be divided into two categories. They are:
The predefined functions are available in the header files. So, the user has to include the respective
header file to use the predefined functions available in it.
Formatted I/O functions allow to supply input or Unformatted I/O functions are the most basic form
display output in user desired format. of input and output and they do not allow to supply
input or display output in user desired format.
printf() and scanf() are examples for formatted getch(), getche(), getchar(), gets(), puts(), putchar()
input and output functions. etc. are examples of unformatted input output
functions.
Formatted input and output functions contain Unformatted input and output functions do not
format specifier in their syntax. contain format specifier in their syntax.
Formatted I/O functions are used for storing data Unformatted I/O functions are used for storing data
more user friendly. more compactly.
Formatted I/O functions are used with all data Unformatted I/O functions are used mainly for
types. character and string data types.
// pro to demo formatted i/o functions // pro to demo un-formatted i/o functions
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
1) ceil(number) rounds up the given number. It returns the integer value which is
greater than or equal to given number.
2) floor(number) rounds down the given number. It returns the integer value which is
less than or equal to given number.
1) strlen( ) Function :
This function is used to find the length of a character string.
Ex: int n;
char st[20] = “Bangalore”;
n = strlen(st);
• This will return the length of the string which is assigned to an integer variable n.
2) strcpy( ) Function :
This function copies contents of one string into another string.
Ex: char str1[10] =”Welcome”;
char str2[10];
strcpy (str1, str2) ; // It copies contents of str2 into str1.
3) strcat( ) Function :
This function concatenates two given strings. It concatenates source string at the end of
destination string.
ex: char str1[20]=”Hello”;
char str2[20]=”Welcome” ;
strcat ( str1, str2 ); // str2 is concatenated at the end of str1.
4) strcmp( ) Function :
This function compares two given strings and returns zero if they are same.
If length of string1 < string2, it returns < 0 value.
If length of string1 > string2, it returns > 0 value.
Ex: char str1[10]=”Hello”;
char str2[10]=”Hello”;
Int n= strcmp(str1,str2); // if n=0 both strings are same
5) strrev() function :
6) strlwr() function :
This function converts a given string into lowercase.
Ex: char str1[10]=”HELLO”;
Strlwr(str1); // output is : hello
7)strupr() function :
This function converts a given string into uppercase.
Ex: char str1[10]=”hello” ;
strupr(str1); // output is : HELLO
Character functions in C
Character functions need ctype.h header file to be included in the program.
Different character functions provided by C Language are:
1. isalpha():
This function checks whether the character variable/constant contains alphabet or not.
2. isdigit()
This function checks whether the character variable/ constant contains digit or not.
3. isalnum()
This function checks whether the character variable/constant contains an alphabet or digit.
4. ispunct()
This function checks whether the character variable/constant contains a punctuator or not.
Punctuators are comma, semicolon etc.
5. isspace()
This function checks whether the character variable/constant contains a space or not.
6. isupper()
This function checks whether the character variable/constant contains an capital letter
alphabet or not.
7. islower()
This function checks whether the character variable/constant contains a lowercase alphabet
or not.
8. toupper()
9. tolower()
Function Description
difftime() This function is used to get the difference between two given times
strftime() This function is used to modify the actual time format
localtime() This function shares the tm structure that contains date and time information
gmtime() This function shares the tm structure that contains date and time information
ctime() This function is used to return string that contains date and time information
asctime() Tm structure contents are interpreted by this function as calendar time. This time is converted
into string.
1. Declaring functions
The function declaration tells the compiler and the user about what is the function’s name,
inputs and output(s) of the function and the return type of the function.
return_type function_name(parameter_list) ;
2. Defining functions
The function definition specifies how the function will be working i.e the logic of the function
will be specified in this step.
function-name(argument-list)
Classification of Functions
Based on the parameters and return values, functions can be categorized into four types.
They are:
Defining an array –
Initializing an array –
characteristics of an array-
One dimensional array –
Two-dimensional array –
Multi dimensional array.