Professional Documents
Culture Documents
|
|
!
|
|
_ f any module has sub modules that is called as calling
module and sub modules are called called modules.
_ one module is communicate with other module through
calling module.
_ the function which is calling other function is called
calling function.
_ A function whom it called is called called function
_ A called function receives control from the a calling
function, when it completes its task, it returns control to
the calling function.
_ the main function called by the operating system, in
turn main function calls the other function to do some
task.
|
|
A large program divided into series of
individual related programs called
modules. These modules are called
functions.
Functions are classified into two categories
1. Library functions ( built_in functions).
2. User_defined functions
|
|
Library (built in) functions
C is a library it is a collection of various types of functions
which perform some standard and predefined tasks.
ƥ This are part of the c compiler that have been written for
general purpose are called library functions.
example
printf()
scanf()
sqrt()
Advantage
1.No need to write the code because the functions are
already available so it makes the programmers job much
easier.
2. these functions are used anywhere in the program.
|
|
User defined functions
|
|
Advantages of function
|
|
ð
¢
!
"
!
"
!
|
|
Definition
|
|
Ú
u
#
|
|
Function declaration
Funtion prototype(syntax)
Return_type function_name (argument list);
|
|
Definition
Syntax for the function definition
Returntype function_name(argument list/parameters list)
argument list declaration
{
local variable declarations:
Statements;
return(expression);
}//function.
All the parts are not essential.
|
|
main()
{
___________
__________
func1();
_________
________
funct2();
________
_________
funct3();
_________
_______
}//main
func1()
{
__________
___________
}func1
func2()
{
________
________
}//func2
fucn3()
{
__________
_________
}//func3
|
|
Return type_ the type of value return by the
called function after its task finished off
|
|
main()\\calling function
{
func1();
printf(Ơwe are in main\nơ);
}//main
func1()\\ called funciton
{
printf(Ơwe are in func1\nơ);
}//func1
|
|
Points to remember
1. C program must have at least one function
2. f program have one function it must be main
3. f program have more than function, in which one must
be main because program execution starts from that
only.
4. We can use unlimited no of functions in one program.
5. Each function is called in the sequence we have
specified in the main function.
6. After each function task is over the control passed to the
calling function(main()).
7
|
|
main()
{
printf(Ơ\n am calling other functionsơ);
func1();
func2();
func3();
Printf(Ơall functions are called\nơ);
}//main
func1()
{
printf(Ơin func1\nơ);
}//func1
func2()
{
printf(Ơin func2\nơ);
}//func2
func3()
{
printf(Ơin func1\nơ);
}//func3
Output:
|
|
! $ " " !
|
|
! % !
!
|
|
!u& !
!
|
|
!u& !
!
|
|
!u& !
!
|
|
! ' (
!
!
|
|
! ) !
*
|
|
!u& ( +
|
|
!u& ( +
|
|
!u& ( +
|
|
!u& ( +
|
|
! , !
"
|
|
Categories of functions
|
|
1. Functions with no arguments
and no return values
* This type of function has no arguments,
meaning that it does not receive any data from
the calling function.
ƥ Simillary this type of function will not return any
value.
ƥ Here the calling function does not receive any
data from the called function. n effect, there is
no data transfer between the calling function
and the called function.
|
|
Example
rr
A X
!"
A #
$%&
A #
$%
A '
A $%&
A $%&()
*
)(
A $%&
A +
A #
$%
A '
A
,-.-/&
A
*$012# *,.0%&
A *$0334-5,-5.%&
A /6,7.&
A
*$018634-/%&
A +
|
|
2. 9:|8;<:=;8>:<
?@A2:8
:B?28?:C
D2
|
|
example
rr
A X
!"
A
$%&
A #
$%
A '
A
&
A $%&
A 6$%&()
*
)(
A
*$018634- %&
A $%&
A +
A
$%
A '
A
,-.&
A
*$012# *,.0%&
A *$0334-5,-5.%&
A $,7.%&
A +
|
|
3. 9:|8;<:=;8>
?@A2:8
:B:<?28?:C
D2
n this category there is data transfer from the calling function to the
called function using parameters. But, there is no data transfer from
called function to the calling function.
A 9
A The arguments that are supplied in to two categories
A 1. actual arguments/parameters
A 2. formal arguments/parameters
A
(
A Actual parameters are the expressions in the calling functions.
These are the parameters present in the calling statement (function
call).
A * (
A formal parameters are the variables that are declared in the header
of the function definition. These list defines and declares that will
contain the data received by the function. These are the value
parameters, copies of the values being passed are stored in the
called functions memory area.
A : Actual and Formal parameters must match exactly in type,
order, and number. Their names however, do not need to match.
|
|
_ the values of actual arguments are assigned to the formal arguments.
func1(x1, x2, x3,Ʀxn);
| | | |
func1(y1, y2, y3,Ʀyn)
_ n case, the actual arguments are more than the formal arguments , extra
actual arguments are discarded.
_ on other hand, the formal arguments are more, the extra formal arguments
are initialized to some garbage values.
_ if any mismatch found in their data type may also takes some value. No
error is reported.
|
|
example
((| *
*E F
*
E
#
A X
!"
A #
$
-
%&
A #
$%
A '
A
-F&
A $%&
A
*$012# *F0%&
A *$0334-5-5F%&
A $-F%&()
*
)(
A $%&
A +
A #
$
,-
.%
A '
A
/&
A /6,7.&
A
*$018634-/%&
A }
|
|
4. 9:|8;<:=;8>
?@A2:8
:B?28?:
C D2
|
|
example
((| *
*E F
*
E
#
A X
!"
A
$
-
%&
A #
$%
A '
A
-F&
A $%&
A
*$012# *F0%&
A *$0334-5-5F%&
A 6$-F%&()
*
)(
A
*$018634- %&
A $%&
A +
A
$
,-
.%
A '
A
/&
A ,7.&
A +
|
|
: generally we are using functions with arguments
and return values (category 4) in our applications. Why
because the job of a function is to perform a well_
defined task, it carries inputs and sends result after
executed. n real world the programming teams codes
the modules (functions) according to the input
(arguments) given by the team coordinators.
|
|
Recursion
n c, a function calls itself is called recursion.
t is a repetitive process where function is called itself.
Ex:_
main()
{
printf(Ơrecursion\nơ);
main();
}//main
output:
recursion
.
.
.
A Any problem that can be solved recursively can also be solved iteratively
(using loop).
A Recursive functions are slow and takes a lot of memory space
compared to iterative functions.
|
|
2,?
#*
*
((| *
*
!
fact (n)= n(n_1)(n_2)____1
fact(5)= 5*4*3*2*1=120
#include <stdio.h
#include<conio.h
long int factorial(long int x);
main()
{
long int n,fact;
printf(Ơenter the number\nơ);
scanf(Ơ%ldơ, &n);
fact=factorial(n);
printf(Ơfactorial of a number=%ldơ,fact);
}//main
factorial(long int x)
{
long int f;
if(x==1)
return 1;
else
f= x*factorial(x_1);
return(f);
}//factorial()
|
|
Factorial of a number using non_recursive function (iterative method)
long int factorial(int x);
main()
{
long int n,fact;
printf(Ơenter n value\nơ);
scanf(Ơ%ldơ,&n);
fact=factorial(n);
printf(Ơfactorial value=%ldơ,fact);
}//main
factorial(int x);
{
long int f=1,i;
for(i=x;i =1;i__)
f=f*i;
return (f);
}//factorial();
Out put
enter n value
5
factorial value= 120
|
|
Return statement
|
|
Scope and life time of variable in functions
|
|
÷
|
|
There are four storage classes:
|
|
u
!"
"
. #
FG&
&
H.*
!
|
|
Features of auto variable when they declared
Storage Memory
Default initial value garbage value
Scope local to the block
in which variable
defined
Life remains within the
block in which
variable defined.
|
|
Example_ auto
main()
{
auto int x=10;
{
auto int x=20;
{
auto int x=30;
printf(Ơ%dơ,x);// 30
}
printf(Ơ%dơ,x);//20
}
Printf(Ơ%dơ,x);//10
}//main
|
|
÷
"
#
u
$
|
|
Features of static variable
|
|
Auto example
main()
{
demo1();
demo1();
demo1();
}//main
demo1()
{
auto int i=1;
printf(Ơ%d\nơ,i);
i++;
}//demo1()
output:
1
1
1
|
|
Extern (external storage classes)
Syntax
extern type identifier;
extern int i;
features
_ This variables or also called global variables.
_ These are declared outside of any function.
_ Storage ƛ memory
_ Default initial value_ 0
_ Scope_ global can access even outside of the function.
_ Life_ through out the program execution.
_ Any variable defined outside of the function by default
treated as extern variable.
|
|
example
#
%
"
&'
(
#
)#
)#
*#
*#
+,,
)
%
--#
"
&'
#
+,,
)
*
% -
# .
"
&'
#
+,,
*
.
-
|
|
Register storage class
|
|
Macro expansion
A
.
A X /01/
23 /
A X
/
/
A &/
A &/ 4
4#
X ! .
03
5
6 )
6 ! 7 7
086 9 :3
;rr
|
|
What is the purpose of macro
|
|
A t is customized to use the macro names
in capital letters.
A Macro names and macro body separated
by space.
A Space between # and define is optional.
A Macro definition never end with
semicolon.
|
|
_ # define directive is used to define operators.
# define PLUS +
main()
{
int c;
c= 10 PLUS 20;
}//main()
_ # define directive could be used even to replace a condition.
_ Ex:
_ #define EX a b
_ main()
_ {
_ int a=10,b=20;
_ if (EX)
_ printf(Ơa is big\nơ);
_ else
_ printf(Ơb is bigơ);
_ }//main
_
|
|
A Statement also replaced using macros
# define stat printf (Ơhaiơ);
main()
{
stat
}//main
|
|
A E
A Macro can also have arguments as same as function
have.
|
|
A Donƞt leave space between the macro name and its arguments.
ex:_ SQUARE (n)
A Macro body should be enclosed within parenthesis.
ex:_
#define SQUARE(n) n*n
main()
{
int a;
a=64/SQUARE(4);
printf(Ơa=%dơ,a);
}//main
_ Macros can split into multiple lines, with a Ɲ\ƞ(back slash) present at the end of the
each line.
ex:
#define ABC for(i=0;i<10;i++)\
printf(Ơ%d\nơ,i);
main()
{
int i;
ABC
}//
|
|
macros versus functions
|
|