Micromeritics - Fundamental and Derived Properties of Powders
Lec 09. Introduction to Functions / Call by Values
1. Lecture 9Lecture 9
Version 1.0Version 1.0
Introduction to FunctionsIntroduction to Functions
Call by ValueCall by Value
2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
We have already known that in a C program,We have already known that in a C program,
there must be a main () functionthere must be a main () function
In C, we used many library functions as well-In C, we used many library functions as well-
printf (), scanf (), clrscr (), or getch ()printf (), scanf (), clrscr (), or getch ()
printf() prints on the outputprintf() prints on the output
scanf () takes input from the user and assignsscanf () takes input from the user and assigns
that input to a variable by going to the variable’sthat input to a variable by going to the variable’s
addressaddress
3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
clrscr () clears the output bufferclrscr () clears the output buffer
getch () waits for a key-stroke from the usergetch () waits for a key-stroke from the user
if you think uniform one thing is common toif you think uniform one thing is common to
say- the function does one particular job. Isn’tsay- the function does one particular job. Isn’t
it?it?
4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
In C, user can define functions as well- as manyIn C, user can define functions as well- as many
functions as they wish. These functions arefunctions as they wish. These functions are
called user defined functionscalled user defined functions
5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh
Example of FunctionExample of Function
7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh
What We have learnt?What We have learnt?
There is no limit on the number of functionsThere is no limit on the number of functions
that might be present in a C program.that might be present in a C program.
Each function in a program is called in theEach function in a program is called in the
sequence specified by the function calls.sequence specified by the function calls.
After each function has done its job, controlAfter each function has done its job, control
returns to the place of calling that function.returns to the place of calling that function.
8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh
Well, then, should all the function calls takeWell, then, should all the function calls take
place inside the main () function? No! Functionsplace inside the main () function? No! Functions
defined by the user can call other functions asdefined by the user can call other functions as
well.well.
10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
C program is a collection of one or more functions.C program is a collection of one or more functions.
A function gets called when the function name isA function gets called when the function name is
followed by a semicolon.followed by a semicolon.
11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
A function is defined when function name is followedA function is defined when function name is followed
by a pair of braces in which one or more statementsby a pair of braces in which one or more statements
may be present.may be present.
12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
Any function can be called from any other function.Any function can be called from any other function.
EvenEven main( )main( ) can be called from other functions.can be called from other functions.
13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
A function can be called any number of times.A function can be called any number of times.
14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
The order in which the functions are defined in aThe order in which the functions are defined in a
program and the order in which they get called needprogram and the order in which they get called need
not necessarily be samenot necessarily be same
15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
A function can be called from other function, but aA function can be called from other function, but a
function cannot be defined in another function. Thus,function cannot be defined in another function. Thus,
the following program code would be wrong, sincethe following program code would be wrong, since
argentina( )argentina( ) is being defined inside another function,is being defined inside another function,
main( )main( )..
16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
There are basically two types of functions:There are basically two types of functions:
Library functions Ex.Library functions Ex. printf( )printf( ),, scanf( )scanf( ) etc.etc.
User-defined functions Ex.User-defined functions Ex. argentina( )argentina( ),,
brazil( )brazil( ) etc.etc.
18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
Function Declaration or function prototypingFunction Declaration or function prototyping
Function Definition or function bodyFunction Definition or function body
The basic format of function definition is-The basic format of function definition is-
19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
We have seen only void as the return-type in functionsWe have seen only void as the return-type in functions
so far. But it can be of any valid C data type. It specifiesso far. But it can be of any valid C data type. It specifies
what type of data the function will hand over to thewhat type of data the function will hand over to the
callercaller
Parameter-list is a comma separated list containingParameter-list is a comma separated list containing
declaration of parameters received by the functiondeclaration of parameters received by the function
when it is called. We have also seen only void in thewhen it is called. We have also seen only void in the
parameter-lists so far (it means the function does notparameter-lists so far (it means the function does not
get any value from the caller)get any value from the caller)
20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
10,20,30
maximum(10,20,30)
maximum(10,20,30)