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
.
.
.
|
|
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()
|
|
Return statement
|
|