0% found this document useful (0 votes)
6 views

Lec 10 - Functions

srgere3rfwr

Uploaded by

nsim22992
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lec 10 - Functions

srgere3rfwr

Uploaded by

nsim22992
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

CSE-275

Course Title: Computer Programming Language

Lec – 10 Functions

Lec Tasnim Ullah Shakib


1
Scenario!!!

Same set of codes


written multiple times
for different values of
the variable.

2
Dream!!!

3
Function
● A self contained block of code that performs a particular task.

● A function is a group of statements that together perform a task.

● Functions are of 2 types-


● User-defined function
● Predefined library function

4
Function
● A large program in C can be divided into many subprograms or
functions.

● C program contains at least 1 function which is the main()


function.

● The execution of the C program always starts with main


function, but it can call other functions to do some part of the
job.

5
Flow of Function

6
Flow of Function
main()
{ Function2
………….. {
………….. ………...
function1; ………...
…………..
Function3;
…………..
function2; …………..
………….. …………...
} }
Function3
Function1
{
{
………...
………...
………...
………...
}
}

7
How Function Works

function not called.


function called. So, display() not
So, display() executed. executed.

2 arguments sent to display() function

8
How Function Works

Here, main() function is using the display() function.


So main() is the calling function while display() is the called
function.

9
How Function Works

send the actual data.

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.

● Naming rules for a function is the same as the naming rules


for a variable.

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

● What happens when we declare it?


■ We declare its properties to the compiler
■ Properties:
1. Name of variable x
2. Type of variable int

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???????

NO! If you don’t


like it, don’t use it!!
● Write Function
Definition before main()
● For example:

22
Function Declaration vs Function Definition

Code with function declaration Same Code with no function


and then function definition declaration and only 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:

Storing the return


value along with
Function Call

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

Return Parameter 1 Parameter 2


type

A function might have some


int sum(int a, int b) { parameters and might not.
int result = a+b;
Parameters are basically inputs
return result;
that are passed to that function for
} further use from other functions
(mostly from main function)
Return
statement

30
Recap

Parameter Parameter
1 2

int sum(int a, int b) { Here int a and int b are two


int result = a+b; parameters of sum function.
return result;
} From main function, two values
(in1 and in2) are sent as
int main(){ argument in the sum function.
int in1 = 3, in2 = 5;
sum(in1, in2);
}
Argument 1 Argument 2
31
Recap

Parameter 1 Parameter 2

in1 and in2 are variables of the


main function.
int sum(int a, int b) {
int result = a+b; a and b are variables of the sum
return result; function.
}
The value of in1 and in2 are copied
to a and b respectively when in1
int main(){ and in2 are passed as argument
int in1 = 3, in2 = 5; from main function to the sum
function as parameter.
sum(in1, in2);
}
Argument 1 Argument 2
32
Recap

Parameter 1 Parameter 2

int sum(int a, int b) {


int result = a+b; sum: a sum: b
return result;
}

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 sum(int a, int b) { 3 5


int result = a+b; sum: a sum: b
return result;
}

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

It’s okay if you don’t accept it with any


int main(){ other variable from main function.
int in1 = 3, in2 = 5;
int s = sum(in1, in2);
} 3 5
main: in1 main: in2
38
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
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]

In Function Call, while


sending array as
parameter: write only
array name
(Same as 1D array)

43
Passing 2D array as Parameter

If the row and column


values are given
using numbers then
the order doesn’t
matter

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!!!

“a” is a local variable


of main(), so its scope
is limited to only
main().
fun() can’t access it

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

The swap never happened


because of values being
COPIED among Local
variables!!

Output:
a : 50
b : 100

49
Scope of a Variable

Global
Variables

The swap Successfully


happened !!

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

Note: Don’t use global variables in any of these programs

52
Thank You

Special thanks to Lec Afia Miss &


Lec Tasneem Miss for some of
the slide contents
53

You might also like