Calling another function from printf?

后端 未结 2 1448
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 00:39

I am doing a simple challenge from a book on C/Objective-C where I have to write a program that computes and displays the square of an integer, while also putting the numbers i

2条回答
  •  不思量自难忘°
    2021-01-22 01:20

    "I called my squareOfInteger function from my printf statement. Is this advisable though?"

    There is nothing wrong in calling the function from within a function. It depends on case, where you say need to retain the squared value (as not in your case where you just need to print the value) and perform other operations on it. like:

    int value = myFun(100);
    int nextVal = myNextFun(value);
    

    "The other thing nagging at me is whether or not I need to reiterate the type as being an int inside the parameter of my function before my variable number, or whether it is enough to declare the result as being of type int, and leaving out type in the parameter, which I have done below."

    Yes you should use define the type of number (in your case as by default it would be integer type and would fail if you have it of other type) as a best practice as it will make code more clear and readable.

提交回复
热议问题