Introduction to Microprocesso programming and interfacing.pptx
Functions in C - Prototypes, recursion, call by value & reference
1. 1
Advance Computer Programming(3320702)
CH – 3
Functions
GOVERNMENT POLYTECHNIC,
AHMEDABAD
SHAH DHRUV P. (166170307093)
SHAH HARSH J. (166170307095)
SHAH KUNJ D. (166170307096)
SUBMITTED BY
2. TOPICS
2
• Functions.
• Prototype.
• Nested Functions.
• Types of Functions.
• Recursion.
• Call By Value & Call By Refrence.
• Function & Array.
• Storage Classes.
3. FUNCTIONS
3
• Defination :- A function is a self contained block of statements
that performs a co-herent task of some kind.
• Every C program can be thought of as a collection of this
function.
• Functions are used when user wants to call a multiple times.
• Functions returns value as either 1 or 0.
• A void function will never return a value.
3.1
4. PROTOTYPE
4
• Prototype is used when a user wants to define a function after the main()
function.
• The syntax for prototype inside main() function is:-
data_type function_name(data_type1,data_type 2,……data_type n);
3.2
5. NESTED FUNCTIONS
5
• If a function makes a call to another function in its body , then it
is called Nested Function.
• For example while making a program of sorting an array , user
uses the swap function inside the function of sorting.
3.3
8. LIBRARY FUNCTIONS
8
• Library Functions are the predefined functions in C language.
• These are the functions which can’t be changed.
• The example of these functions is main().
9. USER DEFINED FUNCTIONS
9
• User defined Functions are functions which are made by the user
in a C program and called in main() function.
• User defined functions are of 4 types:-
1. No Return value and No Argument.
2. With Return value and No Argument.
3. No Return value and With Argument.
4. With Return value and With Argument.
10. NO RETURN VALUE AND NO ARGUMENT
10
• Syntax for declaration :-
void Function_name()
{
BODY
}
• Syntax for calling:-
Function_name();
11. NO RETURN VALUE AND WITH
ARGUMENT
11
• Syntax for declaration :-
void Function_name(argument 1,
argument2…….argument n)
{
BODY
}
• Syntax for calling:-
Function_name(argument 1,argument 2,
……..argument n);
12. WITH RETURN VALUE AND NO
ARGUMENT
12
• Syntax for declaration :-
data_type Function_name()
{
BODY
}
• Syntax for calling:-
data_type variable_name=Function_name();
13. WITH RETURN VALUE AND WITH
ARGUMENT
13
• Syntax for declaration :-
data_type variable=
Function_name(argument 1,argument 2……argument n)
{
BODY
}
• Syntax for calling:-
data_type variable_name=Function_name(argument 1,
argument 2……..argument n);
14. RECURSION
14
• When a function calls itself it is called Recursion.
• Recursion is used in finding factorial of number , Fibonacci
sequence , etc.
• Advantages of recursion are:-
1. Unnecessary use of loop can be stopped.
2. Returning of unnecessary value can be stopped.
3.5
15. RECURSION
15
• Disadvantages of recursion :-
1. If condition is necessary to return a value.
2. Sometimes it is difficult to trace the flow in recursion.
16. CALL BY VALUE
16
• While passing the argument in a function if the parameter
passed is the value , it is called call by value.
• For example :- While making a function of swapping , the value
passed as parameter is value of a variable ,it is called call by
value.
3.6
17. CALL BY REFERENCE
17
• While passing the argument in a function if the parameter
passed is the reference variable i.e. address of variable, it is
called call by reference.
• For example :- While making a function of swapping , the value
passed as parameter is address of a variable ,it is called call by
reference.
18. FUNCTION & ARRAY
18
• In function user can also pass the array as a parameter .
• When array is passed as a parameter in a function , actually
the address of the first element in array is passed .
• Here , user has to take one more parameter with the array as
the total number of elements in array.
3.7
20. SCOPE OF VARIABLES
20
• Scope of Variables can divided into 2 types:-
1. Local Variables.
2. Global Variables.
1. Local Variables:- Scope of local variables is limited upto a
function block (i.e. within the curly braces).
21. SCOPE OF VARIABLES
21
2. Global Variables :- Global Variables has no such boundary limit .
The Global Variables can be accessed from any where in the C
program
These variables are declared above the main() function.
22. LIFE OF VARIABLE
22
• The life of local variable is limited upto the curly braces while
the life of Global Variable is till the program termination as it is
declared only once in the program.
• Register Variable :- Register variables are used to execute the
program faster.
• The syntax to declare Register variable is:-
register data_type variable_name;