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.
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