Search This Blog
Thursday, 8 February 2018
Functions in C
1. Why
User Defined Function is needed?
2.
Definition
3.
Function
in C Language
4.
Return
Values and their Types
5.
Arguments
and Return value combination.
6.
Scope
and lifetime of variables in functions
7.
Storage
Classes
8.
Nesting
of Functions
9.
Recursion
10. Functions and Arrays
11. Summary
1.
Why User Defined Function
is needed?
· The large programs are divided into functional parts, then each part may be coded independently and then combined into a single unit.
· Some type of operations are repeatedly used throughout the program. Such part can be written as a function and called whenever required .
· It saves space and time also.
· It reduces the length of the source program.
· Top-down modular programming is done. i.e high level logic of the problem is solved first then details of lower level function solved.
· The large programs are divided into functional parts, then each part may be coded independently and then combined into a single unit.
· Some type of operations are repeatedly used throughout the program. Such part can be written as a function and called whenever required .
· It saves space and time also.
· It reduces the length of the source program.
· Top-down modular programming is done. i.e high level logic of the problem is solved first then details of lower level function solved.
2.
Definition
Function is a set of statements which perform particular task together.
Function is a set of statements which perform particular task together.
3.
Function in C
Language
C functions are divided into two categories
C functions are divided into two categories
1.
Library
function
Library functions are predefined. They are
not needed to write by user.
e.g. scanf(),prinf,exit,
2.
User-defined
function
These are defined by user at the time of writing
program.
Syntax:
4.
Return Values and
their Types
5.
Arguments and Return
value combination.
6.
Scope and lifetime
of variables in functions
Definitions:
Scope
: It
is a parts of program where variable is available for use.
Lifetime:
It
is a period during which variable retains a given value during execution of
program.
Categories of variable:
1.
Local / Internal
variable
A variables which are declared within
particular function or block of the program.
2.
Global / external
variable
A variables
which are declared outside of any function.
7.
Nesting of Functions
Nesting of function call is allowed in C Language.
e.g main function calls Function1(),Function1
calls Function2 and which in turns call another function such type of nesting
is allowed
*Note: One cannot write function definition within another function
definition
function1(a, function2(p) );
8.
Recursion:
Whole sequence of execution is as follows:
Red color -indicates statements are executed.
Blue color -indicates statements are in execution phase but not completed due to recursive call.
Black solid arrow-show the transfer of control because of recursive call.
Red dotted arrow-shows returning of control after recursive function call finishes.
Definition:
A
Function which calls itself is known as recursion.
e.g
int Factorial(int n)
{
1.
if(n==1)
2.
return
1;
3.
else
4.
return(n*factorial(n-1));
}
Let us see detail execution
of the recursion
In above function ,
n=5;
Now value 1 is returned from factorial(1)
function call is put into the previous function factorial(2) who calls
factorial(1) and so on….
Let us see the sequence
of execution .
Whole sequence of execution is as follows:
Red color -indicates statements are executed.
Blue color -indicates statements are in execution phase but not completed due to recursive call.
Black solid arrow-show the transfer of control because of recursive call.
Red dotted arrow-shows returning of control after recursive function call finishes.
9.
Functions and Arrays
How to pass array to a
function?
In
Calling function: Pass an array name without subscript and
size of an array
e.g maximum(a,n);
Where a
is an array name and n is the size of array.
In
Function Definition:
Sample
Program:
10. Summary
·
Function
is used to modularize the program.
·
Function
can return only one value.
·
Function
may or may not return a value.
·
Function
return integer value by default.
·
Actual
and formal parameter must be same in data type.
·
Function
with return value must return some value with specified data type.
·
Function
can have more than one return statements.
·
A
Return statement can be written anywhere within function.
·
A
function can be written before or after main function.
·
Function
can not be defined inside another function.
·
A
variable declared inside function is local to that function .It is destroyed
when function is exited.
Next Prev
Arrays
Let us see an Array with following points:
- What is an Array?
- Declaration of an Array.
- Types of Array.
- Initialization of an Arrays.
- Solved Case Studies.
Let us see an Array with following points:
- What is an Array?
Array can be defined as follows:
- It is a group of same type of elements.
- It is a set of values of same data type.
- Related data items share the common name .
- Single name can be used to represent collection of same type of items.
E.g Array of int ,Array of float ,Array of char etc.
Array can be defined as follows:
- It is a group of same type of elements.
- It is a set of values of same data type.
- Related data items share the common name .
- Single name can be used to represent collection of same type of items.
E.g Array of int ,Array of float ,Array of char etc.
- Declaration of an Array
Let us see how to declare Array in C Language
Syntax:
data_type Array_name[size_of_array];
This is one dimensional array where
[] are used to specify Array_name is an array and inside bracket we mention the size of array means how many elements are hold by array_name
data_type Array_name[no_of_rows][no_of_column];
This is Two dimensional array
- Types of Array.
The type of an array depends on number of dimensions used.
The types are
- 1-D array(One dimension Array)
- 2-D Array(Two Dimension Array)
- Multi Dimension array
- 1-D array(One dimension Array)
In this data is stored linearly in consecutive locations i.e it reserve consecutive memory locations for specified type of data with size_of_array.
e.g
int roll[10];
It declares an integer array roll with size 10.i.e Array with name roll can store 10 integer elements.
Each element is used with array name and it's subscript/index.
For array index always starts with 0 and ends with (size_of_array-1)
e.g
roll[0] -->This is the first element of an array
roll[1]
.
.
.
roll[9] -->This is the last element of an array.
Memory allocated for this array is calculated as follows:
Assume size of int is 2 bytes
Total consecutive Memory reserved/allocated is 2*10=20 bytes
- 2-D Array(Two Dimension Array)
In this type data is stored in table format by using rows and columns
Syntax:
1) int abc[10][5]; --> abc array has 10 rows and 5 columns
i.e it can store 10*5=50 integer elements into array abc
2) char name[2][10]; -->name array has 2 rows and 10 columns
i.e it can store 2*10=20 character elements into array name
Memory allocation for 2-D array.
Total memory for (rows*column) elements will be allocated.
In above 1st example ,
Assume size of int is 2 bytes
Total consecutive Memory reserved/allocated is 2*50=100 bytes
In above 2nd example ,
Assume size of char is 1 bytes .
Total consecutive Memory reserved/allocated is 1*20=20 bytes.
Total consecutive Memory reserved/allocated is 1*20=20 bytes.
Each element in 2-D is used with array name and it's 2 subscript/index
e.g
abc[0][0] -->this is the first element of an array
abc[0][1]
..
abc[9][4] -->This is the last element of an array
- Multi Dimension array
Similar to 2D this uses multiple dimensions.
e.g
int xyz[2][3][4];
- Initialization of an Arrays
Initialization means assigning initial values to elements in the array i.e at the time of declaration
1-D Array Initialization
type arr-name[size]={comma separated list of values};
e.g
int roll[10]={1,2,3,4,5,6,7,8,9,10};
//it will initialize all 10 elements of roll array
int roll[10]={1,2,3,4};
//it will initialize only 1st 4 elements and remaining to zero(0)
Array Initialization without size
int roll[]={1,2,3,4,5,6};
//It will declare roll array to contain 6 elements with initial vales as per given in list
char name[]={'R','I','Y','A'};
2-D Array Initialization
int roll[2][4]={1,1,1,1,2,2,2,2};
//initialize row by row
//first row to 1 and second row to 2
also written as
int roll[2][4]={{1,1,1,1},{2,2,2,2}};
if value missing in initialization then automatically set to zero.
e,
//first 2 elements of 1st row to 1 and 1st 3 elements of 2nd row to 2 and remaining are set to zerog
int roll[2][4]={{1,1},{2,2,2}};
Subscribe to:
Comments (Atom)








