1. D.E.I. TECHNICAL COLLEGE
COURSE TITLE: Software Development-VI
SLIDE-1
COURSE CODE: VIT351
CLASS: DIPLOMA IN INFORMATION TECHNOLOGY VOCATIONAL (1ST YEAR)
SESSION: 2019-20
2. TOPICS COVER
C STANDARD LIBRARIES
FUNCTIONS IN C
RECURSION
QUIZ SET
3. C Standard Library
C is the programming language which near to
human as well as machine.
It is popularly known as “Middle level Programming
Language”.
C is a powerful, flexible language and has got a
widespread use with a rich standard library of
functions.
C standard library is the standard collection of header
files used to perform certain common operations.
GO TO TOPICS
4. The C standard library provides macros, type definitions
and functions for various tasks such as:
Mathematical Computations
String handling
Input/output Processing
Memory Management
Other operating system services
6. Both the user and the system header files are
included using pre-processing directive #include
Include Syntax has following forms:
#include <file>
It is used for system header files. It searches for ‘file’ in a
standard list of system directories.
#include “file”
It is used for header file of one’s own program. It searches for
a file named ‘file’ in the directory having the current file.
7. Continue….
The most commonly used header file in C are:
stdio.h
string.h
stdlib.h
math.h
ctype.h
alloc.h
9. Functions
A function contains set of instructions enclosed by ‘{ }’
which performs specific operation.
Function is a piece of code to perform a particular task
Function is a block of code which performs usually a unit task
It has some name for identification
Action statements in C language can only reside in some function
block.
Benefits
They make program handling easier.
They reduce the program size.
They make program more readable and understandable.
They can be repeated in the same program.
10. Advantages of Functions
It avoids re-writing of same code over and over
Achieve modularization
Easy to read
Easy to debug
Easy to modify
Better memory utilization
o Function consumes memory on its call
o Function releases memory after its execution
11. Function
Built-in Functions/ Library Functions
The functions that are provided by
C library. They are the part of the
compiler packages.
Ex- pow(), sqrt() etc.,
User-defined Functions
The functions that are created by
the programmers themselves to
carry out an operation.
13. Functions declaration & definition
Function Declaration
Syntax:
return_type function_name (data_type arg1, data_type arg2)
int add(int x, int y); //function declaration
Function Definition
Syntax:
int add(int, int); //function declaration
//function definition
return_type function_name(parameters)
{
Function body
}
14. Continue…
Function Header: function header is same as function declaration
without the semicolon. Function header contains function name,
parameter & return type.
Return Type: Return type is the data type of the value which will be
returned by the function. The function may or may not return a value. If
it does then the data type of the retuning value should be specified,
otherwise the return type needs to be void.
Function Name: This is the name of the function using which we can
call the function when & where needed.
Parameters: The parameters are the input values which will be passed
to the function. It tells about the data types of the arguments, their
order and the number of arguments that will be passed to the
function. The parameters are optional. You can also have functions
without parameters.
Function Body: The function body is the set of statement which
performs a specific task. It defines what the function does.
15. Example:
#include <stdio.h>
int add(int, int); //function declaration
// function definition
int add(int x, int y) //function header
{
// function body
int sum = x+y;
return(sum);
}
// Main Function
int main()
{
int sum = add(23, 31);
printf("%d", sum);
return 0;
}
16. Ways to define functions
There are 4 ways to define the functions:
1. No arguments passed and no return Value
2. No arguments passed but a return value
3. Argument passed but no return value
4. Argument passed and a return value
17. 1.No arguments passed and no return Value
Example:
#include <stdio.h>
void add();
void add()
{
int x = 20;
int y = 30;
int sum = x+y;
printf("sum %d", sum);
}
int main()
{
add();
return 0;
}
Syntax:
function declaration:
void function();
function call: function();
function definition:
void function()
{
statements;
}
18. 2. No arguments passed but a return value
Example:
#include <stdio.h>
int add();
int add()
{
int x = 20;
int y = 30;
int sum = x+y;
return(sum);
}
int main()
{
int sum;
sum = add();
printf("sum %d", sum);
return 0;
}
Syntax:
function declaration:
int function ( );
function call: function ( );
function definition:
int function( )
{
statements;
return a;
19. 3. Argument passed but no return value
Example:
#include <stdio.h>
void add(int, int);
void add(int x, int y)
{
int sum = x+y;
return(sum);
}
int main()
{
add(23, 31);
return 0;
}
Syntax:
function declaration:
void function ( int );
function call: function( a );
function definition:
void function( int a )
{
statements;
}
20. 4. Argument passed and a return value
Example:
#include <stdio.h>
int add(int, int);
int add(int x, int y)
{
int sum = x+y;
return(sum);
}
int main()
{
int sum = add(23, 31);
printf("%d", sum);
return 0;
}
Syntax:
function declaration:
int function ( int );
function call: function ( a );
function definition:
int function( int a )
{
statements;
return a;
}
21. Method of Calling
1. Call by Value
Calling a function by value means, we pass the values of the arguments which are stored or copied
into the formal parameters of the function. Hence, the original values are unchanged only the
parameters inside the function changes.
2. Call by Reference
In call by reference we pass the address(reference) of a variable as argument to any function. When
we pass the address of any variable as argument, then the function will have access to our variable,
as it now knows where it is stored and hence can easily update its value.
Actual Parameter: Those parameters which are passed to functions while calling them is are known
as actual parameter.
For example, 23 & 31 in the above example are the actual parameters.
Formal Parameter: Those parameters which are received by the functions are known as formal
parameters.
For example, x & y in the above example are the formal parameters.
22. Call by value
#include <stdio.h>
void Call_By_Value(int num1)
{
num1=42;
printf("nInside Function, Number is %d", num1);
}
int main()
{
int num;
num=24;
printf("nBefore Function, Number is %d", num);
Call_By_Value(num);
printf("nAfter Function, Number is %dn", num);
return 0;
}
23. Parameters Call by value
Definition
While calling a function, when you pass values by copying variables, it is known as "Call By
Values."
Arguments In this method, a copy of the variable is passed.
Effect Changes made in a copy of variable never modify the value of variable outside the function.
Alteration of value Does not allow you to make any changes in the actual variables.
Passing of variable Values of variables are passed using a straightforward method.
Value modification Original value not modified.
Memory Location Actual and formal arguments will be created in different memory location
Safety Actual arguments remain safe as they cannot be modified accidentally.
Default Default in many programming languages like C++.PHP. Visual Basic NET, and C#.
24. Call by Reference
#include <stdio.h>
// function definition
void Call_By_Reference(int *num1)
{
*num1=42;
printf("nInside Function, Number is %d", *num1);
}
// Main Function
int main()
{
int num;
num=24;
printf("nBefore Function, Number is %d", num);
Call_By_Reference(&num);
printf("nAfter Function, Number is %dn", num);
return 0;
}
25. Parameters Call by reference
Definition
While calling a function, in programming language instead of copying the values of
variables, the address of the variables is used it is known as "Call By References.
Arguments In this method, a variable itself is passed.
Effect Change in the variable also affects the value of the variable outside the function.
Alteration of value Allows you to make changes in the values of variables by using function calls.
Passing of variable Pointer variables are required to store the address of variables.
Value modification The original value is modified.
Memory Location Actual and formal arguments will be created in the same memory location
Safety
Actual arguments are not Safe. They can be accidentally modified, so you need to
handle arguments operations carefully.
Default It is supported by most programming languages like JAVA, but not as default.
26. What is Recursion?
Recursion is a special way of nesting functions, where a function calls itself inside it. We must
have certain conditions in the function to break out of the recursion, otherwise recursion will
occur infinite times.
function1()
{
// function1 body
function1();
// function1 body
}
Not every problem can be solved using recursion
If a problem can be solved using recursion, then it must have an iterative solution too.
Recursion is easy to implement as compare to iterative solution
Recursion takes more memory
Simple code which is easy to understand is always considered as a good code, therefore
recursive functions are used.
GO TO TOPICS
27. Continue..
There are several types of recursion
Linear Recursion
A linear recursive function is a function that only makes a single call to itself each time the function
runs (as opposed to one that would call itself multiple times during its execution). The factorial
function is a good example of linear recursion.
Tail Recursion
A function call is said to be tail recursive if there is nothing to do after the function returns except
return its value
Binary Recursion
Some recursive functions don't just have one call to them-self, they have two (or more). Functions
with two recursive calls are referred to as binary recursive functions.
Exponential Recursion
An exponential recursive function is one that, if you were to draw out a representation of all the
function calls, would have an exponential number of calls in relation to the size of the data set
(exponential meaning if there were n elements, there would be O(a n) function calls where a is a
positive number).
Indirect recursion
A recursive function doesn't necessarily need to call itself. Some recursive functions work in pairs.
Function A calls B and B calls A.
28. Example1: Recursion
#include<stdio.h>
int factorial(int x);
//declaring the function
void main()
{
int a, b;
printf("Enter a number...");
scanf("%d", &a);
b = factorial(a);
//calling the function named factorial
printf("%d", b);
}
int factorial(int x)
//defining the function
{
int r = 1;
if(x == 1)
return 1;
else
r = x*factorial(x-1);
//recursion, since the function calls itself
return r;
}
29. Example2: Recursion
#include <stdio.h>
int sum(int n);
int main() {
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
return 0;
}
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
31. Advantages and Disadvantages of Recursion
Recursion makes program elegant. However, if performance is vital,
use loops instead as recursion is usually much slower.
That being said, recursion is an important concept. It is frequently
used in data structure and algorithms.
For example, it is common to use recursion in problems such as tree
traversal.
32. Quiz Set: 1
Standard Libraries
Functions
GO TO TOPICS
33. 2. What does a C header file contain?
A. It refers to the collection of Header Files
B. It is a collection of classes.
C. It is a collection of standard functions.
D. It refers to special variables and objects.
1. What is C standard Library?
A. It contains the various classes.
B. It contains various special objects, special variables
and function prototypes.
C. The header file contains the function codes.
D. It is a blank file containing nothing.
34. 4. Which header file used to perform
mathematical operation.
A. We copy the file in the program.
B. We keep the header file in the same folder where program exists.
C. We use the directive ‘include’ so that the content copied into the
program.
D. We copy the content of the header file into the program.
3. What do you do when you wish to use header file?
A. <ctype.h>
B. <math.h>
C. <alloc.h>
D. <String.h>
35. 6. Which header file is used to allocate memory
dynamically.
A. <math.h>
B. <stdio.h>
C. <alloc.h>
D. <ctype.h>
5. Which header file support scanf() function?
A. <math.h>
B. <stdio.h>
C. <alloc.h>
D. <ctype.h>
36. A. To do special I/O Functions.
B. To check the type of character being used.
C. To find a character in a string.
D. To declare a variable for storing character.
7. Why do we use ctype.h?
8. What is the disadvantage of function?
A. Function allows sharing of codes.
B. Function allows minimization of size of the code.
C. Functions can not be flexibly used with different types of
inputs.
D. Functions help where are repeating of code.
37. 9. What does a function prototype not contain?
A. Function Codes
B. Function Parameters
C. Return Type
D. Function Name
10. What make a function definition?
A. Name and return types
B. Name, return type and parameters
C. Function prototype and function body
D. The codes make the definition of function
38. 11. What is a formal parameter?
A. A formal parameters are a part of function prototype.
B. A formal parameters are the arguments passed when the
function is invoked.
C. The formal parameters are the values passed as arguments
D. None of the above are true
12. What are actual parameters of a function?
A. Actual parameters are defined with the function prototypes
B. Actual parameters are the argument passed when the function is
invoked.
C. Actual parameters are formal parameters.
D. Actual parameters are part of a function definition.