What
is Recursion?
The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function.
Using
recursive algorithm, certain problems can be solved quite easily. Examples of
such problems are Towers of Hanoi
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
C function argument and
return values
A
function in C can be called either with arguments or without arguments. These
function may or may not return values to the calling functions. All C functions
can be called either with arguments or without arguments in a C program. Also,
they may or may not return any values. Hence the function prototype of a
function in C is as below:
There are
following categories:
Function with no argument and no return value : When a function has no arguments,
it does not receive any data from the calling function. Similarly when it does
not return a value, the calling function does not receive any data from the
called function.
Syntax
:
Function
declaration : void function();
Function
call : function();
Function
definition :
void function()
{
statements;
}
// C
code for function with no
// arguments and no return value
#include
<stdio.h>
void
value(void);
void
main()
{
value();
}
void
value(void)
{
int year = 1, period = 5, amount = 5000,
inrate = 0.12;
float sum;
sum = amount;
while (year <= period) {
sum = sum * (1 + inrate);
year = year + 1;
}
printf(" The total amount is
%f:", sum);
}
Output:
The
total amount is 5000.000000
Function with arguments but no return value : When a function has arguments, it
receive any data from the calling function but it returns no values.
Syntax
:
Function
declaration : void function ( int );
Function
call : function( x );
Function
definition:
void function( int x )
{
statements;
}
// C
code for function
// with
argument but no return value
#include
<stdio.h>
void
function(int, int[], char[]);
int
main()
{
int a = 20;
int ar[5] = { 10, 20, 30, 40, 50 };
char str[30] = "geeksforgeeks";
function(a, &ar[0], &str[0]);
return 0;
}
void
function(int a, int* ar, char* str)
{
int i;
printf("value of a is %d\n\n",
a);
for (i = 0; i < 5; i++) {
printf("value of ar[%d] is
%d\n", i, ar[i]);
}
printf("\nvalue of str is %s\n",
str);
}
Output:
value
of a is 20
value
of ar[0] is 10
value
of ar[1] is 20
value
of ar[2] is 30
value
of ar[3] is 40
value
of ar[4] is 50
The
given string is : geeksforgeeks
Function with no arguments but returns a value : There could be occasions where we
may need to design functions that may not take any arguments but returns a
value to the calling function. A example for this is getchar function it has no
parameters but it returns an integer an integer type data that represents a
character.
Syntax
:
Function
declaration : int function();
Function
call : function();
Function
definition :
int function()
{
statements;
return x;
}
// C
code for function with no arguments
// but
have return value
#include
<math.h>
#include
<stdio.h>
int
sum();
int
main()
{
int num;
num = sum();
printf("\nSum of two given values =
%d", num);
return 0;
}
int
sum()
{
int a = 50, b = 80, sum;
sum = sqrt(a) + sqrt(b);
return sum;
}
Output:
Sum of
two given values = 16
Function with arguments and return value
Syntax
:
Function
declaration : int function ( int );
Function
call : function( x );
Function
definition:
int function( int x )
{
statements;
return x;
}
// C
code for function with arguments
// and
with return value
#include
<stdio.h>
#include
<string.h>
int
function(int, int[]);
int
main()
{
int i, a = 20;
int arr[5] = { 10, 20, 30, 40, 50 };
a = function(a, &arr[0]);
printf("value of a is %d\n", a);
for (i = 0; i < 5; i++) {
printf("value of arr[%d] is
%d\n", i, arr[i]);
}
return 0;
}
int
function(int a, int* arr)
{
int i;
a = a + 20;
arr[0] = arr[0] + 50;
arr[1] = arr[1] + 50;
arr[2] = arr[2] + 50;
arr[3] = arr[3] + 50;
arr[4] = arr[4] + 50;
return a;
}
Output:
value
of a is 40
value
of arr[0] is 60
value
of arr[1] is 70
value
of arr[2] is 80
value
of arr[3] is 90
value
of arr[4] is 100
Call by value and Call by
reference in C
There are two methods to pass the data into the function in C
language, i.e., call by value and call by reference.
Let's understand call by value and call by reference in c
language one by one.
Call
by value in C
· In
call by value method, the value of the actual parameters is copied into the
formal parameters. In other words, we can say that the value of the variable is
used in the function call in the call by value method.
·
In call by value method, we can not modify the
value of the actual parameter by the formal parameter.
·
In call by value, different memory is allocated
for actual and formal parameters since the value of the actual parameter is
copied into the formal parameter.
·
The actual parameter is the argument which is
used in the function call whereas formal parameter is the argument which is used
in the function definition.
Let's try to understand the concept of call by value in c
language by the example given below:
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);
printf("After function call x=%d \n", x);
return 0;
}
Output
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100
Call by Value Example: Swapping the values of the two
variables
#include <stdio.h>
void swap(int , int);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Call by reference in C
· In
call by reference, the address of the variable is passed into the function call
as the actual parameter.
·
The value of the actual parameters can be
modified by changing the formal parameters since the address of the actual
parameters is passed.
·
In call by reference, the memory allocation is
similar for both formal parameters and actual parameters. All the operations in
the function are performed on the value stored at the address of the actual
parameters, and the modified value gets stored at the same address.
Difference
between call by value and call by reference in c
No.
|
Call by value
|
Call by reference
|
1
|
A copy of the value is
passed into the function
|
An address of value is passed
into the function
|
2
|
Changes made inside the function is limited to the function
only. The values of the actual parameters do not change by changing the
formal parameters.
|
Changes made inside the function validate outside of the
function also. The values of the actual parameters do change by changing the
formal parameters.
|
3
|
Actual and formal
arguments are created at the different memory location
|
Actual and formal
arguments are created at the same memory location
|
Features of C Language
C is
the widely used language. It provides many features that
are given below.
Simple
Machine Independent or Portable
Mid-level programming language
structured programming language
Rich Library
Memory Management
Fast Speed
Pointers
Recursion
Extensible
1) Simple
C is a simple language in the sense that it provides a structured approach (to
break the problem into parts), the rich set of library functions, data types,
etc.
2) Machine Independent or Portable
Unlike assembly language, c programs can be executed on different
machines with some machine specific changes. Therefore, C is a
machine independent language.
3) Mid-level programming language
Although, C is intended to do low-level programming. It
is used to develop system applications such as kernel, driver, etc. It also supports the features of a
high-level language. That is why it is known as mid-level
language.
4) Structured programming language
C is a structured programming language in the sense that we can break the program into
parts using functions. So, it is easy to understand and
modify. Functions also provide code reusability.
5) Rich Library
C provides a lot of inbuilt functions that
make the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In
C language, we can free the allocated memory at any time by calling the free() function.
7) Speed
The compilation and execution time of C language is fast since
there are lesser inbuilt functions and hence the lesser overhead.
8) Pointer
C provides the feature of pointers. We can directly interact
with the memory by using the pointers. We can use pointers for memory,
structures, functions, array, etc.
9) Recursion
In C, we can call the function within the function. It
provides code reusability for every function. Recursion enables us to use the
approach of backtracking.
10) Extensible
C language is extensible because it can easily adopt new features.
printf() and scanf() in C
The printf() and scanf() functions are used for input and output
in C language. Both functions are inbuilt library functions, defined in stdio.h
(header file).
printf() function
The printf()
function is used for output. It prints the given statement
to the console.
The syntax of printf() function is given below:
printf("format string",argument_list);
The format
string can be %d (integer), %c (character), %s (string),
%f (float) etc.
scanf() function
The scanf() function is used for
input. It reads the input data from the console.
scanf("format string",argument_list);
Program
to print cube of given number
Let's see a simple example of c language that gets input from
the user and prints the cube of the given number.
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
Output
enter a number:5
cube of number is:125
The scanf("%d",&number) statement
reads integer number from the console and stores the given value in number
variable.
The printf("cube of number is:%d
",number*number*number) statement prints the cube of number
on the console.
5 Write a program to calculate the sum of
series. ( odd number and even number)
#include<stdio.h>
int
main ()
{
int n,i,odd=0, even=0;
printf("Enter the number : ");
scanf("%d",&n);
printf("1+2+3+...........+%d",n);
for (i=1;i<=n; i++)
{
if (i%2==0)
even = even+i;
else
odd = odd+i;
}
printf(" = sum of even = %d, sum of
odd = %d, Sum %d",even,odd, even-odd);
return 0;
}
#include<stdio.h>
C program to check whether a
number is prime or not.
int main()
{
int n;
printf("Enter the number :
");
scanf("%d",&n);
if (n%2==0)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
return 0;
}
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;
i<=rows; i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
}