Commit 69b24a40 authored by Swicech's avatar Swicech
Browse files

Assignment 5 upload

parent f667932b
%% Cell type:markdown id: tags:
[Back to Assignment 5](_Oving5.ipynb)
# The Basics of Functions
**Learning goals:**
- Functions
**Starting Out with Python:**
- Ch. 5.1-5.2
- Ch. 5.4
- Ch. 5.8
%% Cell type:markdown id: tags:
### The Basics of Functions
%% Cell type:markdown id: tags:
Functions are used in Python to split code into smaller blocks. A function often contains code which executes a specific task, and it is good practice to group the majority of your code in functions. This makes your code more understandable, and you avoid writing the same code multiple times. You can also reuse functions written in other .py-files, and use the functions of this files through modules.
Functions are written in the general form shown below. You can change function_name and argument1 and argument2, so that they fit your needs. You can of course have more arguments, or even no arguments. Functions usually returns a value in the "return"-expression, but this is not mandatory if you wish to print text or edit "global variables". In Python, the indentations define what belongs to which part of the program. Functions are defined with the key word "def", and everything that follows the indentation of the next lines belong to the function.
General form of a function:
```python
def function_name(argument1,argument2):
#The code written here is a part of the function "function_name" (remember correct indentation!)
sum = argument1 + argument2 #Adds two numbers
return sum #Returns the answer
answer = function_name(1,2) #The result of the function is stored in the variable "answer"
print(answer) #The answer is printed to the user
```
%% Cell type:markdown id: tags:
## a)
%% Cell type:markdown id: tags:
In this subtask you shall write a function which takes in two integers x and y, and returns their product.
%% Cell type:code id: tags:
``` python
# Write code for a) here:
```
%% Cell type:markdown id: tags:
#### Procedure:
%% Cell type:markdown id: tags:
- Write def followed by a function-name and (x,y).
- Example.: def multiply(x, y):
- (Remember colon after (x,y)!)
- Create a variable and assign it to x*y. Example: value = x*y.
- Return the variable "value" by writing: return value.
- The function is now complete. To print the result, write:
- print(multiply(5,5))
- This must be written outside the function.
%% Cell type:markdown id: tags:
## b)
%% Cell type:markdown id: tags:
In this subtask, you will make a function that receives a number, and prints that it is a nice number.
Example:
```
#The argument is 53
53 is a nice number
```
%% Cell type:code id: tags:
``` python
# Write code for b) here:
```
%% Cell type:markdown id: tags:
#### Procedure:
%% Cell type:markdown id: tags:
- Define the function, give it a name, and let it receive one argument. (Se the example of the general form of a function under "Basics of functions").
- Use print(...) to write to screen.
- This variable doesn't return anything, and is therefore complete the way it is written now. To check that it work, write:
- function_name(53)
- When you execute this code, you will get the same output as in "Example of function execution".
%% Cell type:markdown id: tags:
## The Main() Function
%% Cell type:markdown id: tags:
It is normal for a program to have a main-function that is executed when the program starts. This is the function that calls the other functions of the program when needed/specified. An example of a program with a main-function is shown below:
%% Cell type:code id: tags:
``` python
def main():
print("I can add 5 with 5")
num = add(5,5)
print("5+5 =",num)
def add(x,y):
return x+y
main()
```
%% Output
I can add 5 with 5
5+5 = 10
%% Cell type:markdown id: tags:
## c)
%% Cell type:markdown id: tags:
In this subtask you will make a main()-function which uses the functions you wrote in subtasks a) and b). The main-function shall receive two numbers form the user, and use the function in a) with these as arguments. Further on, function b) will be called on the value returned by function a)
Example run:
```
Give me a number: 5
Give me another number: 4
20 is a nice number
```
%% Cell type:code id: tags:
``` python
# write your code for c) here:
```
%% Cell type:markdown id: tags:
#### procedure:
%% Cell type:markdown id: tags:
- Define the function and call it "main". The function must NOT receive any arguments.
- Make two int-variables which both receives input from a user:
- vari1 = int(input(…))
- vari2 = int(input(…))
- Make a variable which is assigned the return value of function a):
- variable = function_name_a(vari1,vari2)
- Call the function from b) with the variable you used made in point 3 as argument:
- function_name_b(variable)
- Now the main-function is finished. To run the program you just write main() outside the function-block. (Se the last line in "The main-function")
This diff is collapsed.