SlideShare a Scribd company logo
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Affiliated Institution of G.G.S.IP.U, Delhi
INTRODUCTION TO PROGRAMMING
LANGUAGE USING C
Topic: Functions
Paper Id: 20105
Paper code: BCA 105
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Functions
Outline
1 Introduction
2 Program Modules in C
3 Math Library Functions
4 Functions
5 Function Definitions
6 Function Prototypes
7 Header Files
8 Calling Functions: Call by Value and Call by Reference
9 Random Number Generation
10 Example: A Game of Chance
11 Storage Classes
12 Scope Rules
13 Recursion
14 Example Using Recursion: The Fibonacci Series
15 Recursion vs. Iteration
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
• Divide and conquer
– Construct a program from smaller pieces or
components
• These smaller pieces are called modules
– Each piece more manageable than the original
program
Introduction
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Program Modules in C
• Functions
– Modules in C
– Programs combine user-defined functions with library
functions
• C standard library has a wide variety of functions
• Function calls
– Invoking functions
• Provide function name and arguments (data)
• Function performs operations or manipulations
• Function returns results
– Function call analogy:
• Boss asks worker to complete task
– Worker gets information, does task, returns result
– Information hiding: boss does not know details
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Math Library Functions
• Math library functions
– perform common mathematical calculations
– #include <math.h>
• Format for calling functions
– FunctionName( argument );
• If multiple arguments, use comma-separated list
– printf( "%.2f", sqrt( 900.0 ) );
• Calls function sqrt, which returns the square root of its
argument
• All math functions return data type double
– Arguments may be constants, variables, or
expressions
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Functions
• Functions
– Modularize a program
– All variables declared inside functions are local variables
• Known only in function defined
– Parameters
• Communicate information between functions
• Local variables
• Benefits of functions
– Divide and conquer
• Manageable program development
– Software reusability
• Use existing functions as building blocks for new programs
• Abstraction - hide internal details (library functions)
– Avoid code repetition
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Function Definitions
• Function definition format
return-value-type function-name( parameter-list )
{
declarations and statements
}
– Function-name: any valid identifier
– Return-value-type: data type of the result (default
int)
• void – indicates that the function returns nothing
– Parameter-list: comma separated list, declares
parameters
• A type must be listed explicitly for each parameter unless,
the parameter is of type int
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Function Definitions
• Function definition format (continued)
return-value-type function-name( parameter-list )
{
declarations and statements
}
– Declarations and statements: function body (block)
• Variables can be declared inside blocks (can be nested)
• Functions can not be defined inside other functions
– Returning control
• If nothing returned
– return;
– or, until reaches right brace
• If something returned
– return expression;
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Function Prototypes
• Function prototype
– Function name
– Parameters – what the function takes in
– Return type – data type function returns (default int)
– Used to validate functions
– Prototype only needed if function definition comes after use in
program
– The function with the prototype
int maximum( int, int, int );
• Takes in 3 ints
• Returns an int
• Promotion rules and conversions
– Converting to lower types can lead to errors
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Header Files
• Header files
– Contain function prototypes for library functions
– <stdlib.h> , <math.h> , etc
– Load with #include <filename>
#include <math.h>
• Custom header files
– Create file with functions
– Save as filename.h
– Load in other files with #include
"filename.h"
– Reuse functions
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Calling Functions: Call by Value and
Call by Reference
• Used when invoking functions
• Call by value
– Copy of argument passed to function
– Changes in function do not effect original
– Use when function does not need to modify argument
• Avoids accidental changes
• Call by reference
– Passes original argument
– Changes in function effect original
– Only used with trusted functions
• For now, we focus on call by value
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Random Number Generation
• rand function
– Load <stdlib.h>
– Returns "random" number between 0 and RAND_MAX (at
least 32767)
i = rand();
– Pseudorandom
• Preset sequence of "random" numbers
• Same sequence for every function call
• Scaling
– To get a random number between 1 and n
1 + ( rand() % n )
• rand() % n returns a number between 0 and n - 1
• Add 1 to make random number between 1 and n
1 + ( rand() % 6)
– number between 1 and 6
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Random Number Generation
• srand function
– <stdlib.h>
– Takes an integer seed and jumps to that location
in its "random" sequence
srand( seed );
– srand( time( NULL ) ); //load
<time.h>
• time( NULL )
– Returns the time at which the program was compiled in
seconds
– “Randomizes" the seed
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Storage Classes
• Storage class specifiers
– Storage duration – how long an object exists in memory
– Scope – where object can be referenced in program
– Linkage – specifies the files in which an identifier is known
(more in Chapter 14)
• Automatic storage
– Object created and destroyed within its block
– auto: default for local variables
auto double x, y;
– register: tries to put variable into high-speed registers
• Can only be used for automatic variables
register int counter = 1;
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Storage Classes
• Static storage
– Variables exist for entire program execution
– Default value of zero
– static: local variables defined in functions.
• Keep value after function ends
• Only known in their own function
– extern: default for global variables and
functions
• Known in any function
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Scope Rules
• File scope
– Identifier defined outside function, known in all
functions
– Used for global variables, function definitions,
function prototypes
• Function scope
– Can only be referenced inside a function body
– Used only for labels (start:, case: , etc.)
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Scope Rules
• Block scope
– Identifier declared inside a block
• Block scope begins at declaration, ends at right brace
– Used for variables, function parameters (local
variables of function)
– Outer blocks "hidden" from inner blocks if there is
a variable with the same name in the inner block
• Function prototype scope
– Used for identifiers in parameter list
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Recursion
• Recursive functions
– Functions that call themselves
– Can only solve a base case
– Divide a problem up into
• What it can do
• What it cannot do
– What it cannot do resembles original problem
– The function launches a new copy of itself (recursion step) to
solve what it cannot do
– Eventually base case gets solved
• Gets plugged in, works its way up and solves whole problem
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75
Recursion
• Example: factorials
– 5! = 5 * 4 * 3 * 2 * 1
– Notice that
• 5! = 5 * 4!
• 4! = 4 * 3! ...
– Can compute factorials recursively
– Solve base case (1! = 0! = 1) then plug in
• 2! = 2 * 1! = 2 * 1 = 2;
• 3! = 3 * 2! = 3 * 2 = 6;
Ad

