What are functions, and how are they used


I am a beginner and need to know what a function for a small tic tac toe project. But all I need to know is this as understood the rest. is and how to use one as well as what can they do

I was just learning from the python docs about a function and could not understand, as I was searching on google still, I could not find anything, if one could help, it would help me loads.

This is the code for my tic tac toe that the YouTuber gave as an example:

def cool(a,b):




Asked By: gops



I self-learned python and would look at the W3Schools stuff which is quite good.

A function is, in effect, a way of running a section of code with a keyword, if you had

def cool(a,b)

cool(1, 3)

You would have the function running the code you have put inside it and so printing the value of a+b (4) using the values a and b which were passed into the function.

The code you posted would not work because to run the function, a and b need to be passed, which is not done in the question.

Answered By: Superted441

In Python, a function is a block of instructions or code that is used to perform an action. Functions provide an organized way to structure code, making it easier on the human eye. It seems as if you are tired to add a and b, however, a and b are not given values in the return statement.

Answered By: jphotis

Welcome to the world of programming.

As a beginner, you should imagine functions as "reusable" part of your code, with the feature of having temporal variable that can change the behavior of the function based on the body of the function.

Calling the functions

To reuse that part of your code that you wrote in your function, you have to call it.

def python_cool():
    print("Python is cool")


Technically, this code is the same as:

print("Python is cool")
print("Python is cool")
print("Python is cool")

At this point, if you we are talking about parameters, we have to talk about arguments too.


Parameters are the one that you are declaring your function with:

def sample_function( paramter1, parameter2, paramterN ): 


Arguments on the other hand, are the values when you are calling the function with.

sample_function(argument1, argument2, argumenN)


def print_values( value_1, value_2) : 
    print("The first value is: ", value_1)
    print("The second value is: ", value_2) 

print_values(1, 2)
print_value(True, False) 
print_values("apple", "peach")

Return values

Another great benefits of functions that they are able to "become" a value that you can store in a variable in the future

For example:

def multiply_by_three( number ) :
    return number * 3 

This function has a return value of an integer which can be used as:

sample_variable = multiply_by_three(5) 

which is technically:

sample_variable  = 15
Answered By: AdriaNn01
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.