Skip to content

Functions

Mostafa Vahidi edited this page Jan 9, 2018 · 5 revisions

Overview

So far, we've been coding python in small fragments. This can be useful for very small programs or minor tasks, however, we can quickly realize that this has some limitations.

For larger programs, we want to increase the efficiency of our program by allowing parts of our code to be reusable. This can be done by the usage of Functions

Function : A named piece of code, separate from all others.

A Function can take any number and type of input parameters and return any number and type of output results.

You can do two things with functions:

  • Define it
  • Call it

New Concepts

def Statement and Parameters

Here is how you define a function in python:

def test(): The def signals the computer that you're defining a function.

The following word is the name of the function "test". Followed by parentheses "()". Then a colon ":".

Whatever follows the ":" would be the function body or what is executed whenever you call the function.

Here is an intuitive example:

def uglyShoes():

 print('what are those???')

In this case, the print('what are those???') will be executed if you call the function as follows:

uglyShoes()

return Statement

None value

Local and Global Scope

global Statement

Exception Handling

Activity

Binary Search

Summary

Practice Problems

Clone this wiki locally