Click here to Skip to main content
15,509,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
what is function in python and how to use program function in python
thank you

What I have tried:

learn, but I don't really understand. can someone help me
Posted
Updated 14-Nov-22 5:48am

If you are new to a language then you will learn it much more easily by following a proper tutorial: The Python Tutorial — Python 3.7.9 documentation[^].
 
Share this answer
 
In a plain words imagine a function like a box...In this box some arithmetic operations are done...Once these operations are done then the box produces some effect (output or return). This will usually be an integer or float or string or boolean. In some cases we input in this box some variables (parameters). These parameters get inside of the function (box) and they used for calculation (Function schematic). Study first the meaning of the function in mathematics. See the link Functions.
 
Share this answer
 
Python is an object-oriented, general-purpose programming language. Python is a high-level programming language that generally emphasizes code readability with the help of proper indentation. The foremost advantage of using Python is that it is a dynamic programming language. Python syntax is considered relatively easy to understand. It is almost similar to human language, and Python codes are generally smaller in size and have less number of lines. Python has some really powerful libraries for machine learning. Pytorch and Tensorflow are the most extensively used libraries for machine learning to date. Python is sometimes also referred to as the language of the Future. 

Now that we know a little about what Python is and where it is generally used, let us answer the main question. What are functions in Python?

Basically, the function is a block of code that only runs when it is called using the function call. A function may or may not have a return type. In simpler words, the function might return some value on execution to be used for later purposes. Or the function might simply display something when executed. A function that does not return a value is called a void function. We can also provide the function with additional data that may be needed by the function. This additional data is provided to the function using parameters.

Functions in Python can be created using the def keyword. So the most basic function would look like this,
def helloWorld():
	print("Hello World!!")

Notice how I have added a block of space before the print keyword. This block of space is called indentation, and it is used to define the block of code that is present inside the function. For instance, if multiple lines were present inside the function, each line would have the same indentation.
def helloWorld():
	print("Hello World!!")
	print("First Line")
	print("Second Line")

After creating the function, we can call it using the function call. The function call for the above-created function would look like this,
helloWorld()

We can also pass parameters in the code for better functionality. For instance, if we had to add two numbers using a function, the code would look something like this,
def printNum(first, second):
	return first+second

printNum(10, 20)

Notice how I have used the return keyword to return a value. The previous function did not use the return keyword and was a void function. At the same time, this function returns an integer value.

I hope this answer addresses your queries. I have added links for web pages that I found relevant when I was starting my journey of coding. I hope they help you as much as they have helped me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900