Iklan

Function Explanation in Python

 


Hi good coding best friend, good morning, noon, afternoon, or night. This time the admin will share material about the function in python language.

A function is a collection of statements used to perform specific tasks.

Function can break down programs into small sections that make us easy to organize and easy to manage, and can avoid looping and make code reusable.

There is an analogy that admins have read about a function. In the analogy, the function is a galah used to pick up mangoes found in trees.

We simply make or buy the one-time galah, but we can use it repeatedly.

The analogy is the same as the understanding of the function, which we can use without having to recreate the program to be used.

Then what does the function form in python?

If we look at java and c++ programming a function must be defined by data type or void, but if in pyhon enough with magic word named def.

def function_name(parameters):

 """docstring""" 

statement(s)

Explanation:

The main keyword in the function is def which marks the beginning of the function header.

function_name is the Name of the function that uniquely identifies it.

The naming function specifys the same authoring rule identifier with python. Parametrs must be given to avoid errors, or replace parameters with the initial self-word header function marked with a two (:).

Optional documentation string (docstring) to describe what function is done. One or more valid python statements that make up the body function.

The statement must have the same indentation level (usually 4 spaces). Optional return statement to return a value of functional (There is usually a return).

Examples like this:

def greet (*name):

    """this message inputs names and greetings"""

    print ("hello", name)

greet ("monika", "good morning")

The output is like this:


Types of functions in python

There are two types of functions in python:

1. Built-in functions are Functions built into Python, which is what python already has.

2. User-defined functions are Functions defined by the user itself, namely functions that are custom own by the user.

Python Function Arguments

In Python, you can specify a function that takes argument variable numbers.

Example:

def greet(name,msg):

 """this function can be parameter name and message delivered"""

print("Hy",name + ', ' + msg) 

greet("Angel","How are you today")

Here are the outputs:


Arbitrary Arguments

Python can allow us to handle this kind of situation through function calls with a number of random arguments.

In function definitions, we use an asterisk (*) before the name parameter to indicate this kind of argument.

def greet(*names):

 """this message sends a message to everyone on tuple pyhton"""

# names are paramters containing tuple: 

print("Welcome Back",names) 

greet("Nike","Luke","Steve","John")

the following are the output results:


sc:kodingbagus.blogspot.com

DONASI VIA OVO Bantu berikan donasi jika artikelnya dirasa bermanfaat. Donasi akan digunakan untuk memperpanjang domain www.inforudi.com. Terima kasih.

0 Response to "Function Explanation in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel