
Understanding Functions in C Programming
Explore the world of functions in C programming, from declarations to definitions, and learn about their role in creating modular and efficient code. Discover how functions handle arguments, return values, and local variables, and why function declaration is essential for effective program structure.
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Chapter 14 Functions
C Functions zero or multiple arguments passed in single result returned (optional) return value is always a particular type In other languages, called procedures, subroutines, ...
Functions in C Declaration: introduces one or more identifiers into the program and specifies their meaning and properties int Factorial(int n); type of return value name of function (identifier) types of all arguments Function call -- used in expression a = x + Factorial(f + g); 1. evaluate arguments 2. execute function 3. use return value in expression https://en.cppreference.com/w/c/language/declarations
Function Definition State type, name, types of arguments Declaration must match the Definition Provides a name for each argument Name doesn't have to match name in declaration (if present) Function declarations only need the argument type Contains the body of the function int Factorial(int n) { int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return result; }
Why Declaration? Since function definition also includes return and argument types, why is declaration needed? May not be needed for simple programs Declare and define at the same time Function may be seen by compiler before definition Compiler needs to know return and arg types and number of arguments. Definition might be in a different file, written by a different programmer. Don t have to care how the function works, just that is exists Include a "header" file with function declarations only Compile separately, link together to make executable
Example double ValueInDollars(double amount, double rate); declaration (prototype) main() { ... dollars = ValueInDollars(francs, DOLLARS_PER_FRANC); printf("%f francs equals %f dollars.\n", francs, dollars); ... } definition (function code) function call (invocation) double ValueInDollars(double amount, double rate) { return amount * rate; }
Storing local variables for a function For each function call A stack-frame ( activation record ) Is inserted ( pushed ) in the run-time stack It holds local variables, arguments values returned If the function is recursive, for each iteration inserts a stack-frame. When a function returns, the corresponding stack-frame is removed ( popped ) When a function returns, its local variables are no longer valid.