We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20
Understanding Procedures and Functions in Programming
Introduction to Procedures and Functions
● Procedures and functions are essential parts of a program.
● They help organize code into reusable pieces. ● Reusable code can be called multiple times as needed. What is a Procedure? ● A procedure performs a specific action. ● It does not return a value. ● Example: A procedure that prints a message. What is a Function? ● A function performs a specific task and returns a value. ● Functions can take inputs and provide outputs. ● Example: A function that adds two numbers and returns the result. Defining Procedures ● Syntax for defining a procedure: ● Procedure name ● Statements to execute ● End procedure ● Example: ● procedure PrintMessage ● "Hello, World!" ● end procedure Defining Functions ● Syntax for defining a function: ● Function name ● Return type ● Parameters (if any) ● Statements to execute ● Return value Calling Procedures ● To call a procedure, simply use its name followed by parentheses. ● Example: PrintMessage() ● Procedures can be called from anywhere in the program. Calling Functions ● Functions are called similarly to procedures. ● Example: result = AddNumbers(5, 10) ● The returned value can be stored in a variable. Parameters and Arguments ● Parameters are variables in the procedure or function definition. ● Arguments are the actual values passed to the procedure or function. ● Example: In AddNumbers(a, b), a and b are parameters. Passing Parameters ● Parameters can be passed by value or by reference. ● By value: A copy of the value is passed. ● By reference: The memory address is passed, allowing changes to affect the original variable. Return Values ● Functions return values using the return keyword. ● The return statement is typically the last statement in the function. ● Example: return sum Difference Between Procedures and Functions
● Procedures do not return values; they perform actions.
● Functions return values and can be part of expressions. ● Understanding this difference is crucial for effective programming. Example of a Procedure ● Example: A procedure that prints a line of dashes. ● It takes a parameter for the number of dashes to print. ● Call it with: PrintLine(60) Example of a Function ● Example: A function that calculates the sum of squares. ● Takes two parameters and returns the sum of their squares. ● Call it with: result = SumOfSquares(3, 4) Using Functions in Expressions ● Functions can be used in expressions. ● Example: If SumOfSquares(3, 4) > 20, then do something. ● This allows for dynamic decision-making in programs. Common Mistakes ● Forgetting to use parentheses when calling procedures or functions. ● Confusing parameters with arguments. ● Not handling return values properly. Best Practices ● Keep procedures and functions focused on a single task. ● Use meaningful names for procedures and functions. ● Document your code to explain what each procedure or function does. Engaging Questions ● Can you think of a real-world example of a procedure? ● How would you use a function to solve a math problem? ● Why is it important to understand the difference between procedures and functions? Conclusion ● Procedures and functions are vital for organizing code. ● They enhance code reusability and clarity. ● Mastering these concepts is essential for effective programming. Further Learning ● Explore more about programming languages and their syntax. ● Practice writing your own procedures and functions. ● Engage in coding challenges to strengthen your understanding.