E ExamMaster

Engineering Python · Functions & Files

Defining Functions

In Engineering Python because labs reuse steps — def, parameters and return package a calculation you can call with new inputs.

`def` names a function, parameters receive inputs, `return` sends a value back to the caller. Calling the function runs its body.

  • Engineering Python
  • Easy level
  • 4 concepts

1def and return

`def name(params):` starts a function. `return value` exits and yields that value to the caller. Without return, the function yields `None`.

Figure. def double(n) starts the function. return n * 2 exits and yields that value to the caller. double(5) therefore yields 10. Without return, the function yields None.

def

def double(n):
    return n * 2

print(double(5))
What does `return x` do inside a function?
  1. Ends the call and yields x to the caller
  2. Prints x always
  3. Deletes x

return provides the call's result.

2Parameters vs arguments

Parameters are the names in the def line. Arguments are the values you pass at the call site. Order matches unless you use keyword arguments.

Figure. Parameters are the names in the def line. Arguments are the values you pass at the call site. Order matches unless you use keyword arguments.

Takeaway

  1. IdeaParameters are the names in the def line. Arguments are the values you pass at the call site. Order matches unless you use keyword arguments.
In Python, what is the value of a function that finishes execution without an explicit return statement?
  1. 0
  2. False
  3. An empty string ""
  4. None

A Python function that terminates without an explicit return returns None implicitly.

3Calling a function

Writing `f(3)` runs `f` with that argument. Assign the result if you need it later: `y = f(3)`.

Figure. Writing add(2, 3) runs add with those arguments. Assign the result if you need it later: y = add(2, 3). return a + b yields 5.

Takeaway

  1. IdeaWriting `f(3)` runs `f` with that argument. Assign the result if you need it later: `y = f(3)`.

Call

def add(a, b):
    return a + b

y = add(2, 3)
print(y)

4Lab: define and call

Write a small function and print its result.

Figure. square(n) returns n*n. The call square(6) runs that body and yields 36 — the value the check can see.

Takeaway

  1. IdeaWrite a small function and print its result.

Coding lab. Define and call runs in the app, with checks on your output.

Notes

  • In Engineering Python because labs reuse steps — def, parameters and return package a calculation you can call with new inputs.
  • `def name(params):` starts a function. `return value` exits and yields that value to the caller. Without return, the function yields `None`.
  • Parameters are the names in the def line. Arguments are the values you pass at the call site. Order matches unless you use keyword arguments.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

def names a reusable calculation; return yields a value; calls pass arguments.

def and return
`def name(params):` starts a function. `return value` exits and yields that value to the caller. Without return, the function yields `None`.
Parameters vs arguments
Parameters are the names in the def line. Arguments are the values you pass at the call site. Order matches unless you use keyword arguments.
Calling a function
Writing `f(3)` runs `f` with that argument. Assign the result if you need it later: `y = f(3)`.
Lab: define and call
Write a small function and print its result.

Practise Defining Functions

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 2 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.