From 9841c11d2cc4b8a0ad7282255fd8944f93f89a1b Mon Sep 17 00:00:00 2001 From: Satantago Date: Fri, 7 Oct 2022 00:32:05 +0200 Subject: [PATCH 1/3] Pointer function arguments --- 08-pointers/pointer-function-arguments.c | 76 +++++++++++++++++++++++- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/08-pointers/pointer-function-arguments.c b/08-pointers/pointer-function-arguments.c index d78a4f2..db26445 100644 --- a/08-pointers/pointer-function-arguments.c +++ b/08-pointers/pointer-function-arguments.c @@ -1,15 +1,85 @@ /* ------------------------------------------------------------------------------------ - Tutorial: brief description of tutorial content + Tutorial: This tutorial explains the concept of function pointers as argument in C. + + We cannot pass a function as an argument to another function, but we can pass the + reference of a function as a parameter by using a function pointer. This process is + known as call by reference. + + C programming allows you to create a pointer pointing to the function, which can be + further passed as an argument to the function. We can create a function pointer as follows: + +(return_type) (*point_name) (parameter1, parameter2, ...); + +In the above syntax: + -> return_type: the variable type which is returned by the function + -> *pointer_name: the function pointer + -> parameter1, parameter2, ... : the list of parameters passed to the function + +e.g: + float (*add)(); this is a valid declaration for the function pointer + float *add(); this is an illegal declaration for the funciton pointer + +A function pointer can also point to another function, or we can say that it holds +the address of another function. + +We can then pass the function pointer as a parameter: + + 1. We need a function F1 that takes as a parameter a function pointer of type TYPE1. + 2. We need a function of type TYPE1, meaning it has the same return type and same type + of arguments in order. Let's call this function Function2 + 3. We declare a pointer function of type TYPE1. Let's call it FunctionPointer. + 4. We assign Function2 to FunctionPointer: FunctionPointer = Function2 + 5. We then call function F1 with FunctionPointer as a parameter. + +We shall see an illustration of this in the example below. ------------------------------------------------------------------------------------ */ // Code here explaining concept with comments to guide +#include + +// function declaration +int add(int a, int b) +{ + return a + b; +} + +// another function declaration +void display(int (*p)()) +{ + for (int i=1; i <= 5; i++) + { + printf("%d\n", p(i, i+1)); + } +} + int main() { + /* declaration of a pointer to a function that returns a integer and takes as arguments + 2 integers */ + int (*a)(int, int); + // assigning address of add() to the function pointer "a" + a = add; // Now, 'a' is a pointer pointing to the add() + + // We can call the add() function by using the pointer, i.e., 'a'. + print(a(2, 3)); // prints add(2,3) = 5.0 + + + // How we can pass a function pointer as parameter + + void (*p)(int, int); // void function pointer declaration + printf("The values are :"); + display(p); // prints 3, 5, 7, 9, 11, 13 each in a new line + return 0; } + /* ------------------------------------------------------------------------------------ - Challenge: list challenges to be completed here. minimum of one challenge per tutorial + Challenge: + 1. Write a function that prints "I love pointers!" + 2. Declare a corresponding function pointer and assign the previous function to it. + 3. Write a second function with the correspoing function pointer type as parameter. + 4. Call the second function with the first function as a parameter. ------------------------------------------------------------------------------------ -*/ +*/ \ No newline at end of file From 8d8b8e8cf010b721c3fb513c70cd89685a612b19 Mon Sep 17 00:00:00 2001 From: Satantago Date: Fri, 7 Oct 2022 01:54:34 +0200 Subject: [PATCH 2/3] Pointer function arguments --- 08-pointers/pointer-function-arguments.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/08-pointers/pointer-function-arguments.c b/08-pointers/pointer-function-arguments.c index db26445..4edb1b3 100644 --- a/08-pointers/pointer-function-arguments.c +++ b/08-pointers/pointer-function-arguments.c @@ -82,4 +82,4 @@ int main() 3. Write a second function with the correspoing function pointer type as parameter. 4. Call the second function with the first function as a parameter. ------------------------------------------------------------------------------------ -*/ \ No newline at end of file +*/ From 83ca5f52a12341798e9ff6831dc228bc641a8e48 Mon Sep 17 00:00:00 2001 From: Satantago Date: Fri, 7 Oct 2022 02:02:55 +0200 Subject: [PATCH 3/3] Pointer function arguments --- 08-pointers/pointer-function-arguments.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/08-pointers/pointer-function-arguments.c b/08-pointers/pointer-function-arguments.c index 4edb1b3..2022418 100644 --- a/08-pointers/pointer-function-arguments.c +++ b/08-pointers/pointer-function-arguments.c @@ -45,12 +45,17 @@ int add(int a, int b) return a + b; } +void f(int x) +{ + printf("%d\n", x); +} + // another function declaration -void display(int (*p)()) +void display(void (*p)(int)) { for (int i=1; i <= 5; i++) { - printf("%d\n", p(i, i+1)); + p(i); } } @@ -63,14 +68,15 @@ int main() a = add; // Now, 'a' is a pointer pointing to the add() // We can call the add() function by using the pointer, i.e., 'a'. - print(a(2, 3)); // prints add(2,3) = 5.0 + printf("%d", a(2, 3)); // prints add(2,3) = 5.0 // How we can pass a function pointer as parameter - void (*p)(int, int); // void function pointer declaration + void (*p)(int); // void function pointer declaration + p = f; printf("The values are :"); - display(p); // prints 3, 5, 7, 9, 11, 13 each in a new line + display(p); // prints 1, 2, 3, 4, 5 each in a new line return 0; }