c-09
DESCRIPTION
TRANSCRIPT
Lecture 9Lecture 9Version 1.0Version 1.0
Introduction to FunctionsIntroduction to Functions
Call by ValueCall by Value
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to Introduction to FunctionsFunctions
We have already known that in a C We have already known that in a C program, there must be a main () function program, there must be a main () function
In C, we used many library functions as In C, we used many library functions as well- printf (), scanf (), clrscr (), or getch well- printf (), scanf (), clrscr (), or getch () ()
printf() prints on the output printf() prints on the output scanf () takes input from the user and scanf () takes input from the user and
assigns that input to a variable by going assigns that input to a variable by going to the variable’s address to the variable’s address
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to Introduction to FunctionsFunctions
clrscr () clears the output buffer clrscr () clears the output buffer getch () waits for a key-stroke from getch () waits for a key-stroke from
the user the user if you think uniform one thing is if you think uniform one thing is
common to say- the function does common to say- the function does one particular job. Isn’t it? one particular job. Isn’t it?
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Introduction to Introduction to FunctionsFunctions
In C, user can define functions as In C, user can define functions as well- as many functions as they wish. well- as many functions as they wish. These functions are called user These functions are called user defined functionsdefined functions
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 There is no limit on the number of functions that might be present in a functions that might be present in a C program. C program.
Each function in a program is called Each function in a program is called in the sequence specified by the in the sequence specified by the function calls.function calls.
After each function has done its job, After each function has done its job, control returns to the place of control returns to the place of calling that function.calling that function.
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
Well, then, should all the function Well, then, should all the function calls take place inside the main () calls take place inside the main () function? No! Functions defined by function? No! Functions defined by the user can call other functions as the user can call other functions as well. well.
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 one or more C program is a collection of one or more functions. functions.
A function gets called when the function A function gets called when the function name is followed by a semicolon. name is followed by a semicolon.
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
A function is defined when function A function is defined when function name is followed by a pair of braces in name is followed by a pair of braces in which one or more statements may be which one or more statements may be present. present.
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
Any function can be called from any Any function can be called from any other function. Even other function. Even main( ) main( ) can be can be called from other functions. called from other functions.
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 A function can be called any number of times. times.
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
The order in which the functions are The order in which the functions are defined in a program and the order in defined in a program and the order in which they get called need not which they get called need not necessarily be same necessarily be same
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!! A function can be called from other A function can be called from other
function, but a function cannot be defined in function, but a function cannot be defined in another function. Thus, the following another function. Thus, the following program code would be wrong, since program code would be wrong, since argentina( ) argentina( ) is being defined inside is being defined inside another function, another function, main( )main( )..
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
We have known a lot!!We have known a lot!!
There are basically two types of There are basically two types of functions: 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.
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
Function Declaration or function Function Declaration or function prototypingprototyping
Function Definition or function bodyFunction Definition or function body The basic format of function definition is- The basic format of function definition is-
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
Call by ValueCall by Value
We have seen only void as the return-type We have seen only void as the return-type in functions so far. But it can be of any valid in functions so far. But it can be of any valid C data type. It specifies what type of data C data type. It specifies what type of data the function will hand over to the caller the function will hand over to the caller
Parameter-list is a comma separated list Parameter-list is a comma separated list containing declaration of parameters containing declaration of parameters received by the function when it is called. received by the function when it is called. We have also seen only void in the We have also seen only void in the parameter-lists so far (it means the function parameter-lists so far (it means the function does not get any value from the caller) does not get any value from the caller)
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