More Related Content

What's hot (15)

JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
Ravi_Kant_Sahu
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nilesh Dalvi
 
Clojure - An Introduction for Java Programmers
Clojure - An Introduction for Java ProgrammersClojure - An Introduction for Java Programmers
Clojure - An Introduction for Java Programmers
elliando dias
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
Abdul Hannan
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
Internationalization
InternationalizationInternationalization
Internationalization
Ravi Kant Sahu
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
Rohit Verma
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
Ravi_Kant_Sahu
 
Object Oriented Programming lecture 1
Object Oriented Programming lecture 1Object Oriented Programming lecture 1
Object Oriented Programming lecture 1
Anwar Ul Haq
 
Basic IO
Basic IOBasic IO
Basic IO
Ravi_Kant_Sahu
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
Clojure - An Introduction for Java Programmers
Clojure - An Introduction for Java ProgrammersClojure - An Introduction for Java Programmers
Clojure - An Introduction for Java Programmers
elliando dias
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
Abdul Hannan
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
Rohit Verma
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
Ravi_Kant_Sahu
 
Object Oriented Programming lecture 1
Object Oriented Programming lecture 1Object Oriented Programming lecture 1
Object Oriented Programming lecture 1
Anwar Ul Haq
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 

Viewers also liked (13)

Storage class
Storage classStorage class
Storage class
Learn By Watch
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma
 
Binary search
Binary search Binary search
Binary search
Raghu nath
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
Nitesh Kumar Pandey
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
Reem Alattas
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
Linear programming ppt
Linear programming pptLinear programming ppt
Linear programming ppt
Meenakshi Tripathi
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma
 
Binary search
Binary search Binary search
Binary search
Raghu nath
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
Reem Alattas
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
Ad

Similar to Introduction to programming language using c (20)

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
Trinity Dwarka
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
Trinity Dwarka
 
Java programming
Java programmingJava programming
Java programming
Trinity Dwarka
 
Namespaces
NamespacesNamespaces
Namespaces
Sangeetha S
 
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
ceng2404314334535365_hgf66353week_07-08_v0.8.pdfceng2404314334535365_hgf66353week_07-08_v0.8.pdf
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
eagac2004
 
Software Engineering- Types of Testing
Software Engineering- Types of TestingSoftware Engineering- Types of Testing
Software Engineering- Types of Testing
Trinity Dwarka
 
