You are on page 1of 9

Computer Programming

ENEE 101
Lec#13 (Functions part 2)
Department of Electrical and Electronic Engineering
University of Jeddah 1
The main Function And Its Use
• Many languages, such as C, C++, Java, and several others, define a special function that must
be called main() that the operating system automatically calls when it executes the program.

• This function is often called the entry point because it is where the execution enter (starts)
the program.

• By contrast, Python does not have a special function that serves as the entry point to a
script. You can give the entry point function in a Python script any name you want!

• Although Python does not assign any significance to a function named main(), the best
practice is to name the entry point function main() anyways.

• This can help other programmers to know the starting point of your script.
Ref: https://realpython.com/python-main-function/ 2
Using main Function: Example-1 (finding sum)

Defining a sum function

Defining a main function

Calling/invoking a main function


(python entry point)
3
Using main Function: Example-2 (finding max)

4
Transfer of Control (Flow of Program with main function)

Entry
Point

5
Functions with/without Return Values
• A function does not have • Instead, when using return, the retuned value
to return a value. can be saved in a variable for later usage.

6
Q/A
• Show the output of the following codes:

Return is required to pass a value after the function execution. 7


Q/A
• Find the output of the following code:
def Fun7:
print(“I am 7”)
print(“I do nothing”)

def myfun(a, b):


result = a*10 + b
print(“result = ”, result)
Fun7()
print(“I am still in myfun”)
return result
Fun7()
A = myfun(2,7)
print(A)

8
Returning Multiple Values

You might also like