Sunteți pe pagina 1din 36

Functions

Definition
A function is a group of statements that
performs some kind of task.
3 essential things about Function
function prototype can be thought of as
specifying its interface
Function calling when a function is being
called by some other function
function definition specifies what a function
does.

Advantages of Functions
Less code duplication
easier to read
Easier to update programs
Easy to find out errors--each function can be
verified separately
Reusable code the same functions can be
used in different programs
Syntax of Function
Return_type function_ name (arguments)
{
Body of function
Return value;
}
Program of addition WITHOUT using
Functions
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a , b, c;
Scanf(%d%d, &a,&b);
c=a+b;
Printf(value of c= %d,c);
}



Scenario 1
Main function will call another function to do
its own task.

For example--- main function will call add
function to do the addition and display the
result
Program of addition using Functions
#include<stdio.h>
#include<conio.h>
Void add(); // function declaration
Void main()
{
Add(); // calling of add function
}

Void add() // function definition
{
Int a , b, c;
Scanf(%d%d, &a,&b);
c=a+b;
Printf(value of c= %d,c);
}

Doing the addition MORE THAN ONE
TIME in the same program



without_function.doc
Using Function


using_function.doc
Calling Function & Called function
In previous example,

Main function is the Calling function----who is
making a call to add function

Add function is said to be called function
who is responsible for actually performing the
addition of two numbers
Main function --------------is the master

Add function -------------is the slave , who is
performing some task.
Scenario 2
Main function will call another function to do
its own task + main function wants to print
result of its own.

For example--- main function will call add
function to do the addition, now after doing
addition the result is sent back to main
function , then main function will display
the result.
Use of return Statement

#include<stdio.h>
#include<conio.h>
int add(); // function declaration
Void main()
{
Int basket;
basket=Add(); // calling of add function
Printf(value of addition = %d, basket);
}

int add() // function definition
{
Int a , b, c;
Scanf(%d%d, &a,&b);
c=a+b;
return c;
}

Another example
Find out the greater number among two
numbers using Functions , if a>b then
calculate the Quotient , otherwise set answer
=0.
grtr_without using return.docx
using return statement
using_return.doc

Disadvantage of return statement


We can only ONE value from one function to
another function
Program to check whether a given number is
EVEN or ODD using Function


chk_even_odd.docx
Program to calculate the AREA of CIRCLE using
function
area_function.doc
Program to calculate the AREA of CIRCLE using
function , using RETURN STATEMENT
area_return.doc

Scenario 3
Earlier responsibility of main function is just
to call another function.
Another function is responsible for-----------
Taking the input of two numbers
doing the addition of numbers.
BUT NOW , main function wants to take
input of two numbers of its own , wants to
pass these numbers to another function.
How to pass values from one
function to another

Either using
------------Call by VALUE

Or using
------------Call by REFERENCE / Call by ADDRESS
What is parameter/ argument
Passing values from one function to another
function ---------------is said to be PARAMETER
or ARGUMENT.
TWO TYPES of parameter/ argument-----
ACTUAL PARAMETER----used at the time of
function calling
FORMAL PARAMETER-----used at the time of
function definition

What is call by VALUE & call by
REFERENCE
In call by value , we pass the actual values
at the time of function calling.
In call by reference, we pass address
instead of values, at the time of function
calling.

Local variable & Global Variable
Local variables------------
----------------are limited to one block.
----------------its value does not exist in any
other function.
-------------by default value is garbage
Global Variable-----------
--------------- are used in whole program
---------------by default value is zero
---------------its value persists for entire program


Addition of two numbers using call
by VALUE

call_value.doc
Area of circle using Call by VALUE
and using RETURN

call_value_return.doc
Program of factorial using FUNCTIONS
# include<stdio.h>
#include<conio.h>
int fact(int a);
Void main()
{
Int z, basket;
scanf(%d, &z);
basket= Fact(z);
Printf(factorial is=%d , basket);
}
Int fact(int a)
{
Int i, ans=1;
For (i= 1; i<=a; i++)
{
ans= ans* i;
}
Return ans;
}
Scenario 4
Instead of passing values as arguments,
Pass the ADDRESSES of variables , at the time
of function calling.
POINTER
It is a special type of variable,
which contains the address of any other
variable , instead of value.

int * p;
Int q;
P= &q;

Addition of two numbers using call
by REFERENCE
call_ref.doc

Area of circle using Call by REFERENCE
and using RETURN
call_ref_return.doc
S. NO. call by value call by reference
1. Values are passed at the
time of function
Address is passed at the
time of function
2. No use of pointer use of pointer
3. Simple , easy to
understand
Complicated, difficult to
learn
4. Also knows as downward
communication
Also knows as upward
communication
5. NO need to worry about
the memory addresses
User must know about
the concepts of the
memory addresses
6. ( IMPORTANT) Changes made in
function will not
reflected back in main
function
Changes made in
function will reflected
back in main function
Swapping of two numbers using CALL BY VALUE
#include<stdio.h>
#include<conio.h>
Void swap(int a, int b);
Void main()
{
Int a, b;
Scanf(%d %d, &a , &b);
Swap(a,b);
Printf(value of a= %d \n value of b= %d, a, b);
}
Void swap(int a, int b)
{
Int temp;
Temp=a;
a=b;
b=temp;
Printf(value of a= %d \n value of b= %d , a, b);
}
Swapping of two numbers using CALL BY REFERENCE

#include<stdio.h>
#include<conio.h>
Void swap(int *a, int *b);
Void main()
{
Int a, b;
Scanf(%d %d, &a , &b);
Swap(&a,&b);
Printf(value of a= %d \n value of b= %d, a, b);
}
Void swap(int *a, int *b)
{
Int temp;
Temp=*a;
*a=*b;
*b=temp;
Printf(value of a= %d \n value of b= %d , *a, *b);
}

Recursion
A function can call itself either directly or
indirectly.
We can use recursion , where we want to
execute the same set of instructions,
repeatedly.
Factorial of a number using RECURSION
#include<stdio.h>
#include<conio.h>
int fact(int a);
void main()
{
int z,ans;
scanf("%d", &z);
ans=fact(z);
printf("factorial is = %d", ans);
}
int fact(int b)
{
int ans;
if(b==0)
return 1;
else
return b*fact(b-1);
}

Difference between Recursion and
Iteration
S. No. Recursion Iteration
1. A new copy of local
variable is
generated, when a
function call is made
recursively.
A new copy of local
variable is not
generated, when a
function call is
made.
2. Consumes much
memory
Consumes less
memory
3. Time consuming Less time
consuming
4. More complex Less complex

S-ar putea să vă placă și