Functions and Modular Programming PDF

Document Details

WarmheartedComputerArt

Uploaded by WarmheartedComputerArt

Alforque, Adrianne, Bucol, Jason Charls, Hermosilla, Sonnia Beth, Yamson, Lord Justin

Tags

C programming functions modular programming computer science

Summary

This document is a guide to functions and modular programming in C. It covers topics including defining functions, calling functions, parameter passing, return types, and scope of variables. The document provides practical examples and explanations of each concept.

Full Transcript

      FUNCTIONS ANDMODULAR PROGRAMMING OV 1 DEFINING AND E CALLING FUNCTIONS R 2 PARAMETER PASSING VIE 3 4 RETURN VALUE...

      FUNCTIONS ANDMODULAR PROGRAMMING OV 1 DEFINING AND E CALLING FUNCTIONS R 2 PARAMETER PASSING VIE 3 4 RETURN VALUES SCOPE OF VARIABLES WHAT IS A C FUNCTION? Functions in C are reusable blocks of code designed to perform specific tasks. They allow you to write code once and use it multiple times without rewriting it. This specifies the These are inputs to type of value that the function, defined the function will within parentheses. Return return after its Parameters allow the function to receive Parameters Type execution. Common data from the calling code.Example: int (or Arguments) return types include int, void, float, char, add(int a, int b) where a and b are etc. parameters A unique identifier for the function. By The block of code Function convention, enclosed in curly Name function names braces {} that Function should describe defines what the Body what the function function does. does. Key Components of a Function 1 DEFINING FUNCTION A function definition specifies the code that will execute when the function is called. It includes the return type, function name, and parameters (if any). SYNTAX: Example of Defining Function 1 CALLING FUNCTION This is where the function is executed in your program. When a function is called, the execution control is transferred to the function. You can call it by using its name and passing any required arguments To call a function, write the function's name followed by two parentheses () and a semicolon ; SYNTAX: Example of Calling Function Function Prototypes (Declaring) A function prototype declares the function's name, return type, and parameters before the function is defined. You need to declare the function at the top of your program or in a header file This allows functions to be called before their actual definitions. Example of Function Prototype Example of Function Prototype PARAMETER PASSING 2 Parameter passing is the method by which arguments (values or variables) are given to functions in programming, allowing those functions to use and manipulate data provided by the caller. When a function is called, the values or variables sent to it are called parameters or arguments. Types of Parameter Passing Pass by Reference Pass by Value (Using Pointers) The function receives a Instead of a copy, the copy of the variable’s value. function receives the Any changes made to the address of the variable. parameter inside the The function can then function don’t affect the access and modify the original variable in the original variable directly. calling code. This method requires This is the default way pointers and is commonly parameters are passed in C. used when you want the function to change the original data (e.g., arrays). SYNTAX Pass by Value Pass by Reference (via Pointers) EXAMPLE Pass by Value Output EXAMPLE Pass by Reference (via Pointers) Output A return value is the value a function provides back to the part of the program that called it. The return value allows a function to communicate a result or status to its caller, and it plays a key role in controlling program flow. The return statement in a function provides a way to send a value back to the caller. RETURN VALUES 3 Basic int: The most common return type, especially Return for the main function. Returning 0 from main Types typically indicates successful program execution, while a non-zero value usually signals an error. void: A return type that indicates the function does not return a value. Such functions perform actions but do not provide a result. EXAMPLE OF A FUNCTION THAT RETURNS A VALUE: EXAMPLE OF A FUNCTION WITH NO RETURN VALUE: 4 SCOPE OF VARIABLES The scope of a variable determines where it can be accessed and used within the program. There are several types of scope in C, which define the visibility and lifetime of a variable. Variables declared inside a function or within a {} block (such as within a loop or an if statement) have block scope. Accessibility: Only accessible within the block where they are declared. Lifetime: They are created when the block is entered and destroyed when the block is exited. Block Scope (Local Scope) Applies specifically to labels (for use with goto statements) within a function. Accessibility: A label can be used only within the function where it’s defined. Lifetime: Exists only within the function in which it’s defined. Function Scope Variables declared outside of any function, usually at the top of a C file, have file scope. Accessibility: Accessible from any function within the same file. Lifetime: These variables are created when the program starts and destroyed when the program ends. File Scope (Global Scope) Parameters of a function have a local scope within the function. Accessibility: Only accessible within the Lifetime: Created when function they are the function is called and declared in, as local destroyed when the variables. function exits. Function Parameter Scope Variables declared with the extern keyword can have program scope, allowing them to be used across multiple files. Accessibility: Accessible in any file where they are declared as extern. Lifetime: Created when the program starts and destroyed when the program ends. Program Scope (External Variables)

Use Quizgecko on...
Browser
Browser