SIZ Education | Functions in Python | by @faizan2sheikh

in Steem Infinity Zone3 years ago

Hello steemians, I hope you all are having an amazing time learning, in today's post I am going to give you a brief overview of functions that are fundamental in python and are essential to implement different algorithms in python programming language.


Source

Functions

Let's begin with defining what the functions really are. You can think of functions as some block of code containing some specific set of pre-defined lines that perform certain operations on the supplied input and return the desired results.

Functions are "self contained" modules of code that accomplish a specific task.

Basic Components of a function

There are two basic components of a function:

  • Arguments:

Arguments are a set of input values or the raw data that is supplied to a function to extract and return the meaningful information of it.

  • Return Statement:

The return statement is a last line of code in any function that usually returns the output of the function.

Types of functions:

There are basically two types of functions in terms of definition:

  • Built-in functions
  • User-defined functions

Built-in functions

Built-in functions are those functions that are pre-defined in a programming language.

print('Hello world!')

Like in the above code we just used the print() function, we did not provide the inner implementation of how this print() function works, this function is pre-defined by the developers of the language, we just use these.

User-defined functions

User-defined functions are those functions that are defined explicitly by the user to be used in the code, these functions can be modified in any way possible, like changing the number of arguments or the inner implementation of the function.

Defining a function in python

Python programming language is very easy to use in terms of syntax. To define a function you have to do the following things:

def function_name(arguments):
       some boring code
       return result

def keyword is used before the function name to define the function. Arguments are supplied within the round braces. We can supply any number of arguments to the function.
To return something from the function we use return keyword before the thing.

Creating a sum function in python

Now we are creating a basic function which sums all the provided integer arguments and returns a result.

s1.png

We simply defined a sum function for two integers in just 2 lines, a and b are just variables that are used as placeholders for expected inputs, when inputs are supplied they are used instead of a and b.

Modifying the function for more arguments

In the last piece of code, we created a function for addition, but it had a drawback, it could only sum two integers. Don't worry we can solve this problem by using *args in our arguments, this *args sign indicates that arguments can be of any number, function can take any number of arguments.

s2.png

We used a loop to iterate through all the supplied input integers and added them one by one to our sum and in the end returned the sum value.

Conclusion

That's the complete overview of functions in python, I hope to receive positive reviews from the community. Apart from it, I highly appreciate any correction in the above-shared data. Hope you all enjoyed learning. Thanks for reading my post!
Regards, Faizan!

Thanks to:
@cryptokraze
@siz-official
@vvarishayy
@arie.steem
@suboohi

Sort:  
 3 years ago 

Please used #yourcountry tad in your post.
Thank you so much for your attention in our community.

Regards, Faran Nabeel

Ok I will use it next time, thank you for correcting.

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.030
BTC 58102.40
ETH 3065.94
USDT 1.00
SBD 2.26