Function
Function
What is a function?
Definition 1: A function in C is a set of statements that when called perform some specific task. It is the basic
building block of a C program that provides modularity and code reusability. The programming statements of a
function are enclosed within { } braces, having certain meanings and performing certain operations.
Definition2: A function is a block of code which only runs when it is called. you can pass data, known as
parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code:
Define the code once, and use it many times.
Definition3: A function is a group of statements that together perform a task. Every C program has at least one
function, which is main(), and all the most trivial programs can define additional functions.You can divide up your
code into separate functions.
C enables users to break up a program into segments commonly known as functions,each of which can be
written more or less independently of the others.
Every function in the program should perform a well defined task.
Program code of one function is insulated from other functions.
Using Functions
.A function must be declared globally in a c program to tell the compiler about the function name, function
parameters, and return type.
Function header
Examples: int sum(int a, int b);-----this can also be written as int sum(int , int );
**You don’t have to specify the name of the parameter at the time of function declaration.(optional)
** in function declaration you should put a semicolon at the end.
** A function in C must always be declared globally before calling it.
** A function can not be declared inside another function.
** A function having void as its return type can not return any value.
** if a function declared without a return type, by default it will return integer value.
More Examples:
2.Function Definitions
The function definition consists of actual statements which are executed when the function is called (i.e.
when the program control
A C function is generally defined and declared in a single step because the function definition always
starts with the function declaration so we do not need to declare it explicitly.
3.Function Call
A function call is a statement that instructs the compiler to execute the function. We use the function
name and parameters in the function call.
When a function is called, compiler jumps to the called function to execute the statements that are part of
that function.
Once called function is executed, the program control passes back to the calling function.
Examples: sum(a,b);//sum is the name of function. And a,b are arguments or actual
parameters of the function
Display();
Function name and number and type of the arguments in the function call must be same as that given in
the function declaration and the header in the function definition.
If by mistake the parameters passed to a function are more than what it is specified to accept,then extra
arguments will be discarded.
If the parameters passed to a function are less than what it is specified to accept, then the unmatched
argument will be assigned with some garbage values.
Name of variables inside the function declaration, header of function and function call may vary.
If the return type of the function is not void, then the value returned by the called function may be
assigned to some variable,,
o Variable_name=function_name(varaiable1,variable2,….);
4.Return Statement
The return statement is used to terminate the execution of a function and returns control to the
calling function.
When this statement is encountered, the program execution resumes in the calling function at the point
immediately following the function call.
A return statement may or may not return a value to the calling function.
o Syntax: return <expression>;
o Expression is placed in between < >because specifying an expression is optional.
o If an expression is present, it is converted to thereturn type of the function and it is returned to the
calling function.
o Incase expression is omitted, the return value is undefined.
o A function that has a void as its return type cannot return any value to the calling function.
o If a function has return type void and a return statement ,the compiler will generate warning and
return statement expression will not be evaluated.
o For function that has no return statetment, the control automatically returns to the calling function.
o Programmer may or may not place the expression in a return statement within parantheses.
o By default return type of a function is int.
o A program may have multiple return statements,but only one will get executed.
A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are
four different aspects of function calls.
o function without arguments and without return value
o function without arguments and with return value
o function with arguments and without return value
o function with arguments and with return value
1. #include<stdio.h>
2. void printName();
3. void main ()
4. {
5. printf("Hello ");
6. printName();
7. }
8. void printName()
9. {
10. printf("Javatpoint");
11. }
Example2
1. #include<stdio.h>
2. void sum();
3. void main()
4. {
5. printf("\nGoing to calculate the sum of two numbers:");
6. sum();
7. }
8. void sum()
9. {
10. int a,b;
11. printf("\nEnter two numbers");
12. scanf("%d %d",&a,&b);
13. printf("The sum is %d",a+b);
14. }
1. #include<stdio.h>
2. int sum();
3. void main()
4. {
5. int result;
6. printf("\nGoing to calculate the sum of two numbers:");
7. result = sum();
8. printf("%d",result);
9. }
10. int sum()
11. {
12. int a,b;
13. printf("\nEnter two numbers");
14. scanf("%d %d",&a,&b);
15. return a+b;
16. }
1. #include<stdio.h>
2. int sum();
3. void main()
4. {
5. printf("Going to calculate the area of the square\n");
6. float area = square();
7. printf("The area of the square: %f\n",area);
8. }
9. int square()
10. {
11. float side;
12. printf("Enter the length of the side in meters: ");
13. scanf("%f",&side);
14. return side * side;
15. }
1. #include<stdio.h>
2. void sum(int, int);
3. void main()
4. {
5. int a,b,result;
6. printf("\nGoing to calculate the sum of two numbers:");
7. printf("\nEnter two numbers:");
8. scanf("%d %d",&a,&b);
9. sum(a,b);
10. }
11. void sum(int a, int b)
12. {
13. printf("\nThe sum is %d",a+b);
14. }
Example 2:
1. #include<stdio.h>
2. void average(int, int, int, int, int);
3. void main()
4. {
5. int a,b,c,d,e;
6. printf("\nGoing to calculate the average of five numbers:");
7. printf("\nEnter five numbers:");
8. scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
9. average(a,b,c,d,e);
10. }
11. void average(int a, int b, int c, int d, int e)
12. {
13. float avg;
14. avg = (a+b+c+d+e)/5;
15. printf("The average of given five numbers : %f",avg);
16. }
1. #include<stdio.h>
2. int sum(int, int);
3. void main()
4. {
5. int a,b,result;
6. printf("\nGoing to calculate the sum of two numbers:");
7. printf("\nEnter two numbers:");
8. scanf("%d %d",&a,&b);
9. result = sum(a,b);
10. printf("\nThe sum is : %d",result);
11. }
12. int sum(int a, int b)
13. {
14. return a+b;
15. }
Example2
1. #include<stdio.h>
2. int even_odd(int);
3. void main()
4. {
5. int n,flag=0;
6. printf("\nGoing to check whether a number is even or odd");
7. printf("\nEnter the number: ");
8. scanf("%d",&n);
9. flag = even_odd(n);
10. if(flag == 0)
11. {
12. printf("\nThe number is odd");
13. }
14. else
15. {
16. printf("\nThe number is even");
17. }
18. }
19. int even_odd(int n)
20. {
21. if(n%2 == 0)
22. {
23. return 1;
24. }
25. else
26. {
27. return 0;
28. }
29. }
When the execution control is transferred from calling-function to called-function it may carry one or number of
data values. These data values are called as parameters.
***Parameters are the data values that are passed from calling function to called function.
Actual Parameters
Formal Parameters
**The actual parameters are the parameters that are speficified in calling function.
**The formal parameters are the parameters that are declared at called function. When a function gets
executed, the copy of actual parameter values are copied into formal parameters.
in C Programming Language, there are two methods to pass parameters from calling function to called function
Call by Value
Call by Reference
Call by Value
In call by value parameter passing method, the copy of actual parameter values are copied to formal parameters and
these formal parameters are used in called function. The changes made on the formal parameters does not effect
the values of actual parameters. That means, after the execution control comes back to the calling function, the
actual parameter values remains same.
#include<stdio.h>
void main(){
int num1, num2 ;
void swap(int,int) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
In the above example program, the variables num1 and num2 are called actual parameters and the
variables a and b are called formal parameters. The value of num1 is copied into a and the value of num2 is copied
into b. The changes made on variables a and b does not effect the values of num1 and num2.
It does not change the value of original variable, which keeps the data secure.
When the function is called, it has no effect on the actual parameter’s argument.
In this, the value of the actual parameter goes to the formal parameter, so that if any change is made, its
effect is only on the formal parameter, it does not have any effect on the actual parameter.
In this, you cannot change the value of the original variable inside the function.
The actual argument must be a variable.
The same value is stored in two different variables which are not memory efficient.
Call by Reference
In Call by Reference parameter passing method, the memory location address of the actual parameters is
copied to formal parameters. This address is used to access the memory locations of the actual parameters
in called function. In this method of parameter passing, the formal parameters must be pointer variables.
That means in call by reference parameter passing method, the address of the actual parameters is passed to
the called function and is received by the formal parameters (pointers). Whenever we use these formal
parameters in called function, they directly access the memory locations of actual parameters. So the
changes made on the formal parameters effects the values of actual parameters
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
void swap(int *,int *) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
In the above example program, the addresses of variables num1 and num2 are copied to pointer variables a and b.
The changes made on the pointer variables a and b in called function effects the values of actual
parameters num1 and num2 in calling function.
To get the address of the variables ‘&’ symbol is used infront of the variable.
In call by reference the formal parameters are reference variable which holds the address of another
variable, so to denote that is ‘*’ used in front of the variable.
It does not have to create duplicate variables to store the same value.
This does not cause memory loss.
In this, we can change the value of the original variable inside the function.
By passing the address, we are able to change the value of that variable.
In C , all constants and variables have a defined scope. By scope, we mean the accessibility and
visibility of the variables at different points in the program.
A variable or a constant in c has 4 types of scope: block, function, file and program scope.
Block scope
a statement block is a group of statements enclosed within opening and closing of curly brackets({}).
If a variable declared is declared inside a block then as soon as the control exits that block, the variable
will cease to exist. Such variable is also known as local variable, is said to have a block scope.
Blocks placed one after the other is known as parallel blocks
Blocks defined within another block is known as nested blocks.
Example
#include <stdio.h>
int main() {
// Block
{
//Variables within the block
int a = 8;
int b = 10;
printf ("The values are: %d, %d\n", a, b);
}
return 0;
}
The variables a and b are accessible within the block. Hence, we can successfully display their values inside
the block.
Example2
#include <stdio.h>
int main() {
//Outer Block
{
int a = 10;
//Inner Block
{
int b = 20;
printf ("The value of a is: %d\n", a);
printf ("The value of b is: %d\n", b);
}
printf ("The value of a is: %d\n", a);
}
return 0;
}
Variable a (declared within the outer block) is accessible by the outer and inner blocks.
Variable b (declared within the inner block) is accessible by the inner block only since its block scope is
limited to the inner block only,
Variables declared with same names as those in outer blocks, mask the outer block variables while
executing the inner block variables.
Function Scope
A label (and only a label) declared inside a function is in scope everywhere in that function, in all nested
blocks, before and after its own declaration. Note: a label is declared implicitly, by using an otherwise
unused identifier before the colon character before any statement.
Function scope is only applicable with go to label.
Programmer cannot have the same label name inside a function.
void f()
{
{
goto label; // label in scope even though declared later
label:;
}
goto label; // label ignores block scope
}
void g()
{
goto label; // error: label not in scope in g()
}
Program Scope
In the C language, the variable’s scope refers to the part of the program where we reference it. The two
types of variables in C are:
1. Local variables: These are defined within a function body. They are not accessible outside the
function.
2. Global variables. They are defined outside the function body. Thus, they are accessible to the
entire program.
Global variables declared outside the function bodies have a program scope. The availability of global
variables stays for the entire program after its declaration. Moreover, global variables are initialized
automatically by the compiler.
#include <stdio.h>
int test(){
b = b + a;
return b;
}
int main(){
//Access a
printf ("The value of a is: %d\n", a);
//Access b
printf ("The value of b is: %f\n", b);
return 0;
}
example shows two global variables, a and b. As seen in the code below, these variables are accessible by
both the test function and the main function:
It is accessible through out the program.
If a local variable and global variable has same name, then the local variable is executed inside the
function.
File Scope
When a global variable is accessible until the end of a file, the variable is said to have a file scope.
To allow a variable to have a file scope, declare that variable with the static keyword before specifying the
data type.
static int x;
a global static variable can be used anywhere from the file in which it is declared but it is mot
accessible by any other files.
Storage Classes
We use the storage class in the C language for determining the visibility, lifetime, initial value,
and memory location of any given variable. The storage classes define the visibility (scope) and
the lifetime of any function/ variable within a C program. These classes precede the type that they
are going to modify.
A variable given in a C program will have two of the properties: storage class and type. Here, type
refers to any given variable’s data type, while the storage class determines that very variable’s
lifetime, visibility, and also its scope.
There are four different types of storage classes that we use in the C language:
Automatic Storage Class
External Storage Class
Static Storage Class
Register Storage Class
it is also known as the auto storage class, and it acts as the default storage class for all the variables that are
local in nature.
For example,
{
int mount;
Look at the example that we used above- it defines two of the variables in the very same storage class. One
can use ‘auto’ only within the functions- or the local variables.
The allocation of memory for these variables occurs automatically during the runtime.
The scope of an automatic variable is limited to that block in which we are defining them.
The visibility of these variables is also limited to that block in which we are defining them.
The initialization of these variables is, by default, a garbage value.
The memory that is assigned to an automatic variable gets free when it exits from a block.
We use the keyword auto to define the automatic variables.
Any local variable that exists in the C language is, by default, automatic in nature.
#include <stdio.h>
int main()
{
int p; //auto
char q;
float r;
printf(“%d %c %f”,p,q,r); // to print the initial default value of the automatic variables p, q, and r.
return 0;
}
It is also known as the extern storage class, and we use it for giving a reference of any global variable
which is visible to all the files present in a program. When using the extern storage class, we cannot
initialize the variable. However, note that it points to the name of the variable at any storage location that
we have already defined
Whenever we have multiple numbers of files while we are trying to define any global variable or a function
(that will be used in various other files too), then we will use the extern in another file for providing the
reference of the defined function or variable.
Simpler words, we use the entern for declaring a function or a global variable in another file.
The most common use of the extern modifier is when we have two or more than two files that share a
similar global variable or function.
File1.c
#include <stdio.h>
int a;
extern void display();
int main()
{
printf(“%d”,a);
display();
}
File2.c
#include <stdio.h>
extern int a;
void display()
{
printf(“%d”,a);
}
We use the external storage class for conveying to the compiler that the variable that has been defined as
the extern has been declared using an external linkage that exists elsewhere in any program.
One can only perform the initialization of an external variable globally. In other words, one cannot perform
the initialization of an external variable within any given method or block.
he variables that are declared as extern have no allocation of memory. It only has a declaration, and it
intends to specify that the given variable has been declared elsewhere in the available program.
An external integral type’s default initial value is going to be 0, or else it is null.
We can initialize an external variable multiple times, but we can only initialize it a single time.
When we declare a variable as external, the compiler will start searching for that variable for initialization
somewhere in the available program. It might be static or extern. In case it isn’t, the compiler will
ultimately generate an error (a compile-time error).
#include <stdio.h>
/* declaration of function */
void func(void);
/* a global variable */
static int count = 10;
main() {
while(count–) {
func(); /* increment of function */
}
return 0;
}
void func( void ) {
/* definition of function */
static int x = 10; /* a local type of static variable */
x++;
printf(“x is %d and the count is %d\n”, x, count);
}
Output
x is 11 and the count is 9
We use the register storage class for defining the local variables that must be stored in any register, and not
in a RAM. It means that the maximum size of this variable is equal to that of the register size (it is usually
one word). Also, we cannot apply the ‘&’ unary operator to it because it has no memory location.
For example,
We must only use the register in the case of those variables which require quick access, such as the
counters. We must also note that defining a register doesn’t mean that this variable would be stored in any
register. It rather means that this variable MIGHT or might not be stored in a register. It totally depends on
the hardware and also the restrictions of implementation.
Those variables that we define as the register have their memory allocation into the CPU registers. It
depends totally upon the size of the memory that remains in the CPU.
Its access time is comparatively much faster than that of the automatic variables.
One cannot dereference a register variable. In other words, one cannot make use of the ‘&’ operator in the
case of a register variable.
The default initial value of any given register local value will always be 0.
We use the register keyword for the variable that must be stored in a CPU register. However, whether a
variable must be stored in a register or not is always going to be the choice of the compiler.
One can easily store the pointers in a register. It means that any register is capable of storing the given
variable’s address.
We cannot store a variable into a register since we can’t really utilize more than one storage specifier for
the very same variable.
#include <stdio.h>
int main()
{
register int x; // A variable x has memory allocation in the CPU register. Here, the initial value of x, by
default, is 0.
printf(“%d”,x);
}
Output
0
Recursion
• Recursion is the process of repeating items in a self-similar way.
• In programming languages, if a program allows you to call a function inside the same function, then it is
called a recursive call of the function.
• Recursion is defining large complex problems in terms of smaller and more easily solvable problem.
• Recursion is the process which comes into existence when a function calls a copy of itself to work on a
smaller problem.
• Any function which calls itself is called recursive function, and such function calls are called recursive
calls.
• Recursion involves several numbers of recursive calls. However, it is important to impose a termination
condition of recursion.
• Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be defined in
terms of similar subtasks. For Example, recursion may be applied to sorting, searching, and traversal
problems.
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
Different types of the recursion
1. Direct Recursion
2. Indirect Recursion
3. Tail Recursion
4. No Tail/ Head Recursion
5. Linear recursion
6. Tree Recursion
Direct Recursion
When a function calls itself within the same function repeatedly, it is called the direct recursion.
fun()
{
// write some code
fun();
// some code
}
n the above structure of the direct recursion, the outer fun() function recursively calls the inner
fun() function, and this type of recursion is called the direct recursion.
Indirect Recursion
When a function is mutually called by another function in a circular manner, the function is called an indirect
recursion function.
fun1()
{
// write some code
fun2()
}
fun2()
{
// write some code
fun3()
// write some code
}
fun3()
{
// write some code
fun1()
}
In this structure, there are four functions, fun1(), fun2(), fun3() and fun4(). When the fun1() function is
executed, it calls the fun2() for its execution. And then, the fun2() function starts its execution calls the
fun3() function. In this way, each function leads to another function to makes their execution circularly.
And this type of approach is called indirect recursion.
Tail Recursion
A recursive function is called the tail-recursive if the function makes recursive calling itself, and that recursive call
is the last statement executes by the function. After that, there is no function or statement is left to call the recursive
function.
A function is called the non-tail or head recursive if a function makes a recursive call itself, the recursive call will be
the first statement in the function. It means there should be no statement or operation is called before the recursive
calls. Furthermore, the head recursive does not perform any operation at the time of recursive calling. Instead, all
operations are done at the return time.
Linear Recursion
A function is called the linear recursive if the function makes a single call to itself at each time the function runs and
grows linearly in proportion to the size of the problem.
Tree Recursion
A function is called the tree recursion, in which the function makes more than one call to itself within the recursive
function.