Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:23:15+00:00 2026-06-05T16:23:15+00:00

As you know TCL has some mathematical functions such as sin , cos ,

  • 0

As you know TCL has some mathematical functions such as sin, cos, and hypot that are called in expr command with () braces as follows:

puts [expr sin(1.57)]

Now how can I add a function using TCL library functions so that it was called exactly the same way, and was doing something that a certain proc defines.

I would like to clarify my question. Say there is a proc (string) as follows:

proc add { a b } { return [expr $a+$b] } ;# string of a proc

Also I have a TCL interpreter in my C++ code. Now I want get the string of a proc and runtime register a function called add into the tcl::mathfunc namespace (I guess I should use Tcl_CreateObjCommand) so that I could call the following:

puts [expr add(1.57, 1.43)]

How this can be done. Could you please write a simple example. I could not find any example in TCL documentation and in books as well which describe the usage of this command.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:23:16+00:00Added an answer on June 5, 2026 at 4:23 pm

    Creating a function from C isn’t too hard. To do it, you’ve got to write an implementation of a command that will perform the operation, and register that implementation as a command in the correct namespace. (In 8.4 and before, functions were done with a separate interface that was quite a bit nastier to use; the mechanism was wholly overhauled in 8.5.)

    Command Implementation

    Note that the signature is defined, and the ignored parameter is not used here. (It’s really a void * — great when you’re wanting to do things like binding a command to an object — but it simply isn’t needed for doing an addition.)

    static int AddCmd(ClientData ignored, Tcl_Interp *interp, int objc,
            Tcl_Obj *const objv[]) {
        double x, y, sum;
    
        /* First, check number of arguments: command name is objv[0] always */
        if (objc != 3) {
            Tcl_WrongNumArgs(interp, 1, objv, "x y");
            return TCL_ERROR;
        }
    
        /* Get our arguments as doubles */
        if (    Tcl_GetDoubleFromObj(interp, objv[1], &x) != TCL_OK ||
                Tcl_GetDoubleFromObj(interp, objv[2], &y) != TCL_OK) {
            return TCL_ERROR;
        }
    
        /* Do the real operation */
        sum = x + y;
    
        /* Pass the result out */
        Tcl_SetObjResult(interp, Tcl_NewDoubleObj(sum));
        return TCL_OK;
    }
    

    Don’t worry about the fact that it’s allocating a value here; Tcl’s got a very high performance custom memory manager that makes that a cheap operation.

    Command Registration

    This is done usually inside an initialization function that is registered as part of a Tcl package definition or which is called as part of initialization of the overall application. You can also do it directly if you are calling Tcl_CreateInterp manually. Which you do depends on how exactly how you are integrating with Tcl, and that’s quite a large topic of its own. So I’ll show how to create an initialization function; that’s usually a good start in all scenarios.

    int Add_Init(Tcl_Interp *interp) {
        /* Use the fully-qualified name */
        Tcl_CreateObjCommand(interp, "::tcl::mathfunc::add", AddCmd, NULL, NULL);
        return TCL_OK;
    }
    

    The first NULL is the value that gets passed through as the first (ClientData) parameter to the implementation. The second is a callback to dispose of the ClientData (or NULL if it needs no action, as here).

    Doing all this from C++ is also quite practical, but remember that Tcl is a C library, so they have to be functions (not methods, not without an adapter) and they need C linkage.


    To get the body of a procedure from C (or C++), by far the easiest mechanism is to use Tcl_Eval to run a simple script to run info body theCmdName. Procedure implementations are very complex indeed, so the interface to them is purely at the script level (unless you actually entangle yourself far more with Tcl than is really wise).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have some code that is doing an SFTP via expect/tcl. Its something
I know that Tkinter is just a thin layer over Tcl/Tk. Tkinter you can
I know this is something that has been discussed over and over, and I
I know that a fragment's view hierarchy has to be inflated in onCreateView, but
Does python have an equivalent to Tcl's uplevel command? For those who don't know,
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I know that Java have its own garbage collection, but sometimes I want to
I know that I can hijack a form by showing a login form in
I know that immutable objects always have the same state, the state in which
I am trying to list all of the functions a namespace has in it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.