Docsity

User defined functions, Summaries of Computer science

Toggle sidebar
bg1
1
UNIT V
Functions & File Handling
Introduction to Functions, Function Declaration and Definition, Function call Return Types and
Arguments, modifying parameters inside functions using pointers, arrays as parameters,
Recursive function, Storage Classes, Scope and Lifetime of Variables, Memory Management,
Basics of File Handling.
Introduction to Functions
In C, this concept is implemented using functions. Functions are used to divide a larger program
into smaller subprograms such that the program becomes easy to understand and easy to
implement. A function is defined as follows...
Function is a subpart of a program used to perform a specific task and is executed
individually.
Every C program must contain at least one function called main(). However, a program may also
contain other functions.
Every function in C has the following...
• Function Declaration (Function Prototype)
• Function Definition
• Function Call
Function Declaration
The function declaration tells the compiler about function name, the data type of the return value
and parameters. The function declaration is also called a function prototype. The function
declaration is performed before the main function or inside the main function or any other
function.
Function declaration syntax -
returnType functionName(parametersList);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download User defined functions and more Summaries Computer science in PDF only on Docsity!

Functions & File Handling Introduction to Functions, Function Declaration and Definition, Function call Return Types and Arguments, modifying parameters inside functions using pointers, arrays as parameters, Recursive function, Storage Classes, Scope and Lifetime of Variables, Memo Basics of File Handling. Introduction to Functions^ UNIT^ V ry Management,

In C, this concept is implemented using functions. Functions are used to divide a larger program into smaller subprograms such that the program becomes easy to understand and easy to implement. A function is defined as follows... Function is a subpart of a program used to perform a specific task and is executed individually. Every C program must contain at least one function called main(). However, a program may also contain other functions.

Every function in C has the following... Function Declaration The function declaration tells the compiler about function name, the data type of the return value • • • Function Declaration (Function Prototype)Function DefinitionFunction Call

and parameters. The function declaration is also called a function prototype. The function declaration is performed before the main function or inside the ma function. Function declaration syntax returnType functionName(parametersList); - in function or any other

In the above syntax, from the function definition. The The Function Definition The function definition is also known as the is implemented in the function definition. function or after the main function. parametersList (^) returnType is the data values that are sent to the function definition. specifies the data type of the value which is sent as a return value functionName The function definition is performed before the main body of the function is a user-defined name used. The actual task of the function in the program. Function definition syntax returnType functionName(parametersList) { Actual code... } Function Call - When a function call is executed, the execution control jumps to the function definition where the actual code gets executed and returns to the same functions call once the execution completes. The function call is performed inside the main function or any itself. Function call syntax ADVANTAGES OF FUNCTIONS - functionName(parameters); other function or inside the function

  • • • • • UsingFunctions make the program more readable and understandable.Using functions the program implementation becomes easy.Once a function is created it can be used many times (Using functions larger programs can be divided into smaller modules. functions we can implement modular programming. code re-usability ).

Example Program #include #include void main(){ int num1, num2, result int addition(int,int) clrscr() printf("Enter any two integer numbers; ; // function declaration ; :"); } int addition(int a, int b) {^ scanf("%d%d", &num1, &num2);^ printf("SUM = %d", result);^ getch() return a+b^ result =; ;^ addition(num1, num2) // function definition; // function call In the concept of functions, the function call is known as definition is known as When we make a function call, the execution control jumps from calling function to called function. After executing the called function, the execution control comes back to calling function from called function. When the control jumps from calling function may carry one or more data values called " single value called " Based on the data flow between the calling function and called function, the functions are^ } return value "Called Function" ".. Parameters " and while coming back it may carry a "Calling Function" (^) to called function itand the function classified as follows... • • • • Function without Parameters and without Return valueFunction with Parameters and without Return valueFunction without Parameters and with Return valueFunction with Parameters and with Return value

Function without Parameters and without Return value In this type of functions there is no data transfer between calling function and called function. Simply the execution control jumps from calling function, and finally comes back to the calling function. Example Program #include #include void addition(); // function declaration - function to called function and executes called void main(){ } void addition() { clrscr() addition() getch() int num1, num2 ;; ; // function call // function definition; Function with Parameters and without Return value In this type of functions there is data transfer from calling (parameters) but there is no data transfer from called function to calling The execution control jumps from calling^ }^ printf("Enter any two integer numbers^ scanf("%d^ printf("Sum = %d", num1+num2^ %d", &num1, &num2); - function to called functi ); : ") - function to called function ; on along with the parameters-function (return value). and executes called function, and finally comes back to the calling function. Example Program #include #include void addition(int, int) void main(){ int num1, num2; ; // function declaration

Function with Parameters and with Return value In this type of functions there is data transfer from calling (parameters) and also from called function to calling control jumps from calling function, and finally comes back to the calling function along with a return value. Example Program^ }^ return (num1+num2) - function to called function along with paramet ; - function (return value). The execution-function to calleders and executes called-function #include #include int addition(int, int) void main(){ int num1, num2, result clrscr() printf("Enter any two integer numbers : ") scanf("%d; %d", &num1, &num2); ; // function declaration ; ; } int addition(int a, int b) { }^ result =^ printf("Sum = %d", result)^ getch() return (a+b) ;^ addition(num1, num2); // function definition ; ; // function call Parameter Passing in C When the execution control is transferred from calling or number of data values. These data values are called as Parameters are the data values that are passed from calling function to called function. - function to called parameters. -function it may carry one

