CTSD 4
CTSD 4
Functions
Outline
• Introduction
• Types of functions
• Call by value
• Call by reference
Introduction:
• A function in C is a set of statements that when called perform some
specific task.
• It is the basic building block of a C program that provides modularity
and code reusability.
• The programming statements of a function are enclosed within { }
braces, having certain meanings and performing certain operations.
• They are also called subroutines or procedures in other languages.
Introduction:
• The syntax of function can be divided as follows:
• Function Declaration:
In a function declaration, we must provide the function name, its return type, and the number and
type of its parameters. A function declaration tells the compiler that there is a function with the
given name defined somewhere else in the program.
• Function Definition:
The function definition consists of actual statements which are executed when the function is called.
• Function Calls:
A function call is a statement that instructs the compiler to execute the function. We use the
function name and parameters in the function call.
Types of Functions:
Function with
no arguments
and no return
value
Function with
argument
and with no
return value
Function with no arguments and no
return value:
• Function declaration : void function(); #include <stdio.h>
void value(void);
• Function call : function(); void main()
{
• Function definition : value();
}
void function() void value(void)
{
{ float year = 1, period = 5, amount = 5000, inrate = 0.12;
float sum;
statements; sum = amount;
while (year <= period) {
} sum = sum * (1 + inrate);
year = year + 1;
}
printf(" The total amount is :%f", sum);
}
Function with no arguments and
with return value: #include <math.h>
#include <stdio.h>
• Function declaration : int function();
int sum();
• Function call : function();
• Function definition : int main()
{
int function() int num;
num = sum();
{ printf("Sum of two given values = %d", num);
return 0;
statements; }
Pointer Functions
Outline:
• Calling a function through function pointer
• Passing a function's address as an argument to other function
Calling a function through
function pointer
Passing a function's address as an argument
to other function
UNIT 3
PART 3
Recursion
Outline
• Indirect Recursion
• Direct Recursion
1. Tail Recursion
2. No tail/Head Recursion
3. Tree Recursion
4. Nested Recursion
Indirect Recursion:
• A function let say f1 is called indirect recursive if it calls another
function f2 and then that another function f2 calls f1 again.
Program to print numbers
from 1 to 10 in such a way
that when number is odd
and even, add and subtract 1
respectively.
Direct Recursion:
• A function is called directly recursive if it calls itself.
• Direct Recursion is categorized into the following types:
1. Tail Recursion
2. No tail/Head Recursion
3. Tree Recursion
4. Nested Recursion
Tail Recursion:
• If a recursive function calling itself and that recursive call is the last
statement in the function then it’s known as Tail Recursion.
• After that call the recursive function performs nothing.
No Tail/Head Recursion:
• If a recursive function calling itself and that recursive call is the first
statement in the function then it’s known as Tail Recursion.
• There’s no statement, no operation before the call.
Tree Recursion:
• If a recursive function calling itself for more than one time then it’s
known as Tree Recursion.
Nested Recursion:
• In this recursion, a recursive function will pass the parameter as a
recursive call. That means “Recursion Inside Recursion”.
UNIT 4
PART 4
Storage Classes
Outline:
• Introduction
• Some Basic Terminology
• Scope
• Visibility
• Longevity
• Automatic Variables
• External Variables
• Static Variables
• Register Variables
Introduction:
• In C not only do all variables have a data type, they also have a
storage class. According to this concept, variables are categorized into
four classes:
1. Automatic
2. External
3. Static
4. Register
Some Basic Terminology:
• Scope: It determines over what region of the program a variable is actually
available for use or a variable is active.
• Longevity: It refers to the period during which a variable retains a given value
during execution of the program or a variable remains alive.