Introduction to Information Technology- Operating system
Introduction to Information Technology- Operating systemIntroduction to Information Technology- Operating system
Introduction to Information Technology- Operating system
Trinity Dwarka
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1
Yi-Huan Chan
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
Trinity Dwarka
 
INTRODUCTION TO OPERATING SYSTEM
INTRODUCTION TO OPERATING SYSTEMINTRODUCTION TO OPERATING SYSTEM
INTRODUCTION TO OPERATING SYSTEM
Trinity Dwarka
 
Softwear presentation
Softwear presentationSoftwear presentation
Softwear presentation
Alina Asif
 
Technical interview questions
Technical interview questionsTechnical interview questions
Technical interview questions
Soba Arjun
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.ppt
buvanabala
 
FEDT VB NET- Creating VB .Net Project
FEDT  VB  NET- Creating VB .Net Project FEDT  VB  NET- Creating VB .Net Project
FEDT VB NET- Creating VB .Net Project
Trinity Dwarka
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
 
Computer fundamentals
Computer fundamentalsComputer fundamentals
Computer fundamentals
Trinity Dwarka
 
Design Patterns - GOF
Design Patterns - GOFDesign Patterns - GOF
Design Patterns - GOF
Fanus van Straten
 
JAVA PROGRAMMING- OOP Concept
JAVA PROGRAMMING- OOP ConceptJAVA PROGRAMMING- OOP Concept
JAVA PROGRAMMING- OOP Concept
Trinity Dwarka
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
Kongunadu College of Engineering and Technology
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
Kongunadu College of Engineering and Technology
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
Trinity Dwarka
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
Trinity Dwarka
 
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
ceng2404314334535365_hgf66353week_07-08_v0.8.pdfceng2404314334535365_hgf66353week_07-08_v0.8.pdf
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
eagac2004
 
Software Engineering- Types of Testing
Software Engineering- Types of TestingSoftware Engineering- Types of Testing
Software Engineering- Types of Testing
Trinity Dwarka
 
Introduction to Information Technology- Operating system
Introduction to Information Technology- Operating systemIntroduction to Information Technology- Operating system
Introduction to Information Technology- Operating system
Trinity Dwarka
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1
Yi-Huan Chan
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
Trinity Dwarka
 
INTRODUCTION TO OPERATING SYSTEM
INTRODUCTION TO OPERATING SYSTEMINTRODUCTION TO OPERATING SYSTEM
INTRODUCTION TO OPERATING SYSTEM
Trinity Dwarka
 
Softwear presentation
Softwear presentationSoftwear presentation
Softwear presentation
Alina Asif
 
Technical interview questions
Technical interview questionsTechnical interview questions
Technical interview questions
Soba Arjun
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.ppt
buvanabala
 
FEDT VB NET- Creating VB .Net Project
FEDT  VB  NET- Creating VB .Net Project FEDT  VB  NET- Creating VB .Net Project
FEDT VB NET- Creating VB .Net Project
Trinity Dwarka
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
 
JAVA PROGRAMMING- OOP Concept
JAVA PROGRAMMING- OOP ConceptJAVA PROGRAMMING- OOP Concept
JAVA PROGRAMMING- OOP Concept
Trinity Dwarka
 
Ad

More from Trinity Dwarka (20)

Why BAJMC in Trinity Dwarka
Why BAJMC in Trinity DwarkaWhy BAJMC in Trinity Dwarka
Why BAJMC in Trinity Dwarka
Trinity Dwarka
 
Career Options after BCA
Career Options after BCACareer Options after BCA
Career Options after BCA
Trinity Dwarka
 
Principles of Management-Management-Concept & Meaning
  Principles of Management-Management-Concept & Meaning  Principles of Management-Management-Concept & Meaning
Principles of Management-Management-Concept & Meaning
Trinity Dwarka
 
Principles of Management- Management Process & Functions
Principles of Management- Management Process  &  FunctionsPrinciples of Management- Management Process  &  Functions
Principles of Management- Management Process & Functions
Trinity Dwarka
 
Principles of Management- Managerial Levels & Roles-
Principles of Management- Managerial Levels & Roles-Principles of Management- Managerial Levels & Roles-
Principles of Management- Managerial Levels & Roles-
Trinity Dwarka
 
Management-Concept & Meaning
 Management-Concept & Meaning Management-Concept & Meaning
