functions_in_c_step_by_step tutorial with examples
fizzamansoor3
6 views
17 slides
Oct 21, 2025
Slide 1 of 17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About This Presentation
functions_in_c_step_by_step tutorial with examples
Size: 43.74 KB
Language: en
Added: Oct 21, 2025
Slides: 17 pages
Slide Content
Functions in C — Step by Step A concise, student-friendly PowerPoint Created by ChatGPT
Outline What is a function? Function declaration vs definition vs call Function syntax and types of parameters/returns Pass-by-value vs pass-by-reference (pointers) Recursion and examples Scope, storage, and linkage Step-by-step example: write → compile → run Common errors and best practices Exercises
What is a function? A self-contained block of code that performs a specific task. Helps organize code, improve reuse and readability. Example roles: calculation, input/output wrapper, helper routines.
Declaration vs Definition vs Call Declaration (prototype): tells the compiler about function signature. Definition: the actual body/implementation of the function. Call: invoking the function from another place in code. Example: Declaration: int add(int a, int b); Definition: int add(int a,int b){ return a+b; } Call: int s = add(3,4);
Function syntax & return types General form: return_type function_name(parameter_list) { /* body */ } Return types can be any valid C type or void for no return. Parameter list can be empty or include typed parameters. Examples: void printHello(void); float area(float r); int *buildArray(int n); // returns pointer
Pass-by-value (default in C) C passes arguments by value — copies of values are passed. Modifying a parameter inside function doesn't change caller variable. Use pointers when you need to modify caller's data.
Pass-by-reference using pointers (example) // swap using pointers #include <stdio.h> void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } int main() { int x = 5, y = 10; swap(&x, &y); // x becomes 10, y becomes 5 printf("x=%d y=%d\n", x, y); return 0; }
Recursion Function that calls itself — useful for divide-and-conquer and tree algorithms. Must have a base case to stop recursion. Example: factorial
Factorial (recursive) #include <stdio.h> int fact(int n) { if (n <= 1) return 1; return n * fact(n-1); } int main() { printf("5! = %d\n", fact(5)); return 0; }
Scope, storage and linkage Local variables: declared inside function — automatic storage (stack). Static variables: persist across calls, file-level if declared static. Global variables: accessible across functions (avoid if possible). Linkage: extern for cross-file access; static to limit linkage.
Write → Compile → Run (step-by-step) 1) Write source file: functions.c 2) Compile with gcc: gcc -Wall -Wextra -std=c11 -o functions functions.c 3) Run: ./functions (on Windows: functions.exe) 4) Fix compile warnings/errors and re-run.
Full example: calculator functions #include <stdio.h> int add(int a,int b) { return a+b; } int sub(int a,int b) { return a-b; } int mul(int a,int b) { return a*b; } float divide(int a,int b) { return b==0 ? 0.0f : (float)a/b; } int main() { int x=10,y=3; printf("add=%d sub=%d mul=%d div=%.2f\n", add(x,y), sub(x,y), mul(x,y), divide(x,y)); return 0; }
Common errors & debugging tips Missing prototype when function used before definition. Add prototypes. Type mismatch in parameters/return — enable compiler warnings. Uninitialized local variables. Segfaults from invalid pointer use. Use printf debugging or gdb for step-through.
Best practices Keep functions short and single-purpose. Use clear, descriptive names. Prefer const for read-only parameters where possible. Use prototypes in header files for modular code. Document behavior, side-effects, and ownership (who frees memory).
Exercises (try these) 1) Write a function to check if a number is prime. 2) Implement Fibonacci: iterative and recursive versions; compare performance. 3) Write a function that returns dynamically allocated array of first n squares. 4) Implement string reversal using pointers.
References & further reading K&R: The C Programming Language (useful classic). GCC documentation and compiler warnings (-Wall -Wextra). Online tutorials and man pages (man gcc, man 3 printf).
Thank you Questions? You can ask me for: - a version with speaker notes - a shorter handout - or a version tailored for beginners/advanced students