Lecture 9Lecture 9
Version 1.0Version 1.0
Introduction to FunctionsIntroduction to Functions
Call by ValueCall by Value
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
 We have already known th...
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
 clrscr () clears the out...
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to FunctionsIntroduction to Functions
 In C, user can define fu...
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
Example of FunctionExample of Function
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Another ExampleAnother Example
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
What We have learnt?What We have learnt?
 There is no limit on the number of...
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
 Well, then, should all the function calls takeWell, then, should all the fu...
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 C program is a collection of on...
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 A function is defined when func...
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 Any function can be called from...
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 A function can be called any nu...
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 The order in which the function...
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 A function can be called from o...
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
 There are basically two types o...
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
 Function Declaration or function prototypingFun...
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
 We have seen only void as the return-type in fu...
20Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
10,20,30
maximum(10,20,30)
maximum(10,20,30)
21Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
Upcoming SlideShare
Loading in …5
×

C-09

174 views

Published on

Published in: Education, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
174
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C-09

  1. 1. Lecture 9Lecture 9 Version 1.0Version 1.0 Introduction to FunctionsIntroduction to Functions Call by ValueCall by Value
  2. 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. 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. 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. 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh Example of FunctionExample of Function
  6. 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh Another ExampleAnother Example
  7. 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. 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.
  9. 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh
  10. 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. 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. 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. 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. 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. 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. 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.
  17. 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh
  18. 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. 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. 20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh Call by ValueCall by Value 10,20,30 maximum(10,20,30) maximum(10,20,30)
  21. 21. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh Call by ValueCall by Value

×