💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Introduction
  • Calling a function within another function
  • Method 1
  • Method 2 (recommended)

Was this helpful?

  1. Numerical Programming
  2. Tutorial

Tutorial: Passing a Function, Function callback

PreviousTutorial - Sine TaylorNextTutorial: Nonlinear solver

Last updated 8 months ago

Was this helpful?

Introduction

In this tutorial, we will learn how to pass a mathematical function f(x) as an input argument to another function.

Find derivative of given function

How can you use myfunc() within the function of gradientFunc() that finds the derivative?

  • myFun(x): returns F(x)= 2x^3

  • gradientFunc(myfunc): returns 6x^2

Example:

void gradientFunc(double myfunc(const double x), double x[ ], double dydx[ ], int m);

Calling a function within another function

There are several methods of calling a function within another function.

Method 1

Calling the function that is defined globally or in the same scope

#include "stdio.h"
#include "stdlib.h"
#include "TU_functionCall_header.h"


// These are defined in TU_functionCall_header.h
// double myFunc(const double x);
// void func_call(double xin);

// How to change the function equation in myFunc()
// if myFunc() is defined in library header file?
//
// Q2) What if myFunc2() is needed to be used?
// What do you need to modify in TU_functionCall_header.h ?


void main()
{
	double  xin = 2.5;
	func_call(xin);
}

#pragma once

#include "stdio.h"
#include "stdlib.h"

double myFunc(const double x) {
	double y = x * x;
	return y;
}

double myFunc2(const double x) {
	double y = x * x * x;
	return y;
}

void func_call(double xin)
{
	double y = myFunc(xin);
	printf("Y_out = %f \n", y);
}

Method 2 (recommended)

Pass a function as an input argument to another function.


#include "stdio.h"
#include "stdlib.h"
#include "TU_functionCall_header.h"


// These are defined in TU_functionCall_header.h
// double myFunc(const double x);
// void func_call(double xin);

// Q1) How to change the function equation in myFunc()
// if myFunc() is defined in library header file?
//
// Q2) What if myFunc2() is needed to be used?
 
// ANS: pass the function as an input argument for a higher flexibility

double myFunc(const double x) {
	double y = x * x;
	return y;
}

double myFunc2(const double x) {
	double y = x * x * x;
	return y;
}

void main()
{
	double  xin = 2.0;
	func_call(myFunc, xin);
	func_call(myFunc2, xin);
}
#pragma once

#include "stdio.h"
#include "stdlib.h"



void func_call(double func(const double x), double xin)
{
	double y = func(xin);
	printf("Y_out = %f \n", y);
}


Download the demo source files
Download source files