Program Console Vignaankosh.com
Execution Panel
Console is empty.
Pass function as argument is an interactive C dry run visualizer from the Pointersfunctions programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.
This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.
#include <stdio.h>
// Different transformation functions
int square(int x) { return x * x; }
int cube(int x) { return x * x * x; }
int timesTwo(int x) { return x * 2; }
int plusTen(int x) { return x + 10; }
// Generic function that applies transformation to array
void applyToArray(int *arr, int size, int (*transform)(int)) {
int i;
for(i = 0; i < size; i++) {
arr[i] = transform(arr[i]);
}
}
// Function that returns function pointer based on choice
int (*selectOperation(int choice))(int) {
int (*ops[4])(int) = {square, cube, timesTwo, plusTen};
if(choice >= 0 && choice < 4) {
return ops[choice];
}
return square; // default
}
// Higher-order function: apply operation and print
void processAndPrint(int *arr, int size, int (*op)(int), char *opName) {
int i;
printf("\nApplying %s:\n", opName);
printf("Before: ");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
applyToArray(arr, size, op);
printf("\nAfter: ");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int i;
int numbers[] = {1, 2, 3, 4, 5};
int size = 5;
int temp[5];
int (*op)(int);
// Pass different functions to applyToArray
printf("=== Passing functions as arguments ===\n");
// Square
for(i = 0; i < size; i++) {
temp[i] = numbers[i];
}
processAndPrint(temp, size, square, "square");
// Cube
for(i = 0; i < size; i++) {
temp[i] = numbers[i];
}
processAndPrint(temp, size, cube, "cube");
// Times two
for(i = 0; i < size; i++) {
temp[i] = numbers[i];
}
processAndPrint(temp, size, timesTwo, "timesTwo");
// Return function pointer from function
printf("\n=== Returning function pointers ===\n");
op = selectOperation(1); // Get cube function
printf("cube(5) = %d\n", op(5));
return 0;
}