Curso Python. Sintaxis Básica IV Funciones II. Vídeo 6
Introduction to Function Parameters in Python
Overview of Functions and Parameters
- The video introduces the topic of functions in Python, specifically focusing on passing parameters or arguments to functions.
- The presenter opens a previously saved Python file using an IDE configured for .py files, demonstrating how to access and open files on different operating systems.
Creating a Simple Function
- A new function named
sumais declared, which will sum two values without any parameters initially.
- The presenter explains how to declare variables within the function and includes print statements to display the sum of these variables.
Reusability of Functions
- Emphasizes that the main utility of functions is code reusability; the same function can be called multiple times with different inputs.
- Discusses how calling the function multiple times with fixed values (5 and 7) demonstrates its reusability but lacks flexibility.
Understanding Parameters and Arguments
Defining Parameters
- To allow for different inputs during each call, parameters or arguments are introduced in the function definition.
- The presenter explains that parameters are defined within parentheses when declaring a function, allowing it to accept various inputs.
Importance of Passing Arguments
- It’s crucial that if a function expects parameters, those must be provided during each call; otherwise, it will result in an error.
Understanding Function Parameters and Return Values in Programming
Function Parameter Storage and Execution Flow
- The execution flow of a function begins when it is called with parameters. The first parameter is stored in its designated location, while the second parameter is stored accordingly.
- For example, if the first parameter is set to 5 and the second to 7, the interpreter executes the function's code using these values, resulting in an output of 12 when adding them together.
- It's crucial to note that parameters are stored in the order they are provided. In Python, this behavior is default; however, other programming languages may allow modifications.
- If a function has multiple parameters (e.g., 20), each value passed during a call will be assigned to its corresponding parameter based on their position.
- This allows for reusability of functions by passing different arguments each time they are called, demonstrating how functions can perform tasks repeatedly.
Types of Function Parameters
- Function parameters can vary in type. In this case, two integer parameters were used, but functions can also accept strings, booleans, or floating-point numbers among others.
- Functions may optionally return values through a return statement. So far, no such functions have been created yet; only basic functions without return instructions have been discussed.
Analogies for Understanding Functions
- A useful analogy compares functions with return statements to vending machines: just as a machine dispenses products based on input codes, a function returns values based on input parameters.
- When declaring a function (e.g.,
sum(a1,a2)), you can use a return statement within it to provide results back to the caller—similar to how entering a product code yields an item from a vending machine.
Practical Implementation of Functions
- To optimize code further within a function like
sum, one could declare an internal variable (e.g.,result = a1 + a2) and then usereturn resultfor clarity and efficiency.
- Just as different codes yield different products from vending machines (e.g., chips vs. chocolate), varying input parameters lead to different outputs from functions depending on what was entered.
Output Visibility and Printing Results
- After executing code that includes return statements without print instructions, no visible output appears even though values are being processed internally by the program.
Understanding Function Returns in Python
The Basics of Function Return Values
- The function
sumais demonstrated, highlighting the importance of usingreturnto output values. Initially, only one result (12) is printed because other calls are not specified for printing.
- Acknowledgment that beginners may find this concept confusing and seemingly useless; however, it has significant utility in programming.
Storing Function Results
- Emphasizes the ability to store a function's return value in a variable, which enhances its potential use within a program.
- An example is provided where a variable named
almacena_resultadostores the result of callingsuma(5, 8), demonstrating how functions can yield results that can be reused later.
Importance of Printing Returned Values
- After storing the result in a variable, it's noted that nothing will display unless explicitly instructed to print the variable's content.
- When executed with print statements added, it successfully returns 13 (the sum of 5 and 8), illustrating how stored results can be utilized throughout complex codebases.
Understanding Parameter Passing in Python
- Discussion on whether Python passes parameters by reference or by value; reassures viewers that this detail isn't crucial at their current learning stage but hints at future discussions on parameter passing concepts.
- Clarifies that Python always passes values by reference and indicates that this topic will be explored more deeply in subsequent videos.
Conclusion and Next Steps