PPT06-Function and Recursion
PPT06-Function and Recursion
PPT06-Function and Recursion
TOPIC 06
• Modular Programming
• Function
• Identifier Scoping
• Passing Parameter
• Recursion
MODULAR PROGRAMMING
Main Program
SubProgram SubProgram
MODULAR PROGRAMMING
#include<stdio.h>
#include<math.h>
int main() {
int i;
for(i=0; i<6; i++)
printf(“%d %f”,i,sqrt(i));
return 0;
}
FUNCTION DEFINITION
Function Construction
void main () {
int a,b;
printf("Input 2 even values : ");
scanf("%d %d", &a, &b);
printf("Largest value is : %d\n",maximum(a,b));
}
Actual parameter
FUNCTION DEFINITION
Function in C usually written above the initiator/caller or main program. Otherwise, should use
Function Prototype
Syntax :
return-value-type function-name ( parameter-list );
FUNCTION PROTOTYPE
o Example:
void main () {
int a,b;
printf("Input 2 even values : ");
scanf("%d %d", &a, &b);
printf("Largest value : %d\n",maximum(a,b));
}
FUNCTION PROTOTYPE
Important:
parameters data type, number of parameters and its order
IDENTIFIER SCOPING
Identifier Scoping:
scope of an identifier is reachable
Identifier Scoping:
• Local
• Global
IDENTIFIER SCOPING
o Local Identifier
• Identifier declared in a function including the parameters
• Scope limited in the function
o Global Identifier
• Identifier declared outside any function and placed on top of all functions in a C program
• Reachable from any point in the program
• Global Identifier, can be re-declared in subprogram
• It is advisable not to use global variable for the following reasons:
– Error rate might increase as line of code increase.
– Difficult in debugging
– Exclusivity of data is low. All functions in the program can change its value
IDENTIFIER SCOPING
int x;
function1(){
scope of variable x
-
}
int y;
function2(){
int z;
- scope of variable y
}
main(){
int z;
int y;
- z and y scope only in main program
} z in main different from function2()
PASSING PARAMETER
o If a module is not self sufficed, then the needed data/value and its result passes in and out
using a parameter(s)
o List of parameters is the interface of a module with other modules
o Passing Parameter
– By-Value, sent to other module is the value
– By Location/by reference, sent to other module is the address
PASSING PARAMETER
/*Main Program*/
void main()
{
char A = ’-’;
Line(A); /* A is Actual Parameter */
}
PASSING PARAMETER
void main()
{
int X, Y, P, Q; /*local variable*/
printf(“ X=”); scanf(“%d”,&X);
printf(“ Y=”); scanf(“%d”,&Y);
Calculate(X,Y,&P,&Q);
printf(”X + Y = %d\n”, P);
printf(”X * Y = %d\n”, Q);
}
RECURSIVE DEFINITION
• Factorial - Iterative
long factor(int n) {
long i, fac = 1;
for(i=1; i<=n; i++) fac *= i;
return (fac);
}
RECURSIVE
Recursive Drawback
Although recursive code more concise it needs:
– More memory consumption – as stack memory is needed
– Takes longer time, should traverse through all recursive call using stack
int Fib(int n) {
int f;
if(n==0) f = 0;
else if(n==1) f = 1;
else f = Fib(n-2) + Fib(n-1);
return f;
}
PROGRAM EXAMPLE USING
RECURSIVE
Fibonacci Number
Fibonacci illustration N=4
FIB (4)
o In making good program design, modular programming is usually used. This concept is a
code to solve a problem into smaller parts (into modules). Function is a group of statements
used together to do the task. In program C has at least 1 main function, namely main (); And
in a program can make other functions besides the main function ();
o Function in the C language is divided into 2 groups, namely: Standard Library Function. and
programmer-defined function
o Identifier Scoping, based on its reach identifier is divided into 2, namely: Global Identifier is
an identifier that is declared outside the function and placed above all functions in a program
and local identifier is an identifier that is declined in the function, including in the parameter
list. The reach is limited to the function itself.
o Passing parameters are one function that calls another function to carry out certain tasks, to
the function called (Called Function) is given all the data needed to carry out the tasks given
by the caller's function. Delivery of parameters in language c there are 2 types namely call by
value and call by reference/location
o Recursive (recursion) is a process that calls him back. In the recursive C language carried out
by function.
ThankYOU...
REFERENCES
o Paul Deitel & Harvey Deitel. (2022). C how to program. 09. Pearson Education. Hoboken.
ISBN: 978-0-13-739839-3. Chapter 5
o Functions in C: http://aelinik.free.fr/c/ch15.htm