SlideShare a Scribd company logo
Chapter 4:
Functions, Pointers, Errors, Testing
By Mr. Samwel Tarus
Overview
Functions
Definition
Role functions
Types of functions
Communication in functions
Types of variables
Recursion
Program demos
Pointers
Uses of pointers
Declaration of pointers
Errors
Definition
 Types of errors
Testing
Qualities of a good program
Functions
Self contained block of statements which performs a particular task and
returns a value.
The function contains the set of programming statements enclosed by {}.
Collection of functions creates a C program i.e
C = main() + add() + sum() + ….. +last()
Function also called module, block, partition, procedure, subroutine
Every C program must have the main()
Check the role of main() [ 2 main / key roles ]
Importance of functions in a program
Facilitate code Re-use
Facilitate program expansion
Facilitate the location and isolation of faulty functions in a program
Facilitate top down programming
Types of functions
1. Library Functions: Functions which are
declared in the C header files such as
scanf(), printf(), gets(), puts(), ceil(), floor(),
sqrt(), pow() etc.
Functions which have been coded, compiled
and utilized in other programs.
2. User-defined functions: Functions which
are created by the C programmer, so that
he/she can use it many times. It reduces the
complexity of a big program and optimizes
the code.
Communication in functions
Functions communicate by passing messages.
Three concepts in functions that are very important:
Function declaration
Function call
Function definition
User defined functions must be declared before they are used in the
program.
Program demo for functions
/* functions demo program */
#include<stdio.h>
#include<conio.h>
void japan();
void china();
int main()
{
printf(“n initially in main functionn”);
japan();
printf(“nfrom japan n”);
china();
printf(“nfrom china n”);
return 0;
}
void china()
{
printf(“n iam in china n”);
}
void japan()
{
printf(“n iam in japan n”);
}
Concepts of functions
1. Function Declaration:
Done outside all other functions [ between header files and main() ]
Syntax:
return_type function_name(return_type1 arg1, ……);
E.g, int addition(int a, int b, int c);
Declaration informs the compiler about the following:
 Return type of the function
 Function name
 Return types of parameters in the argument list.
 Number of parameters in the argument list
