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

Lecture 5 Programming

Uploaded by

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

Lecture 5 Programming

Uploaded by

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

Programming-1 Using Python

Dr. Yasmin Hosny Alkady

Faculty of Information Technology & Computer Science


Sinai University
E-Mail: [email protected]
Programming-1
Using Python
Lecture 6
Function& Return Statement

3
Outlines

1) Python Function

2) Return statement

4
1
Python Functions

5
Python Functions

▰ What is a function in Python?


➢ In Python, a function is a group of related statements that performs a specific task.

➢ Functions help break our program into smaller

➢ As our program grows larger and larger, functions make it more organized and
manageable.

6
Creating Functions

▰ In Python a function is defined using the def keyword:


▰ Syntax of Function
def function-name( ):

statement(s)

7
Calling a Function

▰ To call a function, use the function name followed by parenthesis:

8
Arguments

▰ Information can be passed into functions as arguments.


▰ Arguments are specified after the function name, inside the parentheses. You can add as
many arguments as you want, just separate them with a comma.
Argument

9
Number of Arguments

▰ By default, a function must be called with the correct number of arguments. Meaning that if
your function expects 2 arguments, you have to call the function with 2 arguments, not
more, and not less. Arguments

10
Number of Arguments

▰ If you try to call the function with less number of arguments


Two Arguments

The error occurred because, This function expects 2


arguments, but gets only 1:

Calling function with only one Arguments


11
Order of the Arguments

▰ You can also send arguments with the key = value syntax.
Only This way the order of the arguments does not matter.

12
Order of the Arguments

13
2
Return Statement

14
Return Values

▰ To let a function return a value, use the return statement.


▰ Return statement is used for getting result from function not only printing.

15
If not using Return Statement

16
With using Return Statement

▰ Example 1:

17
With using Return Statement

▰ Example 2:

18
With using Return Statement

▰ Example 3:
Compute the area of rectangle .

19
With using Return Statement

▰ Example 4:
Compute the sum of two numbers.

20
With using Return Statement

▰ Example 4:
Compute the sum of two numbers.

21
With using Return Statement

▰ Example 5:
Create a function showEmployee() in such a way that it should accept employee name, and its
salary and display both. If the salary is missing in the function call assign default value 9000 to
salary

22
With using Return Statement

▰ Example 6:
Write a function calculation( ) such that it can accept two variables and calculate the addition and
subtraction of them. And also it must return both addition and subtraction in a single return call

23
Exercise 7

Compute the area of rectangle ,when the user enter Width and Hight of rectangle

24
With using Return Statement

▰ Example 7:
Write a function sum( ) such that it can accept two numbers from the user and calculate the sum of
them. And also it must return addition.

25
THANK YOU

26

You might also like