C Functions

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 56

10/22/2019 B Sudhakar 1

Introduction 2

What is function?

How functions will work?

How to declare a function in C language?

What are the different types of functions are


there in C language?

Example programs.
Introduction 3
Modular Programming 4

Divide and Conquer


Break a large problem into smaller pieces

Smaller pieces sometimes called ‘modules’ or


‘subroutines’ or ‘procedures’ or functions
Why Modular Programming?
5
Helps in managing the complexity of the program

- Smaller blocks of code


- Easier to read
Encourages re-use of code
Within a particular program or across different programs

Allows independent development of code

Provides a layer of ‘abstraction’


What is function? 6

Function is a set of instructions to carry out a particular task.


The Function after processing returns a single value.

Function in programming is a segment that groups a number


of program statements to perform specific task

In other word, we say a Function is a group of statements


that can perform any particular task.
7
Every C program has at least one function, which is main().

We have already used functions like…..


Every function in C must have two things…
8

A function declaration tells the compiler about a


function's name, return type, and parameters.

A function definition provides the actual body of the


function.
9
A function declaration tells the compiler about a function's
name, return type, and parameters.

A function declaration must be done within main function or


before main function.
10
A function definition provides the actual body of the
function.

A function definition must be done before main function or


after main function.
Types of functions? 11

Predefined

Userdefined
Predefined functions 12
If the functionality of a function is defined by the compiler
those functions are called predefined functions.

Actually all predefined functions in C Language are defined


inside in any one of the header files.

The functionality of the predefined function is fixed, users


cannot change or redefine it.
Predefined functions 13

NOTE

Predefined functions are also called as System Defined


functions or Library functions.
User-defined functions 14
If the functionality of a function is defined by the user those
functions are called user-defined functions.

User-defined functions are small programs that you can


write to perform an operation.

Users are free to define their own code for the user-defined
functions. Every user-defined function must have the
following….
Declaring User-defined functions 15
Syntax

Example

NOT
E

We also call it as Function Prototype


Definition of User-defined functions
16
Syntax

Example
Calling User-defined functions 17

Syntax

Example
Working of Functions
18
Example Program
void main() 19
{
int a, b, c;
void addition(int , int);
printf(“We are in main…..\n”);
printf(“Enter any two numbers: ”);
scanf(“%d%d”, &a, &b);
addition(a, b);
printf(“We are again in main…..”);
}
void addition(int x, int y)
{
printf(“SUM = %d\n”, x+y);
}
Important terms in functions
20

It is nothing but a function declaration

It is used to initiate the function execution. That means this


line decides when a function has to be perform its task.

It is nothing but the function definition. It decides what is to


be done by the function on a function call.
Example Program
void main() 21
{
int a, b, c; Prototyp
void addition(int , int); e
printf(“We are in main…..\n”);
printf(“Enter any two numbers: ”);
scanf(“%d%d”, &a, &b);
Calling
addition(a, b);
printf(“We are again in main…..”);
}
void addition(int x, int y) Called
{
printf(“SUM = %d\n”, x+y);
}
Parameters & Return Value 22
Parameters are the data values which are passed from
calling function to called function

Return value is the data value which passed from called


function to calling function

For a function we can have any number of parameters

For a function we can have only one return value

In a function the total number of parameters and the order


of the parameters must be same in all function prototype,
function calling and function definition
Parameters & Return Value 23
To return a value from called function to calling function we
use ‘return’ statement
Parameter types
24
In C programming language there are two types of
parameters
25
These are the parameters used at the time of function
calling

Whenever we pass actual parameters copy of its value is


sent to the called function but not entire variable
26
These are the parameters used at called function as
receivers of the actual parameters

The order of the actual parameters and their datatypes


should exactly match with the order and datatypes of the
formal parameters
Function types
27
Based on the parameters and return value, functions are
classified into FOUR types
Without Parameters and without Return value
28
In this type, there is no data transfer between calling and
called functions

Simple control transfer from calling function to called


function, executes called function body and comes back to
the calling function.

Everything is performed within the called function like


reading data, processing data and displaying result.

This type of functions are used to print some message,


line….
Example Program:
29
void main()
{
void add();
printf(“We are in main….\n”);
add();
printf(“We are again in main….\n”);
}
void add()
{
int a,b,c;
printf(Enter any two numbers: );
scanf(“%d%d”, &a, &b);
c = a + b;
printf(“Result = %d”, c);
}
void main( ) 30
{
void add( ); No input
printf(“We are in main….\n”);
add( );
printf(“We are again in main….\n”); Control
}

