CP264 Lecture 4 Function PDF
Document Details
![HaleNeodymium](https://quizgecko.com/images/avatars/avatar-10.webp)
Uploaded by HaleNeodymium
Wilfrid Laurier University
Tags
Summary
This document covers the C functions, libraries and preprocessors. Topics include function fundamentals, memory management, and function call stacks. Additional topics include recursive functions, and memory considerations. It offers helpful insights for computer science students.
Full Transcript
Lecture 4 C functions 1. Functions 2. C Library 3. Preprocessors 1. Functions A function (also called procedure, routine, subroutine) is a block of code which can be called (executed) with input parameters. Why functions? 1. Reuse of code. It is better to use functions for those frequently...
Lecture 4 C functions 1. Functions 2. C Library 3. Preprocessors 1. Functions A function (also called procedure, routine, subroutine) is a block of code which can be called (executed) with input parameters. Why functions? 1. Reuse of code. It is better to use functions for those frequently used blocks of code. 2. Enable modularized and structured design of programs, for effective development, understanding, maintain. 3. Enable to organize a program into separate files, to develop, compile, test separately. 4. Enable the library, making use previously developed functions. A library consists of a collection of functions. Function is the foundation of the so-called procedural programming. Memory management of program execution memory space An executable program is loaded runtime into OS assigned memory space for function local stack region execution. variables call stack The memory space is divided into four regions (segments) for the runtime heap region execution of the program. dynamically allocated – Text region (stack, system stack): hold global, static, data region functions. variables – Data region: hold global, static variable data program Text region – Stack region: hold runtime function functions local variable data – Heap region: hold runtime dynamically allocated memory for OS Kernel data storage Function call stack 1. Function call stall (or call stack, system stack) is the stack region used to hold function argument variables, local variables, and additional information at runtime, operated like stack. 2. When a function is called, the argument variables and local variables of the functions are instanced in the call stack. – The current top address of the call stack is set to the base address the function call. The relative address of a variable assigned by compiler is the offset from the base address. The base address plus the relative address is the absolute address of the variable. 3. When the function call is done, the top address of the call stack is reset to starting address of the function call. – This causes pop off (shrinking) of call stack. It’s like local variables of the function are popped off (released) from the call stack. The memory space in the call stack will be reused by follow up function calls. 4. If function f1 calls function f2, the local variables of f2 are pushed to call stack on top f1’s memory space in call stack. – This causes the growing of call stack. Therefore, deeper function calls, i.e f1 calls f2, f2 calls f3, …, so that f1 and f2 and f3 are in call stack when f3 is running. This leads to more memory usage of call stack at runtime. Memory usage of recursive function Recursive function : call the same function inside the function. Example: compute factorial n! // n! = 1*2*3*…*n // 1! = 1, n ! = n* (n-1)! // using iterative algorithm // recursive algorithm/function int factorial( int n) { int factorial( int n) { int f = 1, i; if (n