Sie sind auf Seite 1von 23

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : Murali Krishna Chintala
Designation : Lecturer in CME
Branch : Computer Engineering
Institute : SUVR & SR GPW, Ethamukkala
Year/ Semester : III Semester
Subject : UNIX & C
Subject Code : CM – 304
Topic : Understand Modular Programming
Duration : 50 Min
Sub Topic : Function call techniques.
Teaching Aids : PPT, Animations
CM304.63 1
Objective

On completion of this period, you would be able


to :

• Understand different function types.

• Write programs using different function call


techniques.

CM304.63 2
Parameters can be passed to a function in two
ways

• Call by value

• Call by reference

CM304.63 3
Call by value

• Actual arguments are copied into the


corresponding formal arguments.

• Changes done to the formal parameters have


no effect on the actual parameters because
actual parameters and formal parameters
have separate memory locations.

CM304.63 4
Call by reference

• Instead of values, only address of the actual


parameters is passed to the formal
parameters.

• Formal parameters are pointer variables and


they point to the same memory locations of
the corresponding actual parameters.

• So any changes done to the formal


parameters effect the actual parameters.

CM304.63 5
Call by reference
Contd..

• We use call by value mechanism if a single


value has to be returned.

• When more than one value is desired we use


call by reference mechanism to indirectly
transfer the resulting values back to the
calling function.

CM304.63 6
Types of user defined functions

• Functions with no arguments and no return


values.

• Functions with arguments but no return


values.

• Functions with arguments and return values.

CM304.63 7
Functions with no arguments and
no return values
Syntax:
main()
{
----
fun()
---
}
fun()
{
---
}
CM304.63 8
Functions with no arguments and
no return values
Contd..

• As the function fun() has no arguments,


main() cannot send any data to fun().

• Since fun() has no return statement it cannot


return any value to main().

CM304.63 9
Functions with no arguments and
no return values
Contd..

Example:
#include <stdio.h>
void fun();/* function proto type*/
main()
{
fun();
}

CM304.63 10
Functions with no arguments and
no return values
Contd..

Example:
void fun()
{
int n;
scanf( “%d”, &n);
printf( “%d”, n);
}

CM304.63 11
Functions with arguments and
no return values

• Functions can have parameters, hence


calling function can send data to the called
function but it cannot return any value to the
calling function.

CM304.63 12
Functions with arguments and
no return values Contd..

Example:

#include <stdio.h>
int sum(int,int); /*function prototype*/
main()
{
scanf(“%d%d”,&x,&y);
printf(“%d”,sum);
}
CM304.63 13
Functions with arguments and
no return values Contd..

Example:

int sum(int a, int b)


{
int sum=0;
sum=a+b;
printf(“%d”,sum);
}

CM304.63 14
Functions with arguments and
return values

• Since functions have parameters, the calling


function can send data to the called function.

• The called function can return the resultant


value to calling function with the use of return
statement.

CM304.63 15
Contd..
Example:
#include<stdio.h>
int sum(int,int);
main()
{
int x,y,z;
scanf(“%d%d”,&x,&y);
z=sum(x,y);
printf(“%d”,z);
}

CM304.63 16
Contd..
Example:

int sum(int a,int b)


{
int sum=0;
sum=a+b;
return(sum);
}

CM304.63 17
Summary
At the end of this class, you have learnt about..

• Parameters can be passed either by call by value or


call by reference.

• User defined functions are classified into

1. No arguments and no return values


2. Arguments and no return values
3. Arguments and return values

CM304.63 18
QUIZ

1. Call by value mechanism is used to return .


a) One value
b) Two values
c) Many
values

CM304.63 19
QUIZ

1. Call by value mechanism is used to return..

 One value
 Two values
 Many values

CM304.63 20
QUIZ

2. Call by reference mechanism is used to return..


a) One value
b) Two values
c) Many values

CM304.63 21
QUIZ

2. Call by reference mechanism is used to return..


 One value
 Two values
 Many values

CM304.63 22
Frequently Asked Questions

1.Write a program to find the biggest of three


numbers using functions?

2.Write a program to sort the given numbers in


ascending order using functions?

CM304.63 23

Das könnte Ihnen auch gefallen