void add( )
Control {
int a,b,c;
printf(Enter any two numbers: );
scanf(“%d%d”, &a, &b);
No return value
c = a + b;
printf(“Result = %d”, c);
}
With Parameters and without Return value
31
In this type, there is data transfer from calling to called
function, but not from called to calling function.

Simple control transfer from calling function to called


function along with some data (parameters), executes called
function body and comes back to the calling function
without any data.

This type of functions are depend on the calling function.


Generated result is utilized by called function and nothing
will be sent back to the calling function.
Example Program:
void main()
{
void add( int, int );
printf(“We are in main….\n”);
add( 10, 20);
printf(“We are again in main….\n”);

B Sudhakar
}
void add(int a, int b)
{
int c;
c = a + b;
printf(“Result = %d”, c);
}

10/22/2019 32
33
void main( )
{
void add( int, int ); 10 & 20 as input
printf(“We are in main….\n”);
add( 10, 20 );
printf(“We are again in main….\n”); Control
}

void add( int a, int b)


Control {
int c;
c = a + b;
No return value printf(“Result = %d”, c);
}
Without Parameters and with Return value
34
In this type, there is no data transfer from calling to called
function, but from called to calling function one data is sent.

Simple control transfer from calling function to called


function, executes called function body and a data value is
sent back to the calling function from called function.

This type of functions are called function is independent. It


reads data, process data and result is sent back to the calling
function.
Example Program:
void main()
{
int c;
int add( );
printf(“We are in main….\n”);
c = add( );

B Sudhakar
printf(“Result = %d\n”, c);
}
int add( )
{
int a, b;
printf(“Enter any two numbers:”);
scanf(“%d%d”, &a, &b);
return (a + b);
10/22/2019
} 35
void main( ) 36
{
int c;
int add( ); No input
printf(“We are in main….\n”);
c = add( );
printf(“Result = %d\n”, c); Control
}

void add( )
{
Control int a,b;
printf(Enter any two numbers: );
30 as return value scanf(“%d%d”, &a, &b);
return (a + b);
}

input values for a & b are 10 & 20 respectively


With Parameters and with Return value
37
In this type, there is data transfer from calling to called
function, and from called to calling function one data is sent
back.

Control transfer from calling function to called function


along with data, executes called function body and a data
value is sent back to the calling function from called
function.

In this type of functions are called & calling functions both


dependent on each other.
Example Program:

void main()
{
int c;
int add( int, int );
printf(“We are in main….\n”);

B Sudhakar
c = add( 10, 20 );
printf(“Result = %d\n”, c);
}
int add( int a, int b)
{
return (a + b);
}

10/22/2019 38
void main( ) 39
{
int c;
int add( int, int );
printf(“We are in main….\n”); 10 & 20 as input
c = add( 10, 20 );
printf(“Result = %d\n”, c);
} Control

Control
void add( int a, int b)
{
30 as return value return (a + b);
}
Different ways to make a function call
40
There are THREE ways of making function call…
1. From main function 41

We have already seen making a function call from main


function.

When we make a function call from main, the control


transfers to called function, executes it, again comes back to
the main function.
2. From a user-defined to another 42
user-defined function
We can also make a function call from one user-defined
function to another user-defined function.
void Calculator( int a, int b)
{ No input

return add(a,b);
} Control

Control int add( int a, int b)


{
return value return (a + b);
}
43
void main()
{
function1();
}
void
function1()
{
function2();
}

void function2()
{
body of the
function;
}
3. From a user-defined to same 44
user-defined function (recursive)
We can also make a function call from same function. That
means function calls itself

If a function calls itself, then it is called as “RECURSION”.

When a function calls itself until the last call is invoked till
that time the first call also remains open.

At every time, a function invoked, the function returns the


result of previous call.

At every recursion there is two steps, (1) a function invoked,


and (2) base recursion termination or exit.
Example Program:
void main( )
{
printf(“This is example of Recursion!!!”);
main( );
}

B Sudhakar
Output:
This is example of Recursion!!!
This is example of Recursion!!!
This is example of Recursion!!!
This is example of Recursion!!!
…….
10/22/2019 45
Example Program:
46
int factorial( int );
void main( )
{
int fact, n;
printf(“Enter any positive integer: ”);
scanf(“%d”, &n);
fact = factorial( n );
printf(“Factorial of %d is %d”, n, fact);
}
int factorial( int n )
{
int temp;
if( n == o)
return 1;
else
temp = n * factorial( n-1 );
return temp;
}
int factorial( int );
void main( ) 47
{
int fact, n;
printf(“Enter any positive integer: ”);
scanf(“%d”, &n);
fact = factorial( n );
printf(“Factorial of %d is %d”, n,
fact);
}

