Functions Flashcards

1
Q

Explain calling a function and what a function call is

A
  • Using function = calling
  • Line of code that uses function = function call
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What is a function?

A
  • Code that you’ve given a name to (such as print, str, iter, or next).
  • Accomplish a task.
  • When you use that function name in a program, execution of the code jumps to that function block, executes the function, and then returns to the point where the function name was called
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to define and call a simple function? Give example

A
def my_first_function():
    print("Hello you")

my_first_function()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define Scope

A
  • Scope refers to the definition of variables or functions in Python and the accessibility in different parts of program .
  • Applies to variables and functions

**Local Scope: **
Variables created within a function and accessible only inside that function.

Global Scope:
Variables defined outside any function, accessible anywhere in the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What scope rule exists with variables created in functions?

A

If a variable was created in a function, it only exists in that function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when you define my_variable inside and outside a function and both Variables have the same name

A
  • Because the variable within the function only exists within the function, to the rest of the program it is as if that variable does not exist.
  • That’s why when we change the value of that variable, it does not change the value of the variable outside of the function.
  • The interpreter actually creates two variables with the same name, one within the function and one outside of the function. Changing the value of one will not affect the other.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a nested function?

A
  • Function defined within another function.
  • Scope of parent function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a global Variable and how to access it within functions. Demonstrate.

A

You use the global keyword to preface the variable you’re going to use. This will tell the interpreter that you want to use the global version of that variable instead of creating a new local version

x = 1
def my_function()
    global x
    x = 10
    print(x)

my_function()
print(x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a nonlocal variable?

A
  • Access a function’s local variable from a nested function within that function.
  • nonlocal keyword.
  • Tells interpreter that you want to use the version created in the parent function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are Arguments?

A

Values that are passed into functions via parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Where are function parameters defined?

A

Between open and closed parentheses is the function definition
Each parameter is seperated by a comma

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define an example of a simple function with parameters

A
def add_num(a, b, c)
    the_sum = a + b + c
    print(the_sum)
    
add_num(1, 5, 6)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can you think of an important rule for arguments passed into function?

A

Number of arguments passed into function must be equal to number of arguments specified in function definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the two types of argument setups. Give example

A

Positional arguments
Correct position is crucial
~~~
add_num(1, 5, 6)
~~~

Named arguments
Order of arguments not important, naming is important
add_num(b=4, c=100, a=1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are default arguments?

A
  • You can specify a default value for an argument in a function definition, and if that argument is not passed in during the function call, the argument will assume the default value.
  • If the argument is passed in during the function call, the passed in value overrides the default value.
  • You specify a default value for an argument by setting it equal to a value in the function definition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define a default argument

A
def add_fun(a, b=5, c=3)
    sum = (a + b + c)
    print(sum)
    
add_fun(1)

Result:
9
16
Q

Which important rule is there with default arguments?

A

All default arguments have to be on the right-hand side of the function’s argument list

17
Q

What are return values?

A
  • Returns values form function

A return statement simply uses the keyword “return” followed by a value or set of values that you want to return to the program at the point you called the function.

One thing to note: the return statement will exit your function, so make sure you do not have code that you want to run placed inside your function after your return statement—that code will never be executed!

18
Q

If a function returns multiple values which data type will be returned? Give example

A
def math(a, b)
    add = a + b
    sub = a - b
    return add, sub
print(math(6, 2)

Result
(8, 4)