Instructions: Answer the questions below. Press [Shift + Enter] after each question to answer.
4 Discussion: In your own words, describe the two (or three) motivations for using functions in your programs.
=>
What is a function in programming?
- A function is an ordered and reusable code block that is used to carry out a single and connected operation. A Function provides your programme with greater modularity and a high degree of reuse of code. Some examples of function include print() and main(), they are called built-in functions which are contained within the language, but we can also write our own functions.
How does a function return a value?
- A return statement stops the function call execution and returns the value of expression followed by the "return" keyword. If the return statement is not followed by any expression then it returns None.
a. What is the purpose of parameters?
- A parameter's purpose is to pass value to a function. The function then uses it as it's the local variable inside it.
b. What is the difference between a formal parameter and an actual parameter(argument)?
- The key distinction between Actual Parameters and Formal Parameters is that when the function is invoked, Actual Parameters are the values transferred to the function, while Formal Parameters are the variables specified by the function that, when the function is called, receives values.
c. In what ways are parameters similar to and different from ordinary variables?
- Parameters and ordinary variables are both used in the same scope in a function they just differ in the sense of initialization. While ordinary variables are initialized at the start of function whereas parameters are passed in a function during call loaded with value.
4. Reasons for using function in your code:
-They make the code more modular.
- Each function has specific meaning so it makes code efficient by reducing repititiveness.
- They make code easy to test.
Get Answers For Free
Most questions answered within 1 hours.