int factorial( int n )


{
int temp;
if( n == o)
return 1;
else
temp = n * factorial( n-1 );
return temp;
}
Memory Allocation
int factorial( int n )
{
n=3 n temp
int temp; 3 648
if( n == o) n temp
3 * factorial(2)
6 else
return 1;
3*2 = 6 2 2
temp = n * factorial( n-1 ); n temp
return temp;
int factorial( int n ) n = 2
1 1
}
{
n temp
int temp; 0
if( n == o)
return 1; 2 * factorial(1)
2
else 2*1 = 2
temp = n * factorial( n-1 );
return temp;
} int factorial( int n ) n=1
{
int temp;
if( n == o)
return 1;
1 * factorial(0)
1 else 1*1 = 1
temp = n * factorial( n-1 );
return temp;
} int factorial( int n ) n = 0
{
int temp;
if( n == o)
return 1;
1 else
temp = n * factorial( n-1 );
Recursion types 49
Depending on the function call recursion can be
categorized into following type
1. Linear Recursion or Direct 50
Recursion
In linear recursion a function calls exactly once to
itself each time the function is invoked, and grows
linearly in proportion to the size of the problem.

int factorial( int n )


{
int temp;
if( n == o)
return 1;
else
temp = n * factorial( n-1 );
return temp;
}
In linear recursion a function call may define at any
statement
2. Tail Recursion 51
Tail Recursion is another form of linear recursion,
where the function makes a recursive call as its very
last operation.
void recursiveRev (int * arr, int i, int j) int fact (int a)
{ {
if(i < j) { if(a<=1)
int tmp; return 1;
tmp = arr[i]; else
arr[i] = arr[j]; return (a*fact(a-1));
arr[j] = tmp;
recursiveRev (arr, i+1, j-1); }
}
}

In tail recursion a function call define at end


statement only
3. Binary Recursion 52
In binary recursion a function makes two recursive
calls to itself when invoked, it uses binary recursion.
Fibonacci series is a very nice example to
demonstrate binary recursion.
int febnum (int n) febnum (4)
{
if(n < 1)
return -1; febnum (3) febnum (2)

else if(n<=2)
return 1;
else febnum (2) febnum (1)

return (febnum(n-1)+febnum(n-2));
}
febnum (1) febnum (0)
4. Mutual Recursion or Indirect 53
Recursion
Calling two or more functions mutual is called
mutual recursion. Say for example, if function A is
calling B and function B is calling A recursively then it
is said that, they are in mutual recursion.
int isOddNumber(int n) int isEvenNumber(int n)
{ {
if (0 == n) if(0 == n)
return 0; return 1;
else else
return isEvenNumber(n - 1); return isOddNumber(n - 1);
} }
Here in the following example isEvenNumber() is calling
isOddNumber() and isOddNumber() is calling isEvenNumber()
5. Nested Recursion 54
When a recursive method has a parameter defined in
terms of itself then it is called nested recursion.
Implementation of classical mathematical function,
Ackermann’s, is good example of nested function.
int ackerman(int m, int n)
{
if(m==0)
return (n+1);
else if(n==0)
return ackerman(m-1,1);
else
return (ackerman(m-1,ackerman(m,n-1)));
}
In nested recursion a function call have recursive int that
recursive call contain another recursive call or a function call
Example for nested recursive 55
#include<stdio.h>
int main()
{
int a,b;
printf("Enter four integers:\n");
scanf("%d%d",&a,&b);
s=ackerman(a,b);
printf(“The result= %d",s);
return 0;
}
int ackerman(int m, int n)
{
if(m==0)
return (n+1);
else if(n==0)
return ackerman(m-1,1);
else
return (ackerman(m-1,ackerman(m,n-1)));
}
Example for mutual Recursion 56
int iseven(int num)
{
if(n<=num)
{
Output: 2 1 4 3 6 5 8 7 10 9
printf("%d\t",n-1);
n++;
return isodd(num);
}
#include<stdio.h>
}
int n=1
int isodd(int num)
void main()
{
{
if(n<=10)
int a;
{
printf("Enter an number");
printf("%d\t",n+1);
scanf("%d",&a);
n++;
isodd(a);
return iseven(num);
}
}
}

You might also like