Calling a Function in Main

How to call a function inside main function from library in Python

You need to import main (or rather from main import Main) in second.py, because currently second.py doesn't know what is 'Main'.

However it is not possible to import main in this case, due to a circular dependency issue, for which there are answers here: Circular import dependency in Python

Also be aware that you can't call Main.func2("data"), because func2 is not a @classmethod, main needs to be instantiated first with brackets Main() or use the @classmethod decorator in Main.func2().

In C, calling a function from main

you defined it again..
Just remove the void from the funciton.
include a header a forward declartion so it will recognize it..

void printSum (void); <-------------------

int main(void){

printSum(); <-------------------------
printf("Hi!\n");

return 0;
}

void printSum (void)
{
printf("Please give two integers\n");
int x,y;
scanf("%d %d", &x,&y);
printf("%d + %d is %d\n",x,y,x+y);
}

Calling Functions in Main Function

Ideally you may want to write something like below. Also, your open_file() has to be rewritten.

class Matrix(object):
def open_file(self):
File = input("Enter a filename: ").lower() #Asks user for a file to input
fp = open(File, "r")
return fp

#### Your matrix class code goes here

def main():
myMatrix = Matrix()
fp = myMatrix.open_file()
ret = myMatrix.read_file(fp)
print(myMatrix)

if __name__ == "__main__":
main()

Calling a function returning a String within the main method

Problem in the Code

You named the variable name and the function name, both hi, so the compiler gets confused about which one you mean.

Solution 1

As @Cheersandhth said, do this:

hi = ::hi();

to get the global hi in its global namespace, which is the function.

Live Example



Solution 2



Otherwise, if you want to change the variable name, you could either change the function name, or the variable, to a different value, and then it would work. Here is an example:

int main(int argc, char** argv)
{
string l_hi;
l_hi = hi();
cout << l_hi;
}

Or the alternative:

int main(int argc, char** argv)
{
string hi;
hi = hello();
cout << hi;
}
string hello()
{
return "Hello, World!";
}

Both of these solutions would work, the one you want to use would be a matter of preference. Also, use this answer as advice to not do name shadowing, which could either lead the compiler into picking one of the two names, (provided they fit the right context), or an error may be thrown.



Solution 3

Wait, there's a third solution?

Yes, but solution three is more of a workaround, although I prefer it. Just get rid of the variable itself, and cout like this:

cout << hi(); //So simple!

Call function from Main()

Just put all logic to another class

 class Class1
{
public void test()
{
MethodInfo mi = this.GetType().GetMethod("test2");
mi.Invoke(this, null);
}
public void test2()
{
Console.Out.WriteLine("Test2");
}
}

and

  static void Main(string[] args)
{
var class1 = new Class1();
class1.test();
}


Related Topics



Leave a reply



Submit