Management-Concept & Meaning
Trinity Dwarka
 
Principles of Management- Planning
Principles of Management- PlanningPrinciples of Management- Planning
Principles of Management- Planning
Trinity Dwarka
 
Organizing Authority & Responsibility- Principles of Management
Organizing Authority & Responsibility- Principles of ManagementOrganizing Authority & Responsibility- Principles of Management
Organizing Authority & Responsibility- Principles of Management
Trinity Dwarka
 
Staffing- Principles of Management
Staffing- Principles of ManagementStaffing- Principles of Management
Staffing- Principles of Management
Trinity Dwarka
 
Directing-Principles of Management
Directing-Principles of ManagementDirecting-Principles of Management
Directing-Principles of Management
Trinity Dwarka
 
Dimensional Modelling-Data Warehouse & Data Mining
 Dimensional Modelling-Data Warehouse & Data Mining Dimensional Modelling-Data Warehouse & Data Mining
Dimensional Modelling-Data Warehouse & Data Mining
Trinity Dwarka
 
Data Preprocessing- Data Warehouse & Data Mining
Data Preprocessing- Data Warehouse & Data MiningData Preprocessing- Data Warehouse & Data Mining
Data Preprocessing- Data Warehouse & Data Mining
Trinity Dwarka
 
Computer Networks- Network Basics
Computer Networks- Network BasicsComputer Networks- Network Basics
Computer Networks- Network Basics
Trinity Dwarka
 
Java Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet ProgramsJava Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet Programs
Trinity Dwarka
 
Linux Environment- Linux vs Unix
Linux Environment- Linux vs UnixLinux Environment- Linux vs Unix
Linux Environment- Linux vs Unix
Trinity Dwarka
 
Linux Environment- Linux Basics
Linux Environment- Linux BasicsLinux Environment- Linux Basics
Linux Environment- Linux Basics
Trinity Dwarka
 
BCA-Mobile Computing- BASICS OF MOBILE COMPUTING
BCA-Mobile Computing- BASICS OF MOBILE COMPUTINGBCA-Mobile Computing- BASICS OF MOBILE COMPUTING
BCA-Mobile Computing- BASICS OF MOBILE COMPUTING
Trinity Dwarka
 
INTRODUCTION TO INFORMATION TECHNOLOGY- IT Basics
INTRODUCTION TO INFORMATION TECHNOLOGY- IT BasicsINTRODUCTION TO INFORMATION TECHNOLOGY- IT Basics
INTRODUCTION TO INFORMATION TECHNOLOGY- IT Basics
Trinity Dwarka
 
Database Management System
Database Management System Database Management System
Database Management System
Trinity Dwarka
 
E-Commerce- Introduction to E-Commerce
E-Commerce- Introduction to E-CommerceE-Commerce- Introduction to E-Commerce
E-Commerce- Introduction to E-Commerce
Trinity Dwarka
 
Why BAJMC in Trinity Dwarka
Why BAJMC in Trinity DwarkaWhy BAJMC in Trinity Dwarka
Why BAJMC in Trinity Dwarka
Trinity Dwarka
 
Career Options after BCA
Career Options after BCACareer Options after BCA
Career Options after BCA
Trinity Dwarka
 
Principles of Management-Management-Concept & Meaning
  Principles of Management-Management-Concept & Meaning  Principles of Management-Management-Concept & Meaning
Principles of Management-Management-Concept & Meaning
Trinity Dwarka
 
Principles of Management- Management Process & Functions
Principles of Management- Management Process  &  FunctionsPrinciples of Management- Management Process  &  Functions
Principles of Management- Management Process & Functions
Trinity Dwarka
 
Principles of Management- Managerial Levels & Roles-
Principles of Management- Managerial Levels & Roles-Principles of Management- Managerial Levels & Roles-
Principles of Management- Managerial Levels & Roles-
Trinity Dwarka
 
Management-Concept & Meaning
 Management-Concept & Meaning Management-Concept & Meaning
Management-Concept & Meaning
Trinity Dwarka
 
Principles of Management- Planning
Principles of Management- PlanningPrinciples of Management- Planning
Principles of Management- Planning
Trinity Dwarka
 
Organizing Authority & Responsibility- Principles of Management
Organizing Authority & Responsibility- Principles of ManagementOrganizing Authority & Responsibility- Principles of Management
Organizing Authority & Responsibility- Principles of Management
Trinity Dwarka
 
