Lec 10 - Functions
Lec 10 - Functions
Lec – 10 Functions
2
Dream!!!
3
Function
● A self contained block of code that performs a particular task.
4
Function
● A large program in C can be divided into many subprograms or
functions.
5
Flow of Function
6
Flow of Function
main()
{ Function2
………….. {
………….. ………...
function1; ………...
…………..
Function3;
…………..
function2; …………..
………….. …………...
} }
Function3
Function1
{
{
………...
………...
………...
………...
}
}
7
How Function Works
8
How Function Works
9
How Function Works
Here,
Value of int variable a copied to int variable b.
Value of char variable c copied to char variable d.
10
Structure of a Function
Function Declaration
11
Structure of a Function
Function Declaration
Function Call
12
Structure of a Function
Function Declaration
Function Call
Function Definition
13
Function Definition
● The general form of a Function Definition is:
return_type function_name( parameter list )
{
// body of the function
}
● So there are 4 parts of a Function Definition:
■ Return Type
■ Function Name
■ Parameter List
■ Function Body
14
Parts of a Function Definition– Return Type
● A function may return a value.
● The return_type is the data type of the value the function
returns.
● Some functions perform the desired operations without
returning a value. In this case, the return_type is the
keyword void.
15
Parts of a Function Definition – Function Name
● Every function has a name by which it is known to the rest
of the program.
16
Parts of a Function Definition – Parameter List
● A parameter is like a placeholder. When a function is invoked, you pass a
value to the parameter.
● This value is referred to as actual parameter or argument. The parameter
list refers to the type, order, and number of the parameters of a function.
● Parameters are optional; that is, a function may contain no parameters.
No Parameters
Parameter list
Argument list
17
Parts of a Function Definition – Function Body
● The function body contains a collection of statements
that define what the function does.
18
Function Declaration
● How do we declare a variable??
int x
19
Function Declaration
● Similarly we can say that, Function Declaration ( also called
Function Prototype/ Forward Declaration) means declaring the
properties of a function to the compiler
● The general form of a Function Declaration is:
return_type function_name( parameter list );
● For example:
● Properties:
1. Name of the function: sum
2. Return type of the function: int
3. Number of parameters: 2
4. Type of parameter 1: int
5. Name of Parameter 1: a
6. Type of parameter 2: int
7. Name of parameter 2: b
20
Function Declaration
● It is not necessary to put the name of the parameters
in function prototype
● For example we can write it as:
● Properties:
1. Name of the function: sum
2. Return type of the function: int
3. Number of parameters: 2
4. Type of parameter 1: int
5. Type of parameter 2: int
21
Function Declaration
● Is it mandatory to declare
function???????
22
Function Declaration vs Function Definition
23
Function Call
● When the return type of a
function is void, to call that
function:
■ you simply need to pass the
required parameters along
with the function name
■ For example:
Function Call
24
Function Call
● When the return type of a
function is NOT void, to
call that function:
■ you need to pass the
required parameters along
with the function name
■ Also you need to store the
returned value.
■ For example:
25
Four Cases of Function
● No arguments/parameters and No return value
26
Four Cases of Function
● Arguments/Parameters but No return value
27
Four Cases of Function
● Arguments/Parameters with return value
28
Four Cases of Function
● No arguments/parameters but return value
29
Recap
30
Recap
Parameter Parameter
1 2
Parameter 1 Parameter 2
Parameter 1 Parameter 2
int main(){
int in1 = 3, in2 = 5; 3 5
sum(in1, in2); main: in1 main: in2
}
Argument 1 Argument 2
33
Recap
Parameter 1 Parameter 2
int main(){
int in1 = 3, in2 = 5; 3 5
sum(in1, in2); main: in1 main: in2
}
Argument 1 Argument 2
34
Recap
Return
type
3 5
sum: a sum: b
int sum(int a, int b) {
int result = a+b;
return result; sum: result
}
int main(){
int in1 = 3, in2 = 5;
int s = sum(in1, in2);
} 3 5
main: in1 main: in2
35
Recap
Return
type
3 5
sum: a sum: b
int sum(int a, int b) {
int result = a+b; 8
return result; sum: result
}
int main(){
int in1 = 3, in2 = 5;
int s = sum(in1, in2);
} 3 5
main: in1 main: in2
36
Recap
Return
type
3 5
sum: a sum: b
int sum(int a, int b) {
int result = a+b; 8
return result; sum: result
} Return result
int main(){
int in1 = 3, in2 = 5;
int s = sum(in1, in2);
} 3 5
main: in1 main: in2
37
Recap
Return
type
3 5
sum: a sum: b
int sum(int a, int b) {
int result = a+b; 8
return result; sum: result
} Return result
Return
type
3 5
sum: a sum: b
int sum(int a, int b) {
int result = a+b; 8
return result; sum: result
} Return result
Or, you might accept
int main(){
it like this program 8
main: s
int in1 = 3, in2 = 5;
int s = sum(in1, in2);
} 3 5
main: in1 main: in2
39
Conditional Return Value
Any Shortcut
possible??
40
Conditional Return Value
41
Passing 1D array as Parameter
In Function
Definition and
Declaration, while
receiving array as
parameter: write
array name with [ ]
In Function Call,
while sending array
as parameter: write
only array name
42
Passing 2D array as Parameter
The column parameter should be written before the 2d array
In Function Definition
and Declaration, while
receiving array as
parameter: write array
name with [ ] [col]
43
Passing 2D array as Parameter
44
Passing String as Parameter
In Function
Definition and
Declaration, while
receiving string as
parameter: write
string name with [ ]
In Function Call,
while sending
string as parameter:
write only string
name
45
Scope of a Variable
● A scope in any programming is a region of the program where
a defined variable can have its existence and beyond that
variable it cannot be accessed.
● We can say that, scope of a variable is the lifetime of a
variable.
● There are 2 places where a variable can be declared:
■ Inside a function which is called Local Variables
■ Outside of all functions which is called Global Variable
46
Scope of a Variable – Local Variable
● They can be used only by statements that are inside
that function or block of code.
Error!!!
47
Scope of a Variable – Global Variable
● Global variables are defined outside a function, usually on top of the
program.
● A global variable can be accessed by any function.
“a” is a Global variable.
All of the functions can
access “a”
Output:
a : 10
a : 10
48
Scope of a Variable
a = 50, b = 100
temp = 50
a = 100
b = 50
Output:
a : 50
b : 100
49
Scope of a Variable
Global
Variables
Output:
a = 100
b = 50
50
Advantages of Function
● Code re-usability increases : A function can be used to keep away
from rewriting the same block of codes which we are going use two
or more locations in a program.
● Increases program readability : This is especially useful if the
code involved is long or complicated. By seeing the function names
we can easily determine what is the purpose of the program.
● Program testing becomes easy : Easy to locate and isolate a faulty
function for further investigation.
● Program development made easy : Work can be divided among
project members thus implementation can be completed in parallel.
● Code sharing becomes possible : A function may be used later by
many other programs this means that a c programmer can use
function written by others, instead of starting over from scratch.
51
Some Practice Problems of Function
● Write a program to check prime and perfect numbers
using function
● Write a program to find all prime numbers in given range
using functions
● Write a program to find diameter, circumference and area of
circle using function
52
Thank You