0% found this document useful (0 votes)
35 views8 pages

Unit 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

Unit 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT-III:

FUNCTIONS- ARRAYS - STRINGS [character Arrays]


 Functions:
▪ Definition and declaration of functions-
▪ Function proto type-
▪ return statement-
▪ types of functions-
▪ Formatted and unformatted functions.
 Built in functions:
▪ Mathematical functions –
▪ String functions –
▪ Character functions –
▪ Date functions.
 User defined functions:
▪ Introduction - Need for user defined functions - Elements of functions –
▪ Function call – call by value and call by reference - Recursive functions.
 Arrays:
▪ Introduction - Defining an array - Initializing an array –characteristics of an array-
• One dimensional array –
• Two-dimensional array –
• Multi-dimensional array.
 Strings:
• Introduction - Declaring and initializing string –
• Reading and Writing strings –
• String standard functions

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:

Definition: A function is a self-contained block of code that performs a certain task/job.

 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:

1. Predefined functions / Library functions


2. User defined functions

Predefined functions / Library functions


 A predefined function or library function is a function which is already written by another developer.
The users generally use these library functions in their own programs for performing the desired task.

 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 and Unformatted Functions:


 Input output functions in C programming falls into two categories, namely, formatted input output
(I/O) functions and unformatted input output (I/O) functions.

Formatted I/O functions: Un-Formatted I/O functions:

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>

int main() int main()


{ {
int a; char ch ;
clrscr(); clrscr();
printf(“Enter value of a:”); printf(“Press any character:”);
scanf(“%d”, &a); ch = getche();
printf(“ a = %d”, a); printf(“\nYou pressed :”
putchar(ch);
}
}

Mathematical function in ‘C’


 C Programming allows us to perform mathematical operations through the functions defined
in <math.h> header file.
 The <math.h> header file contains various methods for performing mathematical operations
such as sqrt(), pow(), ceil(), floor() etc.
 The commonly used functions of math.h header file are given below.

No. Function Description

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.

3) sqrt(number) returns the square root of given number.


4) pow(base, exponent) returns the power of given number.

5) abs(number) returns the absolute value of given number.

String functions in ‘C’


 C supports a large number of string handling functions in the standard library "string.h".

 Few commonly used string handling functions are discussed below:

Function Work of Function


strlen() calculates string length
strcpy() copies a string to another
strcat() concatenates(joins) two strings
strcmp() compares two strings
strrev() Reverses the string
strlwr() converts string to lowercase
strupr() converts string to uppercase

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 :

This function reverses a given string.

Ex: char name[20]=”Raju” ;


strrev(name); // output is : ujaR

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()

 This function converts lowercase alphabet into uppercase alphabet.

9. tolower()

 This function converts an uppercase alphabet into lowercase alphabet.

Date & Time Functions in C


 Time functions in C are used to interact with system time routine and formatted time
outputs are displayed.
 date & time related functions are available in <time.h> header file.

Function Description

setdate() This function used to modify the system date

getdate() This function is used to get the CPU time

clock() This function is used to get current system time

time() This function is used to get current system time as structure

difftime() This function is used to get the difference between two given times
strftime() This function is used to modify the actual time format

mktime() This function interprets tm structure as calendar time

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.

User defined functions-elements function:


 A user defined function is a function which is declared and defined by the user himself.
 While writing programs, if there are no available library functions for performing a particular
task, we write our own function to perform that task.
 Example for user defined function is main function.

Need for functions

 Functions have many advantages in programming.


 Almost all the languages support the concept of functions in some way.
 Some of the advantages of writing/using functions are:

1. Functions support top-down modular programming.


2. By using functions, the length of the source code decreases.
3. Writing functions makes it easier to isolate and debug the errors.
4. Functions allow us to reuse the code.

▪ Every function has the following elements:

1. Function Declaration (Function Prototype)


2. Function Definition
3. Function Call

1. Declaring functions

 The function declaration is the blue print of the function.

 The function declaration can also be called as the function’s prototype.

 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.

The syntax for declaring a function is shown below:

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.

 The syntax of function definition is shown below:


return_type function_name(parameter_list)
{
local variables;
statement(s);
}
Using(calling) Functions
 After declaring and defining the functions, we can use the functions in our program. For
using the functions, we must call the function by its name. The syntax for calling a function is
as shown below:

function-name(argument-list)

Classification of Functions

 Based on the parameters and return values, functions can be categorized into four types.
They are:

1. Function without arguments and without return value.


2. Function without arguments and with return value.
3. Function with arguments and with return value.
4. Function with arguments and without return value.
Arrays: Introduction –
 An array is a variable that can hold multiple values or similar types of data.
 Arrays are the derived data type in C programming language which can store the primitive
type of data such as int, char, double, float.
 All array elements must be are of the same data type.

 Defining an array –
 Initializing an array –
 characteristics of an array-
 One dimensional array –
 Two-dimensional array –
 Multi dimensional array.

You might also like