![]() Here, we are returning the variable sum to the function call. If we want our function to return some value to a function call, we use the return statement. The code above is equivalent to add_numbers(5, 4)Ī Python function may or may not return a value. In Python, we call it Keyword Argument (or named argument). We can also call the function by mentioning the argument name as: add_numbers(num1 = 5, num2 = 4) In the above example, we have created a function named add_numbers() with arguments: num1 and num2. Here, add_numbers(5, 4) specifies that arguments num1 and num2 will get values 5 and 4 respectively.Įxample 1: Python Function Arguments # function with two arguments For example, # function call with two values If we create a function with arguments, we need to pass the corresponding values while calling them. For example, # function with two arguments An argument is a value that is accepted by a function. ![]() The control of the program jumps to the next statement after the function call.Īs mentioned earlier, a function can also have arguments.All codes inside the function are executed. ![]() When the function is called, the control of the program goes to the function definition.Here's how the program works: Working of Python Function ![]() In the above example, we have created a function named greet(). Here's how we can call the greet() function in Python. Now, to use this function, we need to call it. In the above example, we have declared a function named greet(). We will learn about arguments and return statements later in this tutorial. This function doesn't have any arguments and doesn't return any values. Here, we have created a function named greet(). return (optional) - returns value from a function.arguments - any value passed to function.function_name - any name given to the function.def - keyword used to declare a function.The syntax to declare a function is: def function_name(arguments): User-defined functions - We can create our own functions based on our requirements.Standard library functions - These are built-in functions in Python that are available to use. ![]() There are two types of function in Python programming: You can create two functions to solve this problem:ĭividing a complex problem into smaller chunks makes our program easy to understand and reuse. Suppose, you need to create a program to create a circle and color it. A function is a block of code that performs a specific task. ![]()
0 Comments
Leave a Reply. |