Staffing- Principles of Management
Staffing- Principles of ManagementStaffing- Principles of Management
Staffing- Principles of Management
Trinity Dwarka
 
Directing-Principles of Management
Directing-Principles of ManagementDirecting-Principles of Management
Directing-Principles of Management
Trinity Dwarka
 
Dimensional Modelling-Data Warehouse & Data Mining
 Dimensional Modelling-Data Warehouse & Data Mining Dimensional Modelling-Data Warehouse & Data Mining
Dimensional Modelling-Data Warehouse & Data Mining
Trinity Dwarka
 
Data Preprocessing- Data Warehouse & Data Mining
Data Preprocessing- Data Warehouse & Data MiningData Preprocessing- Data Warehouse & Data Mining
Data Preprocessing- Data Warehouse & Data Mining
Trinity Dwarka
 
Computer Networks- Network Basics
Computer Networks- Network BasicsComputer Networks- Network Basics
Computer Networks- Network Basics
Trinity Dwarka
 
Java Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet ProgramsJava Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet Programs
Trinity Dwarka
 
Linux Environment- Linux vs Unix
Linux Environment- Linux vs UnixLinux Environment- Linux vs Unix
Linux Environment- Linux vs Unix
Trinity Dwarka
 
Linux Environment- Linux Basics
Linux Environment- Linux BasicsLinux Environment- Linux Basics
Linux Environment- Linux Basics
Trinity Dwarka
 
BCA-Mobile Computing- BASICS OF MOBILE COMPUTING
BCA-Mobile Computing- BASICS OF MOBILE COMPUTINGBCA-Mobile Computing- BASICS OF MOBILE COMPUTING
BCA-Mobile Computing- BASICS OF MOBILE COMPUTING
Trinity Dwarka
 
INTRODUCTION TO INFORMATION TECHNOLOGY- IT Basics
INTRODUCTION TO INFORMATION TECHNOLOGY- IT BasicsINTRODUCTION TO INFORMATION TECHNOLOGY- IT Basics
INTRODUCTION TO INFORMATION TECHNOLOGY- IT Basics
Trinity Dwarka
 
Database Management System
Database Management System Database Management System
Database Management System
Trinity Dwarka
 
E-Commerce- Introduction to E-Commerce
E-Commerce- Introduction to E-CommerceE-Commerce- Introduction to E-Commerce
E-Commerce- Introduction to E-Commerce
Trinity Dwarka
 

Recently uploaded (20)

How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 

Introduction to programming language using c

  • 1. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Affiliated Institution of G.G.S.IP.U, Delhi INTRODUCTION TO PROGRAMMING LANGUAGE USING C Topic: Functions Paper Id: 20105 Paper code: BCA 105
  • 2. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Functions Outline 1 Introduction 2 Program Modules in C 3 Math Library Functions 4 Functions 5 Function Definitions 6 Function Prototypes 7 Header Files 8 Calling Functions: Call by Value and Call by Reference 9 Random Number Generation 10 Example: A Game of Chance 11 Storage Classes 12 Scope Rules 13 Recursion 14 Example Using Recursion: The Fibonacci Series 15 Recursion vs. Iteration
  • 3. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 • Divide and conquer – Construct a program from smaller pieces or components • These smaller pieces are called modules – Each piece more manageable than the original program Introduction
  • 4. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Program Modules in C • Functions – Modules in C – Programs combine user-defined functions with library functions • C standard library has a wide variety of functions • Function calls – Invoking functions • Provide function name and arguments (data) • Function performs operations or manipulations • Function returns results – Function call analogy: • Boss asks worker to complete task – Worker gets information, does task, returns result – Information hiding: boss does not know details
  • 5. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Math Library Functions • Math library functions – perform common mathematical calculations – #include <math.h> • Format for calling functions – FunctionName( argument ); • If multiple arguments, use comma-separated list – printf( "%.2f", sqrt( 900.0 ) ); • Calls function sqrt, which returns the square root of its argument • All math functions return data type double – Arguments may be constants, variables, or expressions
  • 6. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Functions • Functions – Modularize a program – All variables declared inside functions are local variables • Known only in function defined – Parameters • Communicate information between functions • Local variables • Benefits of functions – Divide and conquer • Manageable program development – Software reusability • Use existing functions as building blocks for new programs • Abstraction - hide internal details (library functions) – Avoid code repetition
  • 7. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Function Definitions • Function definition format return-value-type function-name( parameter-list ) { declarations and statements } – Function-name: any valid identifier – Return-value-type: data type of the result (default int) • void – indicates that the function returns nothing – Parameter-list: comma separated list, declares parameters • A type must be listed explicitly for each parameter unless, the parameter is of type int
  • 8. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Function Definitions • Function definition format (continued) return-value-type function-name( parameter-list ) { declarations and statements } – Declarations and statements: function body (block) • Variables can be declared inside blocks (can be nested) • Functions can not be defined inside other functions – Returning control • If nothing returned – return; – or, until reaches right brace • If something returned – return expression;
  • 9. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Function Prototypes • Function prototype – Function name – Parameters – what the function takes in – Return type – data type function returns (default int) – Used to validate functions – Prototype only needed if function definition comes after use in program – The function with the prototype int maximum( int, int, int ); • Takes in 3 ints • Returns an int • Promotion rules and conversions – Converting to lower types can lead to errors
  • 10. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Header Files • Header files – Contain function prototypes for library functions – <stdlib.h> , <math.h> , etc – Load with #include <filename> #include <math.h> • Custom header files – Create file with functions – Save as filename.h – Load in other files with #include "filename.h" – Reuse functions
  • 11. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Calling Functions: Call by Value and Call by Reference • Used when invoking functions • Call by value – Copy of argument passed to function – Changes in function do not effect original – Use when function does not need to modify argument • Avoids accidental changes • Call by reference – Passes original argument – Changes in function effect original – Only used with trusted functions • For now, we focus on call by value
  • 12. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Random Number Generation • rand function – Load <stdlib.h> – Returns "random" number between 0 and RAND_MAX (at least 32767) i = rand(); – Pseudorandom • Preset sequence of "random" numbers • Same sequence for every function call • Scaling – To get a random number between 1 and n 1 + ( rand() % n ) • rand() % n returns a number between 0 and n - 1 • Add 1 to make random number between 1 and n 1 + ( rand() % 6) – number between 1 and 6
  • 13. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Random Number Generation • srand function – <stdlib.h> – Takes an integer seed and jumps to that location in its "random" sequence srand( seed ); – srand( time( NULL ) ); //load <time.h> • time( NULL ) – Returns the time at which the program was compiled in seconds – “Randomizes" the seed
  • 14. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Storage Classes • Storage class specifiers – Storage duration – how long an object exists in memory – Scope – where object can be referenced in program – Linkage – specifies the files in which an identifier is known (more in Chapter 14) • Automatic storage – Object created and destroyed within its block – auto: default for local variables auto double x, y; – register: tries to put variable into high-speed registers • Can only be used for automatic variables register int counter = 1;
  • 15. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Storage Classes • Static storage – Variables exist for entire program execution – Default value of zero – static: local variables defined in functions. • Keep value after function ends • Only known in their own function – extern: default for global variables and functions • Known in any function
  • 16. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Scope Rules • File scope – Identifier defined outside function, known in all functions – Used for global variables, function definitions, function prototypes • Function scope – Can only be referenced inside a function body – Used only for labels (start:, case: , etc.)
  • 17. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Scope Rules • Block scope – Identifier declared inside a block • Block scope begins at declaration, ends at right brace – Used for variables, function parameters (local variables of function) – Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block • Function prototype scope – Used for identifiers in parameter list
  • 18. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Recursion • Recursive functions – Functions that call themselves – Can only solve a base case – Divide a problem up into • What it can do • What it cannot do – What it cannot do resembles original problem – The function launches a new copy of itself (recursion step) to solve what it cannot do – Eventually base case gets solved • Gets plugged in, works its way up and solves whole problem
  • 19. TRINITY INSTITUTE OF PROFESSIONAL STUDIES Sector – 9, Dwarka Institutional Area, New Delhi-75 Recursion • Example: factorials – 5! = 5 * 4 * 3 * 2 * 1 – Notice that • 5! = 5 * 4! • 4! = 4 * 3! ... – Can compute factorials recursively – Solve base case (1! = 0! = 1) then plug in • 2! = 2 * 1! = 2 * 1 = 2; • 3! = 3 * 2! = 3 * 2 = 6;