0% found this document useful (0 votes)
16 views39 pages

CTSD 4

Uploaded by

vivocem863
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views39 pages

CTSD 4

Uploaded by

vivocem863
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Faculty of Engineering and Technology

Parul Institute of Technology


Department: Computer Science & Engineering
Course: B.Tech – CSE

Subject: Computational Thinking for Structured Design-1


Subject Code: 303105104
UNIT 4
PART 1

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 Function with


arguments Types of no arguments
and with Functions and with
return value 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; }

return x; int sum()


{
} int a = 50, b = 80, sum;
sum = sqrt(a) + sqrt(b);
return sum;
}
Function with argument and with
no return value:
#include<stdio.h>
• Function declaration : void function ( int ); void add(int,int);
int main()
• Function call : function( x ); {
int a,b;
• Function definition: printf("\nEnter The Value of A & B : ");
void function( int x ) scanf("%d%d",&a,&b);
//Function Calling
{ add(a,b); // Actual Parameters
return 0;
statements; }
void add(int x,int y) //Formal Parameters
} {
int c;
c=x+y;
printf("\nTotal : %d",c);
}
Function with arguments and with
return value:
• Function declaration : int function ( int ); #include<stdio.h>
int add(int,int);
• Function call : function( x ); int main()
{
• Function definition: int a,b;
int function( int x ) printf("\nEnter The Value of A & B : ");
scanf("%d%d",&a,&b);
{ a=add(a,b);
printf("\nTotal : %d",a);
statements; return 0;
}
return x;
int add(int x,int y)
} {
return x+y;
}
Call by Value:
#include <stdio.h>
• Call by value in C is where in the int sum(int x, int y)
arguments we pass value and {
int c;
that value can be used in c = x + y;
function for performing the return c;
}
operation.
• Values passed in the function int main()
{
are stored in temporary memory int a = 3, b = 2;
so the changes performed in the int c = sum(a, b);
printf("Sum of %d and %d : %d", a, b, c);
function don’t affect the actual return 0;
value of the variable passed. }
Call by Reference:
• Call by reference is the method in #include <stdio.h>
C where we call the function with void swap(int* x, int* y)
{
the passing address as arguments.
int temp = *x;
• We pass the address of the *x = *y;
memory blocks which can be *y = temp;
}
further stored in a pointer variable
int main()
that can be used in the function. {
• Now, changes performed in the int x = 1, y = 5;
values inside the function can be printf("Before Swapping: x:%d , y:%d\n", x, y);
swap(&x, &y);
directly reflected in the main printf("After Swapping: x:%d , y:%d\n", x, y);
memory. return 0;
}
UNIT 4
PART 2

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.

Storage Class Scope Longevity


Automatic Within the block or function Until end of the block or function
External Throughout the entire program Throughout the entire program
Static Within the block or function Throughout the remainder of the program
Register Within the block or function Until end of the block or function
Automatic Variables:
• They are declared inside a function in which they are to be utilized.
• They are created when the function is called and destroyed
automatically when the function us exited.
• They are also known as Local or Internal variables.
• A variable is declared inside a function without storage class
specification is by default an automatic variable.
• They can be declared as follows:
auto int a; or int a;
Program Demonstrating the concept of Automatic Variables
External Variables:
• They are alive and active throughout the entire program.
• They are also known as Global variables.
• They can be accessed by any function in the program.
• They are declared outside a function.
Program Demonstrating the concept of External Variables
Static Variables:
• They persists until the end of the program.
• It may be either an internal or external depending on the place of the
declaration.
• Internal static variables are similar to automatic variable, except that
they alive throughout the remainder of the program.
• External static variable is declared outside of all the functions and it is
available to all the functions in the program.
Program Demonstrating the concept of Static Variables
Program Demonstrating the concept of Static Variables
Register Variables:
• We can tell the compiler that a variable should be kept in one of the
machine’s registers instead of keeping in the memory.
• Since a register accesses is much faster than a memory access,
keeping the frequently accessed variables in the registers will lead to
faster execution of programs.
• Even if ANSI C standards does not restrict its application to any
particular data type, most compilers allow only int or char variables to
be placed in the register.
Sample Questions:
1. What do you mean by Function in C programming?
2. Explain the basic structure of a function in C programming.
3. Explain different types of functions.
4. Explain Call by Value with a suitable example.
5. Explain Call by Reference with a suitable example.
6. What do you mean by Recursion in C programming?
7. Explain different types of Recursion.
8. Define Scope and Longevity.
9. Compare different storage classes based on Scope and Longevity.

You might also like