0% found this document useful (0 votes)
8 views

Function

c programming

Uploaded by

ambika venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Function

c programming

Uploaded by

ambika venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Parameter Passing Techniques in C

1. Pass By Value (call by value)


2. Pass by reference/pass by pointers(call by reference)

Pass By Value
 The values of actual parameters are copied into formal parameters
 The formal parameters contain only the copy of actual parameters
 If the values of the formal parameters changes in the called function, the
values of the actual parameters are not changed
Pass by reference/Pass by pointers
 The memory addresses of the variables rather than the copies of
values are sent to the called function
 In this case, the called function directly works on the data in the
calling function and the changed values are available in the calling
function for its use
Storage Classes
• Storage class of a variable defines scope(visibility) and lifetime of a variables
and/or functions declared within a c program
• storage class gives the following information about the variable
- determines the part of memory where storage space will be allocated for that
variable or function. memory(register or RAM)
- Specifies how long the storage allocation will continue to exist for that
function or variable
- Specifies the scope of the variable or function. Whether the variable/function
can be referenced throughout the program or only within the function, block,
or source file where it has been defined
- Specifies whether the variable or function has internal, external or no linkage

- Specifies whether the variable will be automatically initialized to zero or to any


indeterminate value
• C supports four storage classes:

1. Automatic

2. Register

3. External

4. static
1. Auto
 It is a default storage class for variables declared inside a block

 Ex: auto int x;

 Scope of the variable is local to the block in which it is declared

 All the local variables declared within a function belong to automatic storage class
by default
 They should be declared at the start of the program block(right after { )

 Memory is automatically allocated upon entry to a block and freed automatically


upon exit from that block
 Every time the block in which the automatic variable is declared is entered, the
value of the variable declared with initializers is initialized
 The auto variables are stored in the primary memory of the computer

 If auto variables are not initialized at the time of declaration, then they contain
Example:

#include<stdio.h> void main()


void function1() {
{ int a=30;
int a=10; function1();
printf(“a=%d\n”,a); function2();
} printf(“a=%d\n”,a);
}
void function2()
{ Output:
int a=20; a=10
printf(“a=%d\n”,a); a=20
} a=30
2. Register

 The register storage class is used to define local variables that should be
stored in a register instead of RAM
 Ex: register int x;
 the variable has a maximum size equal to the register size (usually one
word) and can't have the unary '&' operator applied to it (as it does not
have a memory location).
 The register should only be used for variables that require quick access
 Register variables also have automatic storage duration. Each time a
block is entered, the storage for register variables defined in that block is
accessible and the moment that block is exited , the variables becomes
no longer accessible for use
Example:

#include<stdio.h>
void main()
{
int a,b;
register int z;
printf(“Enter two number to add”);
scanf(“%d%d”,&a,&b);
z=a+b;
printf(“sum=%d”, z);
}
3.Extern

 The extern storage class is used to give reference of a global variable that is visible to

all the program files.

 Ex: extern int x;

 Memory is allocated for external variables when the program begins execution and

remains allocated until the program terminates.

 External variables may be initialized while they are declared

 In case the extern variable is not initialized, it will be initialized to zero by default

 External variables have global scope i.e, these variables are and accessible from all the

functions in the program

 The extern keyword is used with a variable to inform the compiler that this variable is

declared somewhere else. The extern declaration does not allocate storage for

variables
 The main purpose of using extern variables is that they can be accessed
between two different files which are part of a large program.
 In case of a local variable and a global variable have the same name, the
local variable will have precedence over the global one in the function
where it is declared.
 Let’s consider the use of extern in functions.
 when a function is declared or defined, the extern keyword is implicitly
assumed.
 Ex: When we write.

int foo(int arg1, char arg2);


The compiler treats it as:
extern int foo(int arg1, char arg2);
 Since the extern keyword extends the function’s visibility to the whole
program, the function can be used (called) anywhere in any of the files of
the whole program, provided those files contain a declaration of the
function.
4. static

 Static variables have lifetime over the entire program

 Memory is allocated when the program begins and is freed when the
program terminates
 Ex: static int x;

 A variable which is declared or initialized using static keyword always


contains zero as a default value.
 Static local variable is a local variable that retains and stores its value
between function calls or block and remains visible only to the function
or block in which it is defined.
 Static global variables are global variables visible only to the file in
which it is declared.
void print()
Example:
{
#include<stdio.h> static int x;
void print(void); int y=0;
int main() printf(“static integer var x=%d”,x);
{ printf(“integer var y=%d”,y);
printf(“first call of print()”); x++;
print(); y++;
printf(“second call of print()”); }
print(); Output:
return 0; first call of print()
} static integer var x=0
integer var y=0
second call of print()
static integer var x=1
integer var y=0
Comparison of storage classes
Recursive functions
 A function that calls itself is known as a recursive function
 Ex:
Example: calculating factorial of a number
n! = n x (n-1)! , 1!=1
Problem Solution
5! = 5 x 4 x 3 x 2 x 1!
= 5 x 4! =5x4x3x2x1
= 5 x 4 x 3! =5x4x3x2
= 5x 4 x 3 x 2! =5x4x6
= 5 x 4 x 3 x 2 x 1! = 5 x 24
= 120
Ex: program to compute factorial of a number

factorial( int n)
void main()
{
{
if(n==1)
int num,fact;
return 1;
printf(“enter number:”);
else
scanf(“%d”,&num);
return (n*factorial(n-1));
fact=factorial(num);
}
printf(“factorial of a number:”,fact);
}
Example 2: The fibonacci series

The fibonacci series can be given as


0 1 1 2 3 5 8 13 ……….
Fib(n) = Fib(n-1)+ Fib(n-2)
//Program to print fibonacci series

#include<stdio.h> int fib(int n)


int fib(int); {
void main() if(n==0)
{ return 0;
int n,i; if(n==1)
printf(“Enter the value of n:”); return 1;
scanf(“%d”,&n); return(fib(n-1)+fib(n-2));
for(i=0;i<n;i++) }
printf(“%d\t”,fib(i));
}
// Program to find the GCD & LCM using recursion
#include <stdio.h>
int gcd(int x, int y); int gcd(int x, int y)
void main()
{ {
int num1, num2, gcd1, lcm;
if (y == 0)
printf("Enter two integer
Values:\n"); return x;
scanf("%d %d", &num1,
&num2); else
gcd1 = gcd(num1, num2);
printf("GCD: %d", gcd1); return gcd(y, x % y);
Lcm=(num1*num2)/gcd1
}
printf("\nLCM: %d", lcm);
}

You might also like