0% found this document useful (0 votes)
27 views33 pages

Unit1 - Procedural Theory

Uploaded by

santhosh kumar
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)
27 views33 pages

Unit1 - Procedural Theory

Uploaded by

santhosh kumar
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/ 33

SRM Institute of Science and Technology

Advanced Programming Practice-18CSC207J

Unit 1
Overview of the Unit

 Structured Programming Paradigm

 Procedural Programming Paradigm

 Object Oriented Programming Paradigm


Procedural Programming Paradigm

Session 6 – 10 covers the following Topics:-


 Procedural Programming Paradigm
 Routines, Subroutines, functions
 Using Functions in Python
 logical view, control flow of procedural programming in various aspects
 Other languages: Bliss, ChucK, Matlab
 Demo: creating routines and subroutines using functions in Python
 Lab 2: Procedural Programming

TextBook: Shalom, Elad. A Review of Programming Paradigms Throughout the History: With a
Suggestion Toward a Future Approach, Kindle Edition
Procedural Programming
 History and Languages
 Overview
 Concepts and their representation in C
 Modularity
 Variables
 Special variables - pointers
 Variable scope
 Functions
 Recursion
 Static functions
 Example with different procedural approaches
 Review of concepts in other languages
History and Languages
 The high-level form rather than time-consuming lower level
languages - assembly and machine language.
 Machine-independent form - portable, lifetime andusefulness
 Procedural programming was born around 1958,
before structured programming and other paradigms.
 Procedural programming was necessary to enable
breaking complex programs into smaller units –
procedures
 Compiler Vs Interpreter
 Procedural – either compiled or interpreted
 FORTRAN – implemented with compiler
 BASIC – implemented with interpreter
 Programming language : ALGOL , COBOL , Pascal, Modula, C
Compiler Vs Interpreter
COMPILER INTERPRETER

A compiler reads the An interpreter translates and


entire program, makes a executes the program one
translation, and produces a instruction at a
complete binary code time, so a program written
version,which is then loaded into in an interpreted
the computer and language must be interpreted
executed. Once the program each time it is run.
is compiled, neither the
original program nor the
compiler is needed.

Compiled programs execute Interpreted programs are


faster, easier to correct or modify
Overview

 Procedural programming is a list or set of


instructions telling a computer what to do
step by step and how to perform from the
first code to the second code.

 Procedural programming relies on procedures,


also known as routines
Concepts and their representation in C-
Modularity
 “Modularity is the degreeto which a system's
components may be separated and recombined.”

 Advantages of Modularity

 A changeto one file means that only that file needs


to be recompiled

 Easy to edit and navigate smaller self-contained files

 Allows more than one person to work on a particular


project at once

 Simpler to reuse code that is in a self-contained


module or file

 Easy to isolate compile-time and run-time bugs


Variables
 A variable is a name given to a storage
area that programscan manipulate
 Variable naming construct
 Letters, digits, and the underscore character.
 Begins with eithera letter or an underscore.
 C is case-sensitive
 C programming language also allows definition of
various other typesof variables like Enumeration, Pointer,
Array, Structure, Union, etc.
typevariable_list;
typevariable_name = value;
Variable types in C
Type Description
char Typically a singleoctet(one
byte).
int The most natural size of
integer for the
machine.
float A single-precision floating
point value.

Double A double-precision floating


point value.
void Represents the absence of
type.
Variable default initialization

 Variables with static storage duration


are implicitly initialized with NULL (all
bytes have the value 0);
 Initial value of all other
variables is undefined.
Variable declaration
 A variable declaration has its meaning at the
time of compilation only; compiler needs actual
variable declaration at the time of linking
of the program.
 Extern keyword is used to declare a
variable at any place.
 Though a variable can be declared
multiple times in a C program, it can
be defined only once in a file, a function or
a block of code.
/* actual initialization */
#include <stdio.h> a = 10;
// Variable declaration: b = 20;
extern int a, b;
extern int c; c = a + b;
extern float f; printf("value of c : %d
int main () \n", c);
{ f = 70.0/3.0;
/* variable definition: printf("value of f : %f
*/ \n", f);
int a, b;
int c; return 0;
float f;
}
Function
// function declaration
int func();

int main()
{
// function call
int i = func();
}

// function definition
int func()
{
return 0;
}
Special variables - pointers

 A pointer is a variable whose value is


the addressof anothervariable, i.e., direct address
of the memory location

type *var-name;
int *ip; /* pointer to an
integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a
float */
char *ch /* pointer to a
character */
int *ptr = NULL;
Variable scope
 A scopein programming is a region of
the program where a defined variable existsbut
cannot be accessed beyond it.
 Inside a function or a block where they are
called local variables;
 Outside of all functions where they are called
global variables.
 In the definition of function parameters where they
are called formal parameters.
Local Vs Global Variable
#include <stdio.h>

/* global variable declaration */


int g = 20;

int main ()
{
/* local variable declaration */
int g = 10;

printf ("value of g = %d\n", g);

return 0;
}
Local Vs Global Variable
#include <stdio.h>

/* global variable declaration */


int g = 20;

int main ()
{
Output
value of g
/* local variable declaration */ = 10
int g = 10;

printf ("value of g = %d\n", g);

return 0;
}
Functions
 A special function with the name main() is
the first one to run when the program starts.

 All other functions are subroutines of the main() function


can have any name
type name(type1 arg1, type2 arg2, ...) /* function head*/

/* code = function block */


Examples on function return type

 int rename();
 extern int rename();
 void sayHello(int number_of_times)
Functions using Arrays
Call by Value
Call by Reference
Recursion
Static functions
Example with differentprocedural approaches
Using Recursion
Review of concepts in other
languages
 ALGOL
 Functional procedures
 Functional procedures are the procedures that have
resulting value
 Proper procedures
 proper procedure do not return a value.
 While both types are just called functions in C, ALGOL
and some other languages distinguish these types.
 In case of a proper procedure, there is no
need to write void to signalize that there is no
returning type. Instead, simply procedure is written
without any type coming before it.
 In FORTRAN and BASIC, user-defined functions
Characteristics of Procedure-Oriented Programming

 Emphasis is on doing things.

 Large programs are divided into smaller programs


known as functions.
 Most of the functions share global data.

 Data move openly around the system from function to


function.
 Functions transform data from one form to another.

 Employs top-down approach in program design.

You might also like