Unit 4
Unit 4
Unit 4
Structured Design-1
Dr. Chintan B. Thacker, Assistant Professor, Computer Science & Engineering
CHAPTER-4
Functions , Pointer Functions, Recursion , Storage classes:
Contents
1. Functions
2. Pointer Functions
3. Recursion
4. Storage classes
1. Function :
• In the below example, the first sum function is called and 10,30 are
passed to the sum function. After the function call sum of a and b
is returned and control is also returned back to the main function
of the program.
Working concept :
• Function return type tells what type of value is returned after all function is executed.
• When we don’t want to return a value, we can use the void data type.
Example:
int func(parameter_1,parameter_2);
The above function will return an integer value after running statements inside the function.
Note: Only one value can be returned from a C function. To return multiple values, we have to use pointers or
structures.
Function Arguments:
• Function Arguments (also known as Function Parameters) are the data that is passed to a function.
Example:
For Example:
• Functions that the programmer creates are known as User-Defined functions or “tailor-made functions”.
User-defined functions can be improved and modified according to the need of the programmer.
• Whenever we write a function that is case-specific and is not defined in any header file, we need to
declare and define our own functions according to the syntax.
// Driver code
int main()
{
int a = 30, b = 40;
// function call
int res = sum(a, b);
• The data passed when the function is being invoked is known as the Actual parameters.
• In the below program, 10 and 30 are known as actual parameters.
• Formal Parameters are the variable and the data type as mentioned in the function
declaration.
• In the below program, a and b are known as formal parameters.
Example:
We can pass arguments to the C function in two ways:
Call by Value
call by Reference
1. Call by Value:
Parameter passing in this method copies values from actual parameters into
formal function parameters. As a result, any changes made inside the
functions do not reflect in the caller’s parameters.
call by Value example :
►
// C program to show use int var1 = 3, var2 = 2;
// of call by value printf("Before swap Value of var1 and var2 is:
#include <stdio.h> %d, %d\n",
var1, var2);
void swap(int var1, int var2)
{ swap(var1, var2);
int temp = var1; printf("After swap Value of var1 and var2 is:
var1 = var2; %d, %d",
var2 = temp; var1, var2);
} return 0;
}
// Driver code
int main()
Before swap Value of var1 and var2 is: 3,
{
2
After swap Value of var1 and var2 is: 3, 2
2. Call by Reference
► The caller’s actual parameters and the function’s actual parameters refer to
the same locations, so any changes made inside the function are reflected in
the caller’s actual parameters.
• The function can reduce the repetition of the same statements in the program.
• There is no fixed number of calling functions it can be called as many times as you
want.
• The function reduces the size of the program.
Once the function is declared you can just use it without thinking about the internal
working of the function.
Disadvantages of Functions in C
void test() {
Address of variable = 0x7ffd7f36a224
// test function that does nothing
Address of a function = 0x55f8e8abb169
return ;
}
int main() {
int a = 5;
// printing the address of variable a
printf("Address of variable = %p\n", &a);
From this example we can observe that just like a variable has
// printing the address of function main()
an address in memory, our function test() also has an address
printf("Address of a function = %p", test);
return 0;
}
Function Pointer in C
• For example, in the above figure we have a function add() to add two
integer numbers. Here, the function name points to the address of the
function itself so we are using a function pointer fptr that stores the
address of beginning of the function add(a, b) that in 1001 in this case
• Syntax of Function Pointer in C:
• Suppose we declare a function and its pointer as given below: int length = 5;
int areaSquare (int); // function declaration // Different ways to call the function
int (*pointer) (int); // function pointer declaration // 1. using function name
int area = areaSquare(length);
pointer = areaSquare;
// 2. using function pointer (a)
To call the function areaSquare, we can create a function call using int area = (*pointer)(length);
Any of the 3 ways:
// 3. using function pointer (b)
It is good practice to use the indirection operator to clear out that int area = pointer(length);
function is called using a pointer as (*pointer)()
Calling a Function Through a Function Pointer in C
#include<stdio.h> // pointing the pointer to functions memory address
fp = areaRectangle;
// function declaration
int areaRectangle(int, int); // calling the function using function pointer
area = (*fp)(length, breadth);
int main() {
int length, breadth, area; printf("Area of rectangle = %d", area);
return 0;
// function pointer declaration }
// note that our pointer declaration has identical
// arguments as the function it will point to // function definition
int (*fp)(int, int); int areaRectangle(int l, int b) {
int area_of_rectangle = l * b; Enter length and breadth of a
printf("Enter length and breadth of a rectangle\n"); return area_of_rectangle; rectangle
scanf("%d%d", &length, &breadth); } 59
Area of rectangle = 45
Passing A function's address as an Argument to other Function
function
• We cannot pass the function as an argument to another function.
• But we can pass the reference of a function as a parameter by using a function pointer.
• This process is known as call by reference as the function parameter is passed as a pointer that
holds the address of arguments.
• Therefore, C programming allows you to create a pointer pointing to the function, which can be
further passed as an argument to the function.
Function Pointer passed as an Argument
int cube(int a) { // sum = 2^2 + 3^2, as fp points to
#include<stdio.h>
// function return cubic power of a number sqaure()
return a * a * a; int sum = conditionalSum(2, 3, fp);
int conditionalSum(int a, int b,int (*ptr)())
} printf("Square sum = %d\n", sum);
{
// modify the arguments according to the
int main() { // point function pointer to cube()
//condition of the function ptr points to
int (*fp)(int); fp = cube;
a = (*ptr)(a);
// point function pointer to function square()
b = (*ptr)(b);
fp = square; // sum = 2^3 + 3^3, as fp points to
return a + b; function cube()
// sum = 2^2 + 3^2, as fp points to sqaure() sum = conditionalSum(2, 3, fp);
}
int sum = conditionalSum(2, 3, fp); printf("Cubic sum = %d", sum);
printf("Square sum = %d\n", sum); return 0;
int square(int a) {
// function return square power of a number }
// point function pointer to function cube() Square sum = ?
return a * a;
fp = cube; Cubic sum = ?
}
Types of Pointer function
• The type of a pointer to a function is based on both the return type and parameter types of the function
• Null Pointer - If you assign a NULL value to a pointer during its declaration, it is called Null Pointer.
Syntax: int *var = NULL;
• Void Pointer – When a pointer is declared with a void keyword, then it is called a void pointer. To print the
value of this pointer, you need to typecast it.
Syntax: void *var; E.g. int a=2; void *ptr; ptr=&a; printf(“Typecasting a=%d”, *(int *)ptr);
Types of Pointer function
• Wild Pointer – A wild pointer is only declared but not assigned an address of any variable. They are very tricky
and cause segmentation errors
Types of Pointer function
• Dangling Pointer – Suppose there is a pointer p pointing at a variable at memory 1004. If you deallocate this
memory, then this p is called a Dangling pointer
• You can deallocate a memory using free() function
Recursion:
• Recursion is the process in which a function calls itself up to n-number of times.
• If a program allows the user to call a function inside the same function recursively, the procedure is called a
recursive call of the function
void recursion()
{
recursion(); //The recursive function calls itself inside the same function
}
int main()
{
recursion(); //function call
}
Types of Recursion
• Following are the types of the recursion in C programming language, as follows:
1.Direct Recursion
2.Indirect Recursion
3.Tail Recursion
4.No Tail/ Head Recursion
5.Tree Recursion
Direct Recursion
• When a function calls itself within the same function repeatedly, it is called the direct recursion.
In-Direct Recursion
• When a function is mutually called by another function in a circular manner, the function is called an indirect recursion
function.
In this structure, there are four functions, fun1(), fun2(),
fun3() and fun4().
When the fun1() function is executed, it calls the fun2() for its
execution.
And then, the fun2() function starts its execution calls the
fun3() function.
•Automatic
•External
•Static
•Register
Automatic
•Automatic variables are allocated memory automatically at runtime.
•The visibility of the automatic variables is limited to the block in which they are defined.
•The scope of the automatic variables is limited to the block in which they are defined.
•The memory assigned to automatic variables gets freed upon exiting from the block.
• Static local variables are visible only to the function or the block in which they are defined.
• A same static variable can be declared many times but can be assigned at only one time.
• The visibility of the static global variable is limited to the file in which it has declared.
• We can not dereference the register variables, i.e., we can not use &operator for the register variable
• The access time of the register variables is faster than the automatic variables.
• The register keyword is used for the variable which should be stored in the CPU register.
Register
External
• The external storage class is used to tell the compiler that the variable defined as extern is declared with an
external linkage elsewhere in the program.
• The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that
the variable is declared elsewhere in the program.
• We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any block or
method.
• An external variable can be declared many times but can be initialized at only once.
External
External
External