SlideShare a Scribd company logo
Functions in C
Introduction to Functions
• Building blocks of modular programming
• Break complex problems into smaller, manageable pieces
• Promote code reuse and maintainability
Function Syntax
• Basic structure of a C function:
return_type function_name(parameter_list) {
// function body
return value;
}
Function Declaration vs Definition
• Declaration (prototype)
• Definition
int add(int x, int y);
int add(int x, int y) {
return x + y;
}
Function Parameters
• Parameters are variables used to pass data to functions
• Pass by value:
void modify(int x) {
x = x + 1; // Only modifies local copy
}
int main() {
int a = 5;
modify(a); // a remains 5
return 0;
}
Pass by Reference
• Using pointers to modify original values:
void modify(int *x) {
*x = *x + 1; // Modifies original value
}
int main() {
int a = 5;
modify(&a); // a becomes 6
return 0;
}
Global Variables
• Declared outside all functions
• Accessible throughout the program
int globalVar = 10; // Global variable
void function1() {
globalVar++; // Can access and modify
}
void function2() {
printf("%d", globalVar); // Can read
}
Local Variables
• Declared inside functions
• Scope limited to the containing block
void function() {
int localVar = 5; // Local variable
{
int blockVar = 10; // Block scope
}
// blockVar not accessible here
}
// localVar not accessible here
Static Local Variables
• Retain value between function calls
void counter() {
static int count = 0; // Initialized only once
count++;
printf("%dn", count);
}
Variable Scope Rules
• Scope hierarchy example
int global = 10;
void function() {
int local = 20;
{
int local = 30; // Hides outer local
printf("%dn", local); // Prints 30
}
printf("%dn", local); // Prints 20
}
Stack Frames
• Memory organization for function calls
• Each function call creates a new stack frame
• Contains:
• Local variables
• Parameters
• Return address
• Saved registers
void function(int x) {
int y = x + 1;
// Stack frame contains:
// - Parameter x
// - Local variable y
// - Return address
}
Function Call Stack
• Example of multiple function calls:
void function3() {
int z = 3;
}
void function2() {
int y = 2;
function3();
}
void function1() {
int x = 1;
function2();
}
Recursive Function Call
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// Each recursive call creates a new stack frame
Memory Layout
• Program memory organization:
• Text segment (code)
• Data segment (global variables)
• Stack (function calls)
• Heap (dynamic allocation)
Stack Overflow
• Example of dangerous recursion:
void infinite_recursion() {
int large_array[1000]; // Local array
infinite_recursion(); // Will cause stack overflow
}
Return Values
• Common return types: void, int, float, char, etc.
• Different ways to return data:
• Simple type
• Complex type: pointers
• Struct type
int return_value() {
return 42;
}
void return_pointer(int* result) {
*result = 42;
}
struct Point return_struct() {
struct Point p = {1, 2};
return p;
}
Function Pointers
• Store address of functions, can be used to call such functions
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int main() {
int (*operation)(int, int);
operation = add;
printf("%dn", operation(5, 3)); // Prints 8
operation = subtract;
printf("%dn", operation(5, 3)); // Prints 2
return 0;
}
Function Pointers
• Function pointers can be used as parameters:
int apply(int (*func)(int), int value) {
return func(value);
}
int square(int x) {
return x * x;
}
// Usage:
int result = apply(square, 5); // Returns 25
Functions with varied parameter list
#include <stdarg.h>
int sum(int count, ...) {
va_list args;
va_start(args, count);
int total = 0;
for (int i = 0; i < count; i++) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
Inline Functions
• Optimization hint to compiler:
inline int max(int a, int b) {
return (a > b) ? a : b;
}
Recursion
• Function calling itself:
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int factorial_tail(int n, int accumulator) {
if (n <= 1) return accumulator;
return factorial_tail(n - 1, n * accumulator);
}
Function Overloading
• C doesn't support overloading. Functions cannot have the same name
• Alternative approaches:
int add_int(int a, int b) {
return a + b;
}
float add_float(float a, float b) {
return a + b;
}
Function Documentation
• Using comments effectively:
• @brief Calculates the sum of two integers
• @param a First integer
• @param b Second integer
• @return Sum of a and b
/**
* @brief Calculates the sum of two integers
* @param a First integer
* @param b Second integer
* @return Sum of a and b
*/
int add(int a, int b) {
return a + b;
}
Error Handling
• Return codes and error checking:
int divide(int numerator, int denominator, int* result) {
if (denominator == 0) {
return -1; // Error code
}
*result = numerator / denominator;
return 0; // Success
}
Function Composition
• Combining multiple functions:
int process_data(int data) {
data = validate(data);
data = transform(data);
data = format(data);
return data;
}
Callback Functions
• Functions as parameters:
void process_array(int arr[], int size, void (*callback)(int)) {
for (int i = 0; i < size; i++) {
callback(arr[i]);
}
}
void print_item(int x) {
printf("%dn", x);
}
Header Files
• Function declarations in headers:
// math_functions.h
#ifndef MATH_FUNCTIONS_H
#define MATH_FUNCTIONS_H
int add(int a, int b);
int subtract(int a, int b);
#endif
Best Practices
• Function design guidelines:
• Single responsibility principle
• Clear naming conventions
• Consistent parameter ordering
• Proper error handling
• Documentation
• Parameter validation
• Resource cleanup
Summary
• Functions are fundamental to C programming
• Understanding scope and lifetime is crucial
• Stack frame management affects program behavior
• Proper function design leads to maintainable code

More Related Content

PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPT
16717 functions in C++
 
PPTX
Lecture 1_Functions in C.pptx
PPTX
Chapter 4
PPTX
PPTX
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
PPTX
CH.4FUNCTIONS IN C (1).pptx
PPTX
functions of C++
Chp8_C++_Functions_Part2_User-defined functions.pptx
16717 functions in C++
 
Lecture 1_Functions in C.pptx
Chapter 4
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
CH.4FUNCTIONS IN C (1).pptx
functions of C++

Similar to Introduction to functions in C programming language (20)

PPTX
functions
PPTX
UNIT3.pptx
PPT
C++ Language
PPTX
C and C++ functions
PPTX
Function (rule in programming)
PPT
eee2-day4-structures engineering college
PPT
Functions123
PPT
Functions12
PDF
4th unit full
PPT
Part 3-functions
PPT
C++ Functions.ppt
PDF
Bartosz Milewski, “Re-discovering Monads in C++”
PPTX
C++11: Feel the New Language
PPTX
C concepts and programming examples for beginners
PPTX
unit_2 (1).pptx
PPTX
Silde of the cse fundamentals a deep analysis
PPT
L4 functions
PPTX
Cs1123 8 functions
PPTX
Functions in C++ (OOP)
functions
UNIT3.pptx
C++ Language
C and C++ functions
Function (rule in programming)
eee2-day4-structures engineering college
Functions123
Functions12
4th unit full
Part 3-functions
C++ Functions.ppt
Bartosz Milewski, “Re-discovering Monads in C++”
C++11: Feel the New Language
C concepts and programming examples for beginners
unit_2 (1).pptx
Silde of the cse fundamentals a deep analysis
L4 functions
Cs1123 8 functions
Functions in C++ (OOP)
Ad

More from ssuserbad56d (11)

PPTX
Dictionaries in Python programming language
PPTX
OOP in Python Programming: Classes and Objects
PPTX
Software Testing and JUnit and Best Practices
PPT
search
PPT
search
PPT
Scaling Web Applications with Cassandra Presentation.ppt
PPT
Cassandra
PPT
PPTX
Covered Call
PDF
Lec04.pdf
PDF
Project.pdf
Dictionaries in Python programming language
OOP in Python Programming: Classes and Objects
Software Testing and JUnit and Best Practices
search
search
Scaling Web Applications with Cassandra Presentation.ppt
Cassandra
Covered Call
Lec04.pdf
Project.pdf
Ad

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
Pharma ospi slides which help in ospi learning
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Open folder Downloads.pdf yes yes ges yes
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pre independence Education in Inndia.pdf
Cardiovascular Pharmacology for pharmacy students.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
102 student loan defaulters named and shamed – Is someone you know on the list?
How to Manage Starshipit in Odoo 18 - Odoo Slides
UPPER GASTRO INTESTINAL DISORDER.docx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Open Quiz Monsoon Mind Game Prelims.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Anesthesia in Laparoscopic Surgery in India
The Final Stretch: How to Release a Game and Not Die in the Process.

Introduction to functions in C programming language

  • 2. Introduction to Functions • Building blocks of modular programming • Break complex problems into smaller, manageable pieces • Promote code reuse and maintainability
  • 3. Function Syntax • Basic structure of a C function: return_type function_name(parameter_list) { // function body return value; }
  • 4. Function Declaration vs Definition • Declaration (prototype) • Definition int add(int x, int y); int add(int x, int y) { return x + y; }
  • 5. Function Parameters • Parameters are variables used to pass data to functions • Pass by value: void modify(int x) { x = x + 1; // Only modifies local copy } int main() { int a = 5; modify(a); // a remains 5 return 0; }
  • 6. Pass by Reference • Using pointers to modify original values: void modify(int *x) { *x = *x + 1; // Modifies original value } int main() { int a = 5; modify(&a); // a becomes 6 return 0; }
  • 7. Global Variables • Declared outside all functions • Accessible throughout the program int globalVar = 10; // Global variable void function1() { globalVar++; // Can access and modify } void function2() { printf("%d", globalVar); // Can read }
  • 8. Local Variables • Declared inside functions • Scope limited to the containing block void function() { int localVar = 5; // Local variable { int blockVar = 10; // Block scope } // blockVar not accessible here } // localVar not accessible here
  • 9. Static Local Variables • Retain value between function calls void counter() { static int count = 0; // Initialized only once count++; printf("%dn", count); }
  • 10. Variable Scope Rules • Scope hierarchy example int global = 10; void function() { int local = 20; { int local = 30; // Hides outer local printf("%dn", local); // Prints 30 } printf("%dn", local); // Prints 20 }
  • 11. Stack Frames • Memory organization for function calls • Each function call creates a new stack frame • Contains: • Local variables • Parameters • Return address • Saved registers void function(int x) { int y = x + 1; // Stack frame contains: // - Parameter x // - Local variable y // - Return address }
  • 12. Function Call Stack • Example of multiple function calls: void function3() { int z = 3; } void function2() { int y = 2; function3(); } void function1() { int x = 1; function2(); }
  • 13. Recursive Function Call int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); } // Each recursive call creates a new stack frame
  • 14. Memory Layout • Program memory organization: • Text segment (code) • Data segment (global variables) • Stack (function calls) • Heap (dynamic allocation)
  • 15. Stack Overflow • Example of dangerous recursion: void infinite_recursion() { int large_array[1000]; // Local array infinite_recursion(); // Will cause stack overflow }
  • 16. Return Values • Common return types: void, int, float, char, etc. • Different ways to return data: • Simple type • Complex type: pointers • Struct type int return_value() { return 42; } void return_pointer(int* result) { *result = 42; } struct Point return_struct() { struct Point p = {1, 2}; return p; }
  • 17. Function Pointers • Store address of functions, can be used to call such functions int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int main() { int (*operation)(int, int); operation = add; printf("%dn", operation(5, 3)); // Prints 8 operation = subtract; printf("%dn", operation(5, 3)); // Prints 2 return 0; }
  • 18. Function Pointers • Function pointers can be used as parameters: int apply(int (*func)(int), int value) { return func(value); } int square(int x) { return x * x; } // Usage: int result = apply(square, 5); // Returns 25
  • 19. Functions with varied parameter list #include <stdarg.h> int sum(int count, ...) { va_list args; va_start(args, count); int total = 0; for (int i = 0; i < count; i++) { total += va_arg(args, int); } va_end(args); return total; }
  • 20. Inline Functions • Optimization hint to compiler: inline int max(int a, int b) { return (a > b) ? a : b; }
  • 21. Recursion • Function calling itself: int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n-1) + fibonacci(n-2); } int factorial_tail(int n, int accumulator) { if (n <= 1) return accumulator; return factorial_tail(n - 1, n * accumulator); }
  • 22. Function Overloading • C doesn't support overloading. Functions cannot have the same name • Alternative approaches: int add_int(int a, int b) { return a + b; } float add_float(float a, float b) { return a + b; }
  • 23. Function Documentation • Using comments effectively: • @brief Calculates the sum of two integers • @param a First integer • @param b Second integer • @return Sum of a and b /** * @brief Calculates the sum of two integers * @param a First integer * @param b Second integer * @return Sum of a and b */ int add(int a, int b) { return a + b; }
  • 24. Error Handling • Return codes and error checking: int divide(int numerator, int denominator, int* result) { if (denominator == 0) { return -1; // Error code } *result = numerator / denominator; return 0; // Success }
  • 25. Function Composition • Combining multiple functions: int process_data(int data) { data = validate(data); data = transform(data); data = format(data); return data; }
  • 26. Callback Functions • Functions as parameters: void process_array(int arr[], int size, void (*callback)(int)) { for (int i = 0; i < size; i++) { callback(arr[i]); } } void print_item(int x) { printf("%dn", x); }
  • 27. Header Files • Function declarations in headers: // math_functions.h #ifndef MATH_FUNCTIONS_H #define MATH_FUNCTIONS_H int add(int a, int b); int subtract(int a, int b); #endif
  • 28. Best Practices • Function design guidelines: • Single responsibility principle • Clear naming conventions • Consistent parameter ordering • Proper error handling • Documentation • Parameter validation • Resource cleanup
  • 29. Summary • Functions are fundamental to C programming • Understanding scope and lifetime is crucial • Stack frame management affects program behavior • Proper function design leads to maintainable code