Untitled
Untitled
Untitled
Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
types of function
There are two types of function in C programming:
Standard library functions
User-defined functions
Standard library functions
The standard library functions are built-in functions in C programming.
These functions are defined in header files. For example,
The printf(),scanf(),main() are standard library function. This function is
defined in the stdio.h header file.
Hence, to use the printf(),scanf()functions, we need to include
the stdio.h header file using #include <stdio.h>.
EXAMPLE:
#include <stdio.h>
void main()
{
printf("Hello World!");
User-defined function
You can also create functions as per your need. Such functions created by
the user are known as user-defined functions.
How user-defined function works( structure of function):
#include <stdio.h>
void myfun()
{
... .. ...
... .. ...
}
int main()
{
myfun();
The following syntax describes how to write a function which neither takes
any arguments nor returns any value.
Example program :
#include<stdio.h>
void myFunction()
printf("I AM NAGENDRA");
int main()
myFunction();
myFunction();
myFunction();
return 0;
Output:
I AM NAGENDRA
I AM NAGENDRA
I AM NAGENDRA
The following syntax shows how to write a function which does not take any
arguments but returns some value back to the calling function.
{
parenthesis since there is no argument
....
....
return value;
}
Example program :
#include<stdio.h>
int myFunction()
printf("I AM NAGENDRA");
return 0;
}
int main()
myFunction();
myFunction();
myFunction();
return 0;
Output:
I AM NAGENDRA
I AM NAGENDRA
I AM NAGENDRA
Example program:
#include<stdio.h>
printf("%s,%d\n”,a,b);
}
int main()
return 0;
Output:
madhu,3
Naveen,14
Yashwant,30
Example program:
#include<stdio.h>
printf("%s,%d\n”,a,b);
return 0;
int main()
return 0;
Output:
madhu,3
Naveen,14
Yashwant,30
Program:
#include <stdio.h>
void india()
{
printf("i am in india\n");
void hyderabad()
printf("i am in hyderabad\n");
void main()
india();
hyderabad();
Output:
i am in main function
i am in india
i am in hyderabad
C Function Declaration and Definition
A function consist of two parts:
Example program:
#include <stdio.h>
return 0;
printf("hello!");
Output:
hello!
Parameters are specified after the function name, inside the parentheses.
You can add as many parameters as you want, just separate them with a
comma:
Syntax:
returnType functionName(parameter1, parameter2, parameter3)
{
//code_to_be_executed
}
Example:
#include <stdio.h>
printf("%s,%d\n”,a,b);
int main()
return 0;
Output:
madhu,3
Naveen,14
Yashwant,30
The void keyword, used in the previous examples, indicates that the
function should not return a value. If you want the function to return a value,
you can use a data type (such as int or float, etc.) instead of void, and use
the return keyword inside the function:
Example:
#include <stdio.h>
return x + y;
int main()
return 0;
Output:
Result is:8
STANDARD LIBRARY FUNCTIONS IN C
Library functions are built-in functions that are grouped
together and placed in a common location called library.
Each function here performs a specific operation. We can use
this library functions to get the pre-defined output.
All C standard library functions are declared by using many
header files. These library functions are created at the time of
designing the compilers.
We include the header files in our C program
by using #include<filename.h>. Whenever the program is
run and executed, the related files are included in the C
program.
Header File Functions
Some of the header file functions are as follows −
stdio.h − It is a standard i/o header file in which Input/output functions
are declared
conio.h − This is a console input/output header file.
string.h − All string related functions are in this header file.
stdlib.h − This file contains common functions which are used in the
C programs.
math.h − All functions related to mathematics are in this header file.
time.h − This file contains time and clock related functions.Built
functions in stdio.h
Built functions in stdio.h
Let’s see what are the built functions present in stdio.h library function.
1 printf()This function is used to print the all char, int, float, string etc.,
values onto the output screen.
32 remove()Deletes a file.
33 flush()Flushes a file.
Parameter Passing Techniques in C
Or
Inter function communication
There are different ways in which parameter data can be passed into and
out of methods and functions. Let us assume that a function B() is called
from another function A(). In this case A is called the “caller
function” and B is called the “called function or callee function”. Also,
the arguments which A sends to B are called actual arguments and the
parameters of B are called formal arguments.
Terminology
Formal Parameter : A variable and its type as they appear in the
prototype of the function or method.
Actual Parameter : The variable or expression corresponding to a
formal parameter that appears in the function or method call in the
calling environment.
Two methods of Parameter Passing:
1.Pass By Value
Pass by reference
Program:
Output:
Output:
Printf(“%d %d %d %d”,a[0],a[1],a[2],a[3]);
}
Void main
Int a[]={10,20,30,40};
fun(a);
Output:
10 20 30 40
Signature of function
Program:
void main()
{
recursion();
}
The C programming language supports recursion, i.e., a function to call
itself. But while using recursion, programmers need to be careful to define
an exit condition from the function, otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems,
such as calculating the factorial of a number, generating Fibonacci series,
etc.
Number Factorial:
int factorial(int n)
{
if(n <= 1)
{
return 1;
}
else return n * factorial(n - 1);
}
int main()
{
int n = 12;
printf("Factorial of %d is %d\n",ni, factorial(n));
return 0;
}
Output:
Factorial of 12 is 479001600
Fibonacci Series:
The following example generates the Fibonacci series for a given number
using a recursive function –
Program:
#include <stdio.h>
int fibonacci(int i)
{
if(i == 0)
{
return 0;
}
else if(i == 1)
{
return 1;
}
else
return fibonacci(i-1) + fibonacci(i-2);
}
int main()
{
int i;
return 0;
}
Output:
0
1
1
2
3
5
8
13
21
34
Advantages Disadvantages
As it can be seen that the length (size) of the array above made is 9. But
what if there is a requirement to change this length (size). For Example,
If there is a situation where only 5 elements are needed to be entered
in this array. In this case, the remaining 4 indices are just wasting
memory in this array. So there is a requirement to lessen the length
(size) of the array from 9 to 5.
Take another situation. In this, there is an array of 9 elements with all 9
indices filled. But there is a need to enter 3 more elements in this array.
In this case, 3 indices more are required. So the length (size) of the
array needs to be changed from 9 to 12.
This procedure is referred to as Dynamic Memory Allocation in C.
Therefore, C Dynamic Memory Allocation can be defined as a
procedure in which the size of a data structure (like Array) is changed
during the runtime.
1. malloc()
2. calloc()
3. realloc()
4. free()
Now let's have a quick look at the methods used for dynamic memory allocation.
Function Syntax
Malloc:
The malloc() function allocates single block of requested memory.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
char *p;
p = malloc( 15 * sizeof(char) );
if(p== NULL )
else
{
strcpy( p,"nagendra");
free(p);
Output:
Calloc:
The calloc() function allocates multiple block of requested memory.
void main()
{
char *p;
//memory allocated dynamically
p = calloc( 15 , sizeof(char) );
if(p== NULL )
{
printf("Couldn't able to allocate memory\n");
}
else
{
strcpy( p,"nagendra");
}
Output:
Realloc:
If memory is not sufficient for malloc() or calloc(), you can reallocate the
memory by realloc() function. In short, it changes the memory size.
Example program for realloc:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char *p;
//memory allocated dynamically
p = calloc( 15 , sizeof(char) );
p=realloc(p,100*sizeof(char));
if(p== NULL )
{
printf("Couldn't able to allocate memory\n");
}
else
{
strcpy( p,"nagendra");
}
printf("Dynamically allocated memory content : %s\n", p );
free(p);
}
Output:
Algorithm
Analysis
Algorithm:
Start
Read a, b, c values
Compute d = b*b-4ac
if d > 0 then
o r1 = -b+ sqrt(d)/(2*a)
o r2 = -b-sqrt(d)/(2*a)
Otherwise if d = 0 then
o compute r1 = -b/2a, r2=-b/2a
o print r1,r2 values
Otherwise if d < 0 then print roots are imaginary
Stop
Program:
# include<stdio.h>
# include<conio.h>
# include<math.h>
main (){
float a,b,c,r1,r2,d;
printf (“enter the values of a b c”);
scanf (“ %f %f %f”, &a, &b, &c);
d= b*b – 4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf (“The real roots = %f %f”, r1, r2);
}
else if (d= =0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf (“roots are equal =%f %f”, r1, r2);
}
else
printf(“Roots are imaginary”);
getch ();
}
Testing:
Asymptotic notations
DIVIDE AND CONQUER STRATEGY
Searching techniques
Searching refers to the process of finding a desired element in set of
items. The desired element is called "target".
Searching Algorithms :
1. Linear Search
2. Binary Search
Linear search
Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is
checked and if a match is found then that particular item is returned,
otherwise the search continues till the end of the data collection.
Example:
Algorithm:
Linear Search ( Array Arr, Value a ) // Arr is the name of the array, and a
is the searched element.
Step 1: Set i to 0 // i is the index of an array which starts from 0
Step 2: if i > n then go to step 7 // n is the number of elements in array
Step 3: if Arr[i] = a then go to step 6
Step 4: Set i to i + 1
Step 5: Goto step 2
Step 6: Print element a found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Binary search
Difference between linear and binary search
Sorting
A sorting algorithm is used to arrange elements of an array/list in a
specific order. For example,
Sorting an
array
Here, we are sorting the array in ascending order.
There are various sorting algorithms that can be used to complete this
operation. And, we can use any algorithm based on the requirement.
Algorithm:
1. begin BubbleSort(arr)
2. for all array elements
3. if arr[i] > arr[i+1]
4. swap(arr[i], arr[i+1])
5. end if
6. end for
7. return arr
8. end BubbleSort
Insertion sort :
Insertion sort is a simple sorting algorithm that works similar to the way
you sort playing cards in your hands. The array is virtually split into a
sorted and an unsorted part. Values from the unsorted part are picked
and placed at the correct position in the sorted part.
SELECTION SORT:
EXAMPLE :
It is an unstable sorting
2. It is a stable sorting algorithm. algorithm.
The best-case time complexity is Ω(N) when the For best case, worst case and
array is already in ascending order. It have Θ(N2) in average selection sort have
3. worst case and average case. complexity Θ(N2).