CP - Unit-3
CP - Unit-3
UNIT – III
Functions-
Introduction to Structured Programming
Functions- basics, user defined functions
Inter function communication (call by value, call by
reference)
Standard functions
Storage classes-auto, register, static, extern, scope rules
Arrays to functions
Recursive functions
Example C programs.
Functions: Structured Programming
The planning for large programs consists of first understanding the problem as a
whole, second breaking it into simpler, understandable parts.
We call each of these parts of a program a module and the process of subdividing a
problem into manageable parts top-down design.
Top-down design is usually done using a visual representation of the modules known
as a structure chart.
Functions
Program segment that carries out some specific, well-defined task
Example
A function to add two numbers
A function to find the largest of n numbers
A function will carry out its intended task whenever it is called or invoked.
Can be called multiple times.
Every C program has at least one function, which is main().
Advantages of functions:
1. Write your code as collections of small functions to make your program modular
2. Structured programming
3. Code easier to debug
4. Easier modification
5. Reusable in other programs
C functions aspects Syntax
#include <stdio.h>
function definition
Declaring User-defined functions
Syntax
Example
Definition of User-defined functions
Syntax
Example
Calling User-defined functions
Syntax
Example
Working of Functions
inter function communication
(call by value
call by reference)
void add( )
Control {
int a,b,c;
printf(Enter any two numbers: );
scanf(“%d%d”, &a, &b);
No return value c = a + b;
printf(“Result = %d”, c);
}
With Parameters and without Return value
void add( )
{
Control int a,b;
printf(Enter any two numbers: );
30 as return value scanf(“%d%d”, &a, &b);
return (a + b);
}
void main()
{
int c;
int add( int, int );
printf(“We are in main….\n”);
c = add( 10, 20 );
printf(“Result = %d\n”, c);
}
int add( int a, int b)
{
return (a + b);
}
void main( )
{
int c;
int add( int, int );
printf(“We are in main….\n”); 10 & 20 as input
c = add( 10, 20 );
printf(“Result = %d\n”, c);
} Control
Control
void add( int a, int b)
{
30 as return value return (a + b);
}
Storage classes
- Auto
- Register
- Static
- Extern
Why Storage Classes?
• Where the variable would be stored?
– Memory or CPU Registers
• What will be the initial value of the variable?
– i.e., default value (Starting value)
• What is the scope of the variable?
– For which function the value of the variable would be
available
• What is the life time of a variable?
– How long would be variable exists
Storage classes are used to define things like storage location (whether RAM or
REGISTER), scope, lifetime and the default value of a variable.
Storage Memory
Output: 20 10
Example 3
#include<stdio.h>
int main(){
{
int a=20;
printf("%d",a);
}
printf(" %d",a); //a is not visible here
return 0;
}
Output: 20 20 20 20
Register Storage Class
Storage Register
Output: 0
Example 2
#include<stdio.h>
static int a;
int main(){
printf("%d",a);
return 0;
}
Output: 0
Example 3
#include <stdio.h>
static char c;
static int i;
static float f;
int main(){
printf("%d %d %f",c,i,f);
return 0;
}
Output: 0 0 0.000000
Example 4
#include <stdio.h>
static int i=10;
int main(){
i=25; //Assignment statement
printf("%d",i);
return 0;
}
Output: 25
Example 5
#include<stdio.h>
int main(){
{
static int a=5;
printf("%d",a);
}
//printf("%d",a); variable a is not visible here.
return 0;
}
Output: 5
External Storage Class
Storage Memory
Output: 0
Example 2
#include <stdio.h>
extern int i; //extern variable
int main(){
printf("%d",i);
return 0;
}
Output: 15
Example 4
#include <stdio.h>
extern int i=10; //extern variable
int main(){
printf("%d",i);
return 0;
}
Output: 10
Example 5
#include <stdio.h>
int main(){
extern int i=10; //Try to initialize extern variable
locally.
printf("%d",i);
return 0;
}
Infinite recursion occurs if the recursion An infinite loop occurs with iteration
step does not reduce the problem in a
manner that converges on some if the loop-condition test never
condition.(base case) becomes false
Recursion terminates when a base case Iteration terminates when the
is recognized loop-condition fails
Recursion is usually slower then iteration Iteration does not use stack so it's
due to overhead of maintaining stack
faster than recursion
Recursion uses more memory than Iteration consume less memory
iteration
Infinite recursion can crash the system infinite looping uses CPU cycles
repeatedly
Recursion makes code smaller Iteration makes code longer
Example Program:
int factorial( int );
void main( )
{
int fact, n;
printf(“Enter any positive integer: ”);
scanf(“%d”, &n);
fact = factorial( n );
printf(“Factorial of %d is %d”, n, fact);
}
int factorial( int n )
{
int temp;
if( n == o)
return 1;
else
temp = n * factorial( n-1 );
return temp;
}
int factorial( int );
void main( )
{
int fact, n;
printf(“Enter any positive integer: ”);
scanf(“%d”, &n);
fact = factorial( n );
printf(“Factorial of %d is %d”, n, fact);
}
int main() {
int ageArray[] = {2, 8, 4, 12};
int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
return sum;
}