In C, there are two types of parameters and they are as follows... The parameters executed, the copy of actual parameter values • • actual parametersActual ParametersFormal Parameters are the parameters that are declared at called function. When a function gets are the parameters that are is copied into formal parameters. specified in calling function. The formal In C Programming Language, there are two methods to pass parameters from calling function to called function and they are as follows... Call by Value In call by value • • Call by ValueCall by Reference parameter passing method, the copy of actual parameter values is copied to formal parameters and these formal parameters are used in called function. on the formal parameters does not the execution control comes back to the calling function, the actual parameter values For Example Program example, #include #include void swap(int,int) consider the following program... ; // function declaration affect the values of actual parameters The changes made. That means, after remain same. void main(){ int num1, num2 clrscr() num1 = 10 num2 = 20 printf(" swap(num1, num2) ;;;nBefore swap: num1 = %d, num2 = %d", num1, num2); ; // calling function ;

Example Program #include #include *void swap(int , void main(){ int num1, num2 clrscr() num1 = 10;; *int ) ; ; // function declaration } **void swap(int a, int b) {^ num2 = 20^ printf("^ swap(&num1, &num2)^ printf("^ getch();\;nBefore swap: num1 = %d, num2 = %d", num1, num2)nAfter swap: num1 = %d, num2 = %d", num1, num2); ; // calling function // called function ; Output:^ }^ int temp^ *temp = a^ **a = b^ *b = temp;;;; In the above example program, the addresses of variables variables the values of actual parameters a and b. The changes made on the pointer variables num1 and num2 in calling function. num1 a and and num2 b in called function effects are copied to pointer

Array Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C. Pass array with call by value method #include void printArr(int arr[5], int n) { s as Parameters for (int i = 0; i < n; i++) printf("%d ", arr[i]);

Pass array with call by reference^ }^ int main() {^ } #include ^ int arr[] = {1, 2, 3, 4, 5};^ printArr(arr, 5);^ return 0;^ // Pass array to function void printArr(int* arr, int n) { } int main() { return 0; for (int i = 0; i < n; i++) int arr[] = {1, 2, 3, 4, 5}; printArr(arr, 5); printf("%d ", arr[i]); // Pass array to function Recursive Function In C programming language, function calls can be made from the main() function, other functions or from the same function itself. The recursive function is defined as follows...^ } A function called by itself is called recursive function.:

C Storage Classes In location In the C programming language, the memory of variables is allocated either in computer memory (RAM) or CPU Registers. The allocation of memory depends on storage classes. In C programming language, there are FOUR storage classes and they are as follows... C programming (whether RAM or REGISTER), language, storage scope classes, lifetime are and the used default value to define thingsof a variable. like storage

1. The default storage class of all local variables (variables declared inside block or function) is auto storage class. Variable of auto storage class has the following properties... 1.^ 2.^ 3.^ 4.auto storage class^ auto storage classextern storage classstatic storage classregister storage class Property Keyword Storage Default Value (^) Description autoComputer Memory (RAM)Garbage Value Example Program 1^ Scope^ Life time #include #include int main(){^ Local to the block in which the variable is definedTill the control remains within the block in which variable is defined int a=10; { } printf(" %d",a); return 0; int a=20; printf("%d",a);

Example Program 2^ int main(){^ } #include #include extern int i;^ printf("%d",i);^ return 0; //extern variable

3. The static variable allows to initialize only once and can be modified any number of times. Variable of static storage class has the following properties... Static storage class^ int main(){^ }^ printf("%d",i);^ return 0; Keyword Storage Default Value^ Property^ Description staticComputer Memory (RAM)Zero Example Program 1^ Scope^ Life time #include #include^ Local to the block in which the variable is definedThe value of the persists between different function calls (i.e.,^ Initialization is done only once)

Example Program 2^ static int a;^ int main(){^ } #include< #include^ printf("%d",a);^ return 0;stdio.h>

4. Register storage class^ static int i=10;^ int main(){^ }^ i=25;^ printf("%d",i);^ return 0;^ //Assignment statement The register storage class is used to specify the memory of the variable that has to be allocated in CPU Registers. The memory of the register variable is allocated in CPU Register but not in Computer memory (RAM). Keyword Storage Property Description registerCPU Register Variable of register storage class has the following properties... Default Value Scope Life time Garbage ValueLocal to the block in which the variable is definedTill the control remains within the block in which variable is defined

Scope and Lifetime of Variables In C, every variable has two important properties: They are related but not the same. 1. Scope of Variables 1. 2. ScopeLifetime – Where – How long in the program the variable can be accessed. the variable exists in memory. a) Block Scope (Local Variables) void } int printf • • • (^) func x =Declared inside a blockAccessible only inside that block.Most common type of scope.("%d" () 10 {; (^) , x);// block scope { } (functions, loops, if-statements). x b) Function Scope goto label: cannot be accessed outside printf • • (^) label;Only applies toNot typically used for ordinary variables.("Example"); labels (^) (used infunc(). goto statements). c) File Scope (Global Variables) int void } printf g = • • (^) fooDeclaredAccessible from their declaration point to the end of the file. (^5) (();"%d" (^) {// file scope, g); outside all functions.

d) Function Prototype Scope int 2. Lifetime of Variables a) Automatic Lifetime sum • • Applies to parameters in function prototypes.Scope is limited to the prototype itself.(int a, int b); // 'a' and 'b' only known here void } b) Static Lifetime // x is destroyed here int^ •^ •^ •^ func x =For local variables (withoutCreated when a function/block starts execution.Destroyed when the function/block ends. () 10 {; // automatic lifetime^ static). Applies to: Static Local Variable • • • • • Variables declared withGlobal variablesHas block scope but static lifetime.Created once, exists until program ends.Retains its value between function calls. static void } Calling 1 2 3 Global Variables static count++; printf counter counter() (^) (int"%d " count =() (^) , count);{ 3 times prints: 0 ; // survives between calls