Programming Languages Unit - III

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

PROGRAMMING LANGUAGES

UNIT - III
• FUNCTIONS
• Advantages of Functions
• The function provides modularity.
• The function provides reusable code.
• In large programs, debugging and editing tasks is
easy with the use of functions.
• The program can be modularized into smaller parts.
• Separate function independently can be developed
according to the needs.
• There are two types of functions in C
• FUNCTION TYPES
• 1.Built-in(Library) Functions
– The system provided these functions and stored in
the library. Therefore it is also called Library
Functions.
e.g. scanf(), printf(), strcmp, strlen, strcat etc.
– To use these functions, you just need to include the
appropriate C header files.
• User Defined Functions These functions are
defined by the user at the time of writing the
program.
• math.h LIBRARY FUNCTION
• All C inbuilt functions which are declared in
math.h header file are given below. The source
code for math.h header file is also given below for
your reference.
• LIST OF INBUILT C FUNCTIONS IN math.h FILE:
• “math.h” header file supports all the
mathematical related functions in C language. All
the arithmetic functions used in C language are
given below.
• Click on each function name below for detail
description and example programs.
Function Description

round ( ) This function returns the nearest integer


value of the float/double/long
double argument passed to this function. If
decimal value is from “.1 to .5”, it returns
integer value less than the argument. If
decimal value is from “.6 to .9”, it returns
the integer value greater than the
argument.
sin ( ) This function is used to calculate sine
value.
cos ( ) This function is used to calculate cosine.
exp ( ) This function is used to calculate the
exponential “e” to the xth power.
tan ( ) This function is used to calculate
tangent.
log ( ) This function is used to calculates
natural logarithm.
sqrt ( )
This function is used to find square
root of the argument passed to this
function.
• Console I/O functions: These functions allow us to
receive input from the input devices like keyboard and
provide output to the output devices like the Visual
Display Unit.
• Console I/O functions
• A console comprises the VDS and the keyboard. The
Console Input and Output functions can be classified
into two categories:
• Formatted console I/O functions: These functions allow
the user to format the input from the keyboard and the
output displayed in the desired manner.
• Unformatted console I/O functions: These functions do
not allow the user this feature.
• These various formatted and unformatted
console I/O functions are shown in the figure
below:
• Formatted Console Input Function: scanf()
• scanf() is the formatted console input function
which reads formatted input from
stdin(standard input). It can read any integer,
float, character, string, etc data from the user.
The syntax of using scanf() is as follows:
scanf("format specifier" ,arguments address);
Let us consider an example: scanf(“%d”, &num);
• In this example, the %d is the format specifier
for an integer hence, num stores an integer
value. &num indicates the address of num
where the value is to be stored under the
variable name ‘num’.
• Formatted Console Input Function: printf()
• printf() is the formatted console output function
which prints the formatted output to the
stdout(standard output). It can display integers,
floating point values, characters, string, etc as
indicated by the user.
• The syntax of using printf() is as follows:
printf("text");
• This shall simply print “text” on the output screen.
Any other textual data can be written within double
quotes and displayed using printf() function. In order
to print any variable value along with text we have the
following format:
• printf("text <format specifier>",variable);
• This shall print the value of the variable in the
format specified by the format specifier.
• For example,
• printf(“marks: %d”,marks);
• This has the format specifier as %d which
stands for integer data and it shall print the
marks in integer format.
• THE STANDARD FILES
• C programming treats all the devices as files.
So devices such as the display are addressed in
the same way as files and the following three
files are automatically opened when a
program executes to provide access to the
keyboard and screen.
• Standard File File PointerDevice
• Standard input stdin Keyboard
• The getchar() and putchar() Functions
• The int getchar(void) function reads the next
available character from the screen and returns it as
an integer. This function reads only single character
at a time. You can use this method in the loop in case
you want to read more than one character from the
screen.
• The int putchar(int c) function puts the passed
character on the screen and returns the same
character. This function puts only single character at
a time. You can use this method in the loop in case
you want to display more than one character on the
screen. Check the following example −
• FUNCTION TYPES
2.User Defined Functions These functions are
defined by the user at the time of writing the
program.

You might also like