Nb: Declaration creates a template / structure of the function. This structure will
be used to link the function call and function definition.
#include<stdio.h>
#include<conio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("nThe sum is : %d", result);
}
int sum(int a, int b)
{
return a+b;
}
#include<stdio.h>
#include<conio.h>
int sum();
void main()
{
int result;
result = sum();
printf("%d", result);
}
int sum()
{
int a,b;
printf("nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Communication in functions: Cont’d…..
1. Pass by value
2. Pass by reference
Pass by value
Call/Pass by value is the process where the copies of the actual parameters
are passed in one to one correspondence to the formal parameters in the
function definition. Therefore, any changes made in the formal parameters do
not affect the actual parameters.
e.g, int addition(int a, int b, int c);
Copies
int addition(int x, int y, int z)
Nb: Variables a, b, c are called actual parameters
Variables x, y, z are called formal parameters
/* Pass by Value*/
#include<stdio.h>
#include<conio.h>
int badilisha(int, int);
int main()
{
int a=5, b=7;
printf("nOriginal valuesn");
printf("na = %dt", a);
printf("b = %dn",b);
badilisha(a,b);
printf("nValue after interchangen");
printf("na = %dt", a);
printf("b = %dn",b);
return 0;
}
int badilisha(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("nValues in function
definitin");
printf("na = %dt", x);
printf("b = %dn",y);
}
Pass by reference
Call by reference is the process where the original values are passed to the
corresponding function definition. Any changes made in the function definition
affects the actual parameters.
eg int addition(int &a, int &b, int &c);
int addition(int *x, int *y, int *z)
Pass by reference uses the concept of pointers:
5
Operators used in Pointers
& Ampersand
Address of operator
Returns the address where the variable is residing in the memory
* Asterisk
 Value at address operator
 returns the value contained in the address
Let a = 5;
a
5
6500
Pointers
Variable which stores the address of another variable.
Pointer variables can be of type i.e, int, float, char, array, function, etc.
Pointers are user defined datatypes
Syntax for declaration:
return type *variable_name;
int *ptr;
char *c;
/* Pass by Reference*/
#include<stdio.h>
#include<conio.h>
int swap(int *x, int *y);
int main()
{
int a=5, b=7;
printf("nOriginal valuesn");
printf("na = %dt", a);
printf("b = %dn",b);
badilisha(&a,&b);
printf("nValue after interchangen");
printf("na = %dt", a);
printf("b = %dn",b);
return 0;
}
int badilisha(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
printf("nValues in function
definitionn");
printf("na = %dt", *x);
printf("b = %dn", *y);
}
Advantages / uses of pointers
Reduces the code and improves the performance, it is used to retrieving strings,
trees, etc. and used with arrays, structures, and functions.
We can return multiple values from a function using the pointer.
It makes you able to access any memory location in the computer's memory.
Dynamic memory allocation
c language, we can dynamically allocate memory using malloc() and calloc()
functions
Arrays, Functions, and Structures
C language pointers are widely used in arrays, functions, and structures.
Types of variables
Local variables:
Variables declared within a function. Lifetime and the scope is within the
function for which it is declared.
Global variables:
Variables declared outside all other functions in the program. Lifetime and
the scope is the entire program
Program example
/* Types of Variable*/
#include<stdio.h>
#include<conio.h>
int a = 20;
int show();
int main()
{
int a = 10;
printf("n a = %dn",a);
show();
return 0;
}
int show()
{
printf("n a = %dn", a);
return 0;
}
Storage Classes in C
Storage classes in C are used to determine the lifetime, visibility,
memory location, and initial value of a variable.
There are four types of storage classes in C:
Automatic
External
Static
Register
Storage
Classes
Storage
Place
Default
Value
Scope Lifetime
auto RAM Garbage Value Local Within function
extern RAM Zero Global Whole program
static RAM Zero Local Till the end of the main program,
Retains value between multiple
functions call
register Register Garbage Value Local Within the function
Storage Classes in C
Recursion in C
A process where a function calls itself several times; e.g,
/*Recursion */
#include<stdio.h>
#include<conio.h>
int main()
{
printf("nExample of recursionn");
main();
return 0;
}
/*factorial of any value */
#include <stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,f;
printf("Enter valuen");
scanf("%d",&n);
f = fact(n);
printf("factorial = %dn", f);
}
int fact(int n)
{
int res = 1;
if (n==0)
{
return res;
}
else if ( n == 1)
{
return res;
}
else
{
res = res * n*fact(n-1);
return res;
}
Errors
Problems or faults that occur in the program, which makes the behavior of the
program abnormal.
Also known as the bugs or faults
Detected either during the time of compilation or execution.
The process of removing these bugs is known as debugging.
Types of errors
1. Syntax error
2. Run-time error
3. Logical error
4. Latent / Hidden errors
5. Semantic error
Syntax error
Errors that occur as a result of the violation of the rules of the language;
Detected by the compiler at compilation time.
Commonly occurred syntax errors are:
If we miss the parenthesis (}) while writing the code.
Displaying the value of a variable without its declaration.
If we miss the semicolon (;) at the end of the statement.
#include <stdio.h>
int main()
{
a = 10;
printf("The value of a is : %d", a);
return 0;
}
Runtime error
Errors that occur at runtime.
Detected by the compiler when running the program.
Examples
Mismatch of data types
Trying to open a file which is not created
Lack of free memory space.
Logical error
Errors that occur as a result of poor understanding of the logic.
Compiler cannot detect such errors.
Program runs and outputs results but results are wrong.
Latent error
Errors that are only visible when some set of values are used in the program.
Example:
result = (a + b) /(a – b);
Let1 a = 10, b = 5;
Output: result = 3
Let2 a = 5, b = 5;
Output: division by zero
Semantic error
Errors that occurred when the statements are not understandable by the compiler. E.g,
Use of a un-initialized variable.
int i;
i=i+2;
Type compatibility
int b = "javatpoint";
Errors in expressions
int a, b, c;
a+b = c;
Testing
The process of evaluating a system or its component(s) with the intent to find
whether it satisfies the specified requirements or not.
Executing a system in order to identify any gaps, errors, or missing
requirements in contrary to the actual requirements.
The process of analyzing a software item to detect the differences between
existing and required conditions (that is defects/errors/bugs) and to evaluate the
features of the software item.
Criteria for testing a program
1. Accuracy
2. Functionality
3. Reliability
4. Usability
5. Efficiency
6. Maintainability
7. Portability
8. Robustness
9. User friendliness
10. Completeness
11. Consistency
Functions.pptx, programming language in c

More Related Content

Similar to Functions.pptx, programming language in c (20)

function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Programming in C FUNCTION Basic concepts.pptx
Programming in C FUNCTION Basic concepts.pptxProgramming in C FUNCTION Basic concepts.pptx
Programming in C FUNCTION Basic concepts.pptx
vengaimarbhan1
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptxUnit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptx
PragatheshP
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
C function
C functionC function
C function
thirumalaikumar3
 
Functions and Header files ver very useful
Functions and Header files ver very usefulFunctions and Header files ver very useful
Functions and Header files ver very useful
RamSiddesh1
 
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. AnsariBasic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
G. H. Raisoni Academy of Engineering & Technology, Nagpur
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
ziyadaslanbey
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
KarthikSivagnanam2
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdfModule 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
TeenaGeorge15
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
C Programming - Basics of c -history of c
C Programming - Basics of c -history of cC Programming - Basics of c -history of c
C Programming - Basics of c -history of c
DHIVYAB17
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Chapter3
Chapter3Chapter3
Chapter3
Kamran
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Programming in C FUNCTION Basic concepts.pptx
Programming in C FUNCTION Basic concepts.pptxProgramming in C FUNCTION Basic concepts.pptx
Programming in C FUNCTION Basic concepts.pptx
vengaimarbhan1
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptxUnit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptx
PragatheshP
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Functions and Header files ver very useful
Functions and Header files ver very usefulFunctions and Header files ver very useful
Functions and Header files ver very useful
RamSiddesh1
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
ziyadaslanbey
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdfModule 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
C Programming - Basics of c -history of c
C Programming - Basics of c -history of cC Programming - Basics of c -history of c
C Programming - Basics of c -history of c
DHIVYAB17
 
Chapter3
Chapter3Chapter3
Chapter3
Kamran
 

More from floraaluoch3 (7)

Clustering Illustrations Publishing 1.pptx
Clustering Illustrations Publishing 1.pptxClustering Illustrations Publishing 1.pptx
Clustering Illustrations Publishing 1.pptx
floraaluoch3
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
DOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cDOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in c
floraaluoch3
 
901470_Chap1.ppt.artificial intelligence
901470_Chap1.ppt.artificial intelligence901470_Chap1.ppt.artificial intelligence
901470_Chap1.ppt.artificial intelligence
floraaluoch3
 
lecture-2-3_Memory.pdf,describing memory
lecture-2-3_Memory.pdf,describing memorylecture-2-3_Memory.pdf,describing memory
lecture-2-3_Memory.pdf,describing memory
floraaluoch3
 
lect11-12_parallel.pdf,describing parallelism
lect11-12_parallel.pdf,describing parallelismlect11-12_parallel.pdf,describing parallelism
lect11-12_parallel.pdf,describing parallelism
floraaluoch3
 
Computational models,vonneuman model,turing model
Computational models,vonneuman model,turing modelComputational models,vonneuman model,turing model
Computational models,vonneuman model,turing model
floraaluoch3
 
Clustering Illustrations Publishing 1.pptx
Clustering Illustrations Publishing 1.pptxClustering Illustrations Publishing 1.pptx
Clustering Illustrations Publishing 1.pptx
floraaluoch3
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
DOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cDOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in c
floraaluoch3
 
901470_Chap1.ppt.artificial intelligence
901470_Chap1.ppt.artificial intelligence901470_Chap1.ppt.artificial intelligence
901470_Chap1.ppt.artificial intelligence
floraaluoch3
 
lecture-2-3_Memory.pdf,describing memory
lecture-2-3_Memory.pdf,describing memorylecture-2-3_Memory.pdf,describing memory
lecture-2-3_Memory.pdf,describing memory
floraaluoch3
 
lect11-12_parallel.pdf,describing parallelism
lect11-12_parallel.pdf,describing parallelismlect11-12_parallel.pdf,describing parallelism
lect11-12_parallel.pdf,describing parallelism
floraaluoch3
 
Computational models,vonneuman model,turing model
Computational models,vonneuman model,turing modelComputational models,vonneuman model,turing model
Computational models,vonneuman model,turing model
floraaluoch3
 
Ad

Recently uploaded (20)

Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
Ad

Functions.pptx, programming language in c

  • 1. Chapter 4: Functions, Pointers, Errors, Testing By Mr. Samwel Tarus
  • 2. Overview Functions Definition Role functions Types of functions Communication in functions Types of variables Recursion Program demos Pointers Uses of pointers Declaration of pointers Errors Definition  Types of errors Testing Qualities of a good program
  • 3. Functions Self contained block of statements which performs a particular task and returns a value. The function contains the set of programming statements enclosed by {}. Collection of functions creates a C program i.e C = main() + add() + sum() + ….. +last() Function also called module, block, partition, procedure, subroutine Every C program must have the main() Check the role of main() [ 2 main / key roles ]
  • 4. Importance of functions in a program Facilitate code Re-use Facilitate program expansion Facilitate the location and isolation of faulty functions in a program Facilitate top down programming
  • 5. Types of functions 1. Library Functions: Functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor(), sqrt(), pow() etc. Functions which have been coded, compiled and utilized in other programs. 2. User-defined functions: Functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.
  • 6. Communication in functions Functions communicate by passing messages. Three concepts in functions that are very important: Function declaration Function call Function definition User defined functions must be declared before they are used in the program.
  • 7. Program demo for functions /* functions demo program */ #include<stdio.h> #include<conio.h> void japan(); void china(); int main() { printf(“n initially in main functionn”); japan(); printf(“nfrom japan n”); china(); printf(“nfrom china n”); return 0; } void china() { printf(“n iam in china n”); } void japan() { printf(“n iam in japan n”); }
  • 8. Concepts of functions 1. Function Declaration: Done outside all other functions [ between header files and main() ] Syntax: return_type function_name(return_type1 arg1, ……); E.g, int addition(int a, int b, int c); Declaration informs the compiler about the following:  Return type of the function  Function name  Return types of parameters in the argument list.  Number of parameters in the argument list Nb: Declaration creates a template / structure of the function. This structure will be used to link the function call and function definition.
  • 9. #include<stdio.h> #include<conio.h> int sum(int, int); void main() { int a,b,result; printf("nEnter two numbers:"); scanf("%d %d",&a,&b); result = sum(a,b); printf("nThe sum is : %d", result); } int sum(int a, int b) { return a+b; }
  • 10. #include<stdio.h> #include<conio.h> int sum(); void main() { int result; result = sum(); printf("%d", result); } int sum() { int a,b; printf("nEnter two numbers"); scanf("%d %d",&a,&b); return a+b; }
  • 11. Communication in functions: Cont’d….. 1. Pass by value 2. Pass by reference
  • 12. Pass by value Call/Pass by value is the process where the copies of the actual parameters are passed in one to one correspondence to the formal parameters in the function definition. Therefore, any changes made in the formal parameters do not affect the actual parameters. e.g, int addition(int a, int b, int c); Copies int addition(int x, int y, int z) Nb: Variables a, b, c are called actual parameters Variables x, y, z are called formal parameters
  • 13. /* Pass by Value*/ #include<stdio.h> #include<conio.h> int badilisha(int, int); int main() { int a=5, b=7; printf("nOriginal valuesn"); printf("na = %dt", a); printf("b = %dn",b); badilisha(a,b); printf("nValue after interchangen"); printf("na = %dt", a); printf("b = %dn",b); return 0; } int badilisha(int x, int y) { int z; z = x; x = y; y = z; printf("nValues in function definitin"); printf("na = %dt", x); printf("b = %dn",y); }
  • 14. Pass by reference Call by reference is the process where the original values are passed to the corresponding function definition. Any changes made in the function definition affects the actual parameters. eg int addition(int &a, int &b, int &c); int addition(int *x, int *y, int *z) Pass by reference uses the concept of pointers:
  • 15. 5 Operators used in Pointers & Ampersand Address of operator Returns the address where the variable is residing in the memory * Asterisk  Value at address operator  returns the value contained in the address Let a = 5; a 5 6500
  • 16. Pointers Variable which stores the address of another variable. Pointer variables can be of type i.e, int, float, char, array, function, etc. Pointers are user defined datatypes Syntax for declaration: return type *variable_name; int *ptr; char *c;
  • 17. /* Pass by Reference*/ #include<stdio.h> #include<conio.h> int swap(int *x, int *y); int main() { int a=5, b=7; printf("nOriginal valuesn"); printf("na = %dt", a); printf("b = %dn",b); badilisha(&a,&b); printf("nValue after interchangen"); printf("na = %dt", a); printf("b = %dn",b); return 0; } int badilisha(int *x, int *y) { int z; z = *x; *x = *y; *y = z; printf("nValues in function definitionn"); printf("na = %dt", *x); printf("b = %dn", *y); }
  • 18. Advantages / uses of pointers Reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions. We can return multiple values from a function using the pointer. It makes you able to access any memory location in the computer's memory. Dynamic memory allocation c language, we can dynamically allocate memory using malloc() and calloc() functions Arrays, Functions, and Structures C language pointers are widely used in arrays, functions, and structures.
  • 19. Types of variables Local variables: Variables declared within a function. Lifetime and the scope is within the function for which it is declared. Global variables: Variables declared outside all other functions in the program. Lifetime and the scope is the entire program
  • 20. Program example /* Types of Variable*/ #include<stdio.h> #include<conio.h> int a = 20; int show(); int main() { int a = 10; printf("n a = %dn",a); show(); return 0; } int show() { printf("n a = %dn", a); return 0; }
  • 21. Storage Classes in C Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C: Automatic External Static Register
  • 22. Storage Classes Storage Place Default Value Scope Lifetime auto RAM Garbage Value Local Within function extern RAM Zero Global Whole program static RAM Zero Local Till the end of the main program, Retains value between multiple functions call register Register Garbage Value Local Within the function Storage Classes in C
  • 23. Recursion in C A process where a function calls itself several times; e.g, /*Recursion */ #include<stdio.h> #include<conio.h> int main() { printf("nExample of recursionn"); main(); return 0; }
  • 24. /*factorial of any value */ #include <stdio.h> #include<conio.h> int fact(int); int main() { int n,f; printf("Enter valuen"); scanf("%d",&n); f = fact(n); printf("factorial = %dn", f); } int fact(int n) { int res = 1; if (n==0) { return res; } else if ( n == 1) { return res; } else { res = res * n*fact(n-1); return res; }
  • 25. Errors Problems or faults that occur in the program, which makes the behavior of the program abnormal. Also known as the bugs or faults Detected either during the time of compilation or execution. The process of removing these bugs is known as debugging. Types of errors 1. Syntax error 2. Run-time error 3. Logical error 4. Latent / Hidden errors 5. Semantic error
  • 26. Syntax error Errors that occur as a result of the violation of the rules of the language; Detected by the compiler at compilation time. Commonly occurred syntax errors are: If we miss the parenthesis (}) while writing the code. Displaying the value of a variable without its declaration. If we miss the semicolon (;) at the end of the statement. #include <stdio.h> int main() { a = 10; printf("The value of a is : %d", a); return 0; }
  • 27. Runtime error Errors that occur at runtime. Detected by the compiler when running the program. Examples Mismatch of data types Trying to open a file which is not created Lack of free memory space.
  • 28. Logical error Errors that occur as a result of poor understanding of the logic. Compiler cannot detect such errors. Program runs and outputs results but results are wrong.
  • 29. Latent error Errors that are only visible when some set of values are used in the program. Example: result = (a + b) /(a – b); Let1 a = 10, b = 5; Output: result = 3 Let2 a = 5, b = 5; Output: division by zero
  • 30. Semantic error Errors that occurred when the statements are not understandable by the compiler. E.g, Use of a un-initialized variable. int i; i=i+2; Type compatibility int b = "javatpoint"; Errors in expressions int a, b, c; a+b = c;
  • 31. Testing The process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not. Executing a system in order to identify any gaps, errors, or missing requirements in contrary to the actual requirements. The process of analyzing a software item to detect the differences between existing and required conditions (that is defects/errors/bugs) and to evaluate the features of the software item.
  • 32. Criteria for testing a program 1. Accuracy 2. Functionality 3. Reliability 4. Usability 5. Efficiency 6. Maintainability 7. Portability 8. Robustness 9. User friendliness 10. Completeness 11. Consistency