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 7811061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:01:15+00:00 2026-06-02T04:01:15+00:00

Some programming language allow parameters have default value in function call or attributes in

  • 0

Some programming language allow parameters have default value in function call or attributes in struct has default. Like python or javascript, when we omit some parameters in function call, the default value will be replaced in the function (same as struct can have default in attribute as well). So how does compiler actually handle this? particular in symbol table? I think adding one more additional attribute for pointer pointing to default value also I guess default value pushed on stack on run-time? Please let me know if I am correct. Thanks.

Update:
Here default value I mean say in Python, following program parameter b has default value 0.

def foo(a, b=0):
    return a+b
print foo(1)
print foo(1, 1)

we will get result 0 and 1. So how is this handled by compiler in run-time or compile-time?

  • 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-02T04:01:18+00:00Added an answer on June 2, 2026 at 4:01 am

    When the argument list of a function call is evaluated, the code generator can tell what arguments are missing by comparing the list to the function signature stored in the symbol table. Once you’ve discerned which argument needs to be replaced, it’s just a matter of throwing the default value into the argument registers (or stack frame). If you’re dealing with an OO language and the formal parameter is an object, you might push 0 (NULL) into the next word of the stack. If the parameter is a built-in primitive, you’ll push whatever default value the language specifies.

    Of course compilers can vary, but in a simplistic one, it’s not absolutely necessary to associate a default value in the symbol table. First, lets be clear that a symbol table records all variable declarations. You must store information about those declarations (e.g. class, method, type, line, char, etc.), but surely there’s no need to record a default value for each variable when there are only but a few types, and thus only a few possible default values.

    Some languages (Java/C++) specify that a class with an uninitialized object attribute has a default value of NULL. In implementing something like that, when the code generator creates the class constructor, it’ll be sure to generate code that places a 0 in the object memory mapped to that attribute (assume everything is pointer based). When you go to generate the constructor, and you’re iterating over the class attribute list (an AST node), if there’s no initialization expression for that attribute, call a method to do the default.

    private void genConstructor(int classId) {
    
         //allocate new object
    
         codeGen.write("li $a0, "+classId); 
         codeGen.write("jal Runtime.newObject");
    
         //call parent constructor
    
         codeGen.write("move $a0, $v0"); 
         codeGen.write("jal ParentInit");
    
         //initialize the attributes this class has declared
    
         for(Attributes a: getAttributes(classId)) {
    
            //provide default value
            if(a.getInitExpr() == null)
                doDefault(a.getType(), a.getNum());
            else
                doInit(a.getInitExpr(), a.getNum());
         }
    }
    
    // An 'Int' default value is 1, everything else is 0 (Objects included)
    // $t0 has the object address
    // attributes start at the 5th word of each object 
    private void doDefault(String type, int attrNum) {
         switch(type) {
              case "Int": {
                    codeGen.write("sw $one, "+(5+attrNum)+"($t0)");
              }
              default: {
                    codeGen.write("sw $zero, "+(5+attrNum)+"($t0)");
              }
         }
    }
    

    Update:

    I am wondering how compiler handle the default value set by programmer
    instead of “default” for “class” or “type”.

    I’m assuming you’re speaking of something similar to default args in C++ constructors. In that case, as Ira mentioned, making a field in the symbol table record sounds like the simplest way to go about it. When generating the code for the missing argument, look up the default value in the table.

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

Sidebar

Related Questions

Some programming languages (like haskell ) allow cyclic dependencies between modules. Since the compiler
so I'm making some basic programming language (just for exercise). I've made simple grammar
I'm doing some research on the Groovy programming language, and despite all the information
I'm writing a converter between some dialects of the same programming language. I've found
I have code produced by our proprietry system in the Eltron Programming Language: This
I first learned web programming with php a while back. It has some features
I would like to have a go at making some simple games for personal/learning
It seems like in most mainstream programming languages, returning multiple values from a function
I've been looking into how programming languages work, and some of them have a
Hey everyone! I am new to blackberry programming although I do have some java

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.