M3 P1.pptx
M3 P1.pptx
C
22POP13
Module-3
• Functions: Introduction using functions, Function
definition, function declaration, function call, return
statement, passing parameters to functions, scope of
variables, storage classes, recursive functions.
• Arrays: Declaration of arrays, accessing the
elements of an array, storing values in arrays,
Operations on arrays, Passing arrays to functions,
two dimensional arrays, operations on
two-dimensional arrays, two dimensional arrays to
functions, multidimensional arrays, applications of
arrays.
Functions
• Function is a group of statements that perform a
particular task.
• C functions can be of 2 categories:
1)Built in functions.
• It is built in function or library function which is
provided by the C compiler. User is not required to
write it.
• E.g. printf(), scanf()
2)User defined functions It is written by the user.
• C programs can be written in 2 ways:
• Without using user defined functions or
Unstructured programming
• Using user defined functions or Structured
programming or modular programming.
• In unstructured programs entire logic is written
inside one function which is main () function.
#include<stdio.h>
void main()
{
int a=10,b=20, sum;
sum=a+b;
printf("Sum is %d\n",sum);
}
Output:
Sum is 30
• Example of structured C program
#include<stdio.h>
int add(int a, int b); //function declaration
void main()
{
int a=10,b=20,sum;
sum=add(a,b); //function call
printf("Sum is %d\n",sum);
}
int add(int a, int b) //function definition
{
int sum;
sum=a+b;
return sum; Output:
Sum is 30
}
ELEMENTS OF USER DEFINED FUNCTIONS:
• Function definition
• Function call
• Function declaration
Function definition
• Actual code for the function to perform the
specific task. It contains two parts:
• Function header, consists of return type,
function name, formal parameter list.
• Function body.
return_type function_name(parameter list)
{
local variable declaration;
executable statement1;
executable statement2;
…
…
return statement;
}
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}
----------------------------------------------------------------
void add(int a, int b)
{
printf(“ sum is %d”,a+b);
}
FUNCTION DEFINITION and FUNCTION
CALL
void main()
{
int a=10,b=20, sum;
sum=add(a,b); //function call
printf(“sum is %d”,sum);
}
int add(int a, int b) //function definition
{
int sum;
sum= a+b;
return sum;
}
/* Program to find product of given two integers */
#include<stdio.h> Function
int mul(int a, int b); // int mul(int, int); Declaration(FUNCTION
void main() PROTOTYPE)
{
int a, b, c;
printf(“Enter two integers\n”);
scanf(“%d%d”,&a,&b);
c=mul(a,b);
printf(“The Product is = %d”,c); Function Call
}
int mul(int a, int b)
{
return(a*b);
}
• Function declaration or function prototype
return_type function_name (parameter list);
int temp;
temp=p;
p=q;
q=temp; Output:
} Values before exchanging are 10 and 20
Values after exchanging are 10 and 20
void main()
{
int a=10,b=20;
printf("Values before exchanging are %d and
%d\n",a,b);
exchange(a,b);
printf("Values after exchanging are %d and %d\n",a,b);
}
Pass by reference or pass by pointer
• In this method address of the variable is passed to the
function during function call.
• If any change is done to formal parameters, then
actual parameters are changed.
• Actual and formal parameters are created in same
memory location.
• Pointers are required.
#include <stdio.h>
void exchange(int *p, int *q)
{
Output:
int temp; Values before exchanging are 10 and 20
temp=*p; Values after exchanging are 20 and 10
*p=*q;
*q=temp;
}
void main()
{
int a=10,b=20;
printf("Values before exchanging are %d and %d\n",a,b);
exchange(&a,&b);
printf("Values after exchanging are %d and %d\n",a,b);
}
POINTER
Pointer is a variable which holds address of another variable.
Pointer Variables
p a
2000 5
3000 2000
Address
Scope of variables
goto label_exec;
}
Program Scope
print();
}
void print()
{
printf (" \n The value of x in function %d", x);
}
File Scope
• When a global variable is accessible until the end
of the file, the variable is said to have file scope.
• To allow a variable to have file scope, declare that
variable with the static keyword before specifying
its data type, as shown:
static int x = 10;
• A global static variable can be used anywhere
from the file in which it is declared but it is not
accessible by any other files.
file2.c file1.c
#include<stdio.h> void fun()
#include "file1.c" {
int a=5; -------------
void main() a=a+3;
{ -------------
printf(“%d\t”,a); }
fun();
printf(“%d\t”,a);
}
file2.c file1.c
#include<stdio.h> void fun()
#include "file1.c" {
static int a=5; -------------
void main() a=a+3;
{ -------------
printf(“%d\t”,a); }
fun();
printf(“%d\t”,a);
}
Storage class
• Storage class specifiers define the scope
(visibility) and lifetime of variables declared
within a C program.
• The scope of the variable indicates the part of
the C program in which the variable name is
visible or accessible.
• Lifetime of variables indicates how long the
storage allocation will continue to exist for
that variable.
• C supports the following storage classes :
• Auto
• Register
• External
• Static
auto Storage Class(Automatic variables)
• Syntax:
auto datatype variable;
• These variables are declared inside a function
and used only inside that function.
• They are created when function is called and
destroyed automatically when the function is
exited. Hence name automatic.
• By default automatic variables are initialized
to garbage (Junk) value.
#include<stdio.h>
void f2()
{ void main()
auto int a=10; {
printf("%d\t",a); auto int a=1000;
} f1();
void f1() printf("%d",a);
{ }
auto int a=100;
f2(); OUTPUT
printf("%d\t",a); 10 100 1000
}
register Storage Class (register variables)
Syntax:
register datatype variable;
• When a variable is declared using register storage
class, it is stored in a CPU register instead of RAM.
• These are accessed faster than memory.
• Frequently used variables (looping variables) will
be kept in register.
• These variables do not have address to access and
hence they cannot be operated using & operator.
register Storage Class
#include <stdio.h>
void main ()
{
int n;
register int i, sum;
printf (" enter the value for n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=sum+i;
}
}
extern Storage Class(External variables)
Output: 5 8
#include<stdio.h>
int x;
void f()
{
extern int x;
x=x+5;
printf(“%d\t”,x);
} Output:
void main() 5 10
{
extern int x;
x=x+5;
printf(“%d\t”,x);
f();
}
#include<stdio.h>
int x;
void f()
{
int x;
x=x+5;
printf(“%d\t”,x);
} Output:
void main() xxxxx xxxxx
{
int x;
x=x+5;
printf(“%d\t”,x);
f();
}
static storage class(Static variables)
{
increment();
increment();
increment();
}
• Static storage class can be specified for extern
variables. For example:
static extern int x;
• When we declare a variable as extern static
variable, then that variable is accessible from
all the functions in this source file.
Qualifier Storage Initial Scope Lifetime
value
Auto CPU Garbage Inside End of block
memory function
Register CPU Garbage Inside End of block
registers function
Extern CPU 0 Throughout Till the end
memory of the
program
Static CPU 0 Inside End of
memory function program
Recursion
• Recursion is the process of defining a problem
(or the solution to a problem) in terms of (a
simpler version of) itself.
• For example, we can define the operation
"find your way home" as:
• If you are at home, stop moving.
• Take one step toward home.
• "find your way home".
Recursive functions
• Recursive function( Recursion) is defined as a
function that calls itself until terminating
condition.
• Recursive solution has two major cases:
• Base case, in which the problem is solved directly
which does not require a cal to itself.
• Recursive case, in which the problem is divided
into sub-parts. The function calls itself with
sub-parts of the problem. Then result is obtained
by combining the solutions of sub-parts.
Write a C program to calculate factorial of a
number using recursion.
• Base case, is when n=1, the factorial is 1
• Recursive case is factorial(n)= n* factorial(n-1)
Base case, is when n=1, the factorial is 1
Recursive case is factorial(n)= n* factorial(n-1)
printf("Enter a number\n");
scanf("%d",&n);
printf("Fibonacci series is "); Output:
for(i=0;i<n;i++) Enter a number
{ 3
res=fib(i); Fibonacci series is 0 1 1
printf("%d\t",res);
}
return 0;
}
• Write a program to calculate GCD using
recursive functions.