0% found this document useful (0 votes)
5 views

Functions Transcript

The document discusses defining a function in Python to add two numbers. It shows how to: 1) Define a function called add_two_numbers that takes in two arguments, x and y, and defines a variable total equal to x + y. 2) Have the function return the value of total after adding x and y. 3) Call the add_two_numbers function by passing in values of 1 and 2, and assign the return value to a variable called output. 4) Print the output variable to display the result of 3.

Uploaded by

bhuttoahmed2006
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Functions Transcript

The document discusses defining a function in Python to add two numbers. It shows how to: 1) Define a function called add_two_numbers that takes in two arguments, x and y, and defines a variable total equal to x + y. 2) Have the function return the value of total after adding x and y. 3) Call the add_two_numbers function by passing in values of 1 and 2, and assign the return value to a variable called output. 4) Print the output variable to display the result of 3.

Uploaded by

bhuttoahmed2006
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

As we saw in the last task,

python comes with inbuilt functions like the print function,


which is used to display information and the type function
which is used to display
the data type of a variable, but you can write your own
functions to do various tasks.
For example, say you want to write a function to add
two integers together.
How would we do that?
So let's get rid of the previous code that we have so control
A and then backspace to delete everything.
And in order to write a function, you need to use
the keyboard def.
This is short for define. After def, write the name
of the function.
In this case, we will call our function add
two underscore numbers. add_two_numbers
and then brackets.
So this bracket open and close brackets they need to follow
the name off the function. You will remember the arguments
or useful data that the function will act upon is supposed
to be sent in brackets.
So we just need to specify here that we expect arguments
and give those arguments some names that we can use
inside our function.
So let's say we are adding two numbers and we call the first
number x comma the second number y?
And at the end of this name after the brackets,
And you need to also put a colon. Press enter.
When you write a function, the part of the code which
is supposed to be inside, uh, inside the function needs
to be indented.
So your IDE,
in this case visual studio, will take care of this
automatically.
So you will see that once you press enter, after this line,
after the colon, it will automatically go to a one tab ahead
of the default indentation.
So your IDE automatically take care of it.
This basically means that you have followed the syntax
by using the colon correctly.
Okay, now, we're trying to add these two numbers so all we
really have to do inside this function is create
another variable total, and equate it to x plus y. Press enter
and see the indentation
is still maintained.
Now, a function in this case needs to return a value,
and it needs to return the value of total.
So all you have to do is use the return keyword followed
by value that you're trying to return.
That's pretty much it.
That's your function.
So we're defining a function that adds two numbers given
to the function.
The first number is represented by x, and the
2nd one is represented by y.
Then inside the function, we create a new variable total
and assign it to a value equal to x plus y. Then as the last
step in the function,
We returned the value of total. Notice after we added
the return statement,
You don't need to have a different indentation anymore.
So if you still see your cursor is intended, you need
to press backspace and go back to the original indentation.
Cool. So we have to find a function.
But if we run the script now, it won't do anything
because we haven't actually called this function.
It just it's defined so python knows,
ah, that it exists and python knows that which piece of code
is to be run if we used the function name anywhere
in the code.
Okay, so let's do that.
After the function definition, let's simply use the function
by writing the name of the function add_two_numbers
and you will see a helpful prompt from visual studio code
automatically pop up.
So it kind of understands that you're trying to call
this function and gives you the option to select it.
That's fine, let's use that.
So add two numbers, but we also need to pass the values for x
and y. Let's say one and two. And there we go,
we have successfully defined a function, and we've also called it.
Okay, so let's save this: control S, go back to our terminal
and again we will do the same thing.
We will basically run this file.
So python3 app.py. Now, you already have this written
out. So if you press just the up arrow, it's will show you
the last command that you entered and you don't have to type
it again.
Press enter and see what happens.
Well, nothing wrong happens.
There's no error, but it doesn't actually display the total
of these two numbers.
So what happened?
Well, the script actually runs successfully, but when using
the script.
Unlike with the python interpreter, we have to make use of the
print function to display information.
So let's edit our file and include a print statement to print
out the output of this. So we can simply do something
like declare another variable called output and assign it
to whatever value we get from this function.
And in the next line we can simply print output. Save the file, go back
to terminal. Again,
python3 app.py, and this time we can actually see
the output three printed out.
Great! Good stuff.
So congratulations on making this far.
You have learned a few basic concepts of programming,
with python.
But before we can start writing our little to do
this program, we will have to go through a few more concepts.
I will see you in the next task, and we will take a look
at lists and tuples.

You might also like