Function in C Programming

C Programming | Function

Comment, if answer to the questions highlighted in red are correct, or not. Or if any suggestion.

#include <stdio.h>
int main() {
    cout<<“Hello World!”;
    return 0;
}

/** Function is the code which execute other codes
*Here main is a function
*Here main is a predefined function, library function
*Main is also known as initiator for a program
*A function have () in C programmin.**/

Q. Above program has how many functions?
Ans. Two functions.
1. main()
2. printf()

#include <stdio.h>
void message(); //Declaration
void main() {
        message();//Calling
        printf(“Hello World!”);
}

void message() //Define
{
        printf(“I am in messagen”);
}
Q. What if function is not defined, and called in main( ).
Ans. Main function will generate an error, undefined reference.

Q. Is function declaration necessary?
Ans. Yes, if function defined later of main function.

Q. What if I do not write function declararion?
Ans. It will produce error, if function defined after main().

Q. What if I dont want to write declaration?
Ans. Than define function above main funtion.


#include <stdio.h>
int main() {
    printf(“Hello World!”);
    return 10;
}

Q. What does int mean before function?
Ans. Function should return a value.

Q. What type of value it can return?
Ans. It can return integer value.

Q. What if we return a string value?
Ans. And it produce error, becuase string is not type casted.

Q. What if we return ‘A’ ?
Ans. It will return ASCII interger value of char ‘A’.


#include <stdio.h>
int main() {
        printf(“Welcome”);
        return 10;
    printf(“Hello World!”);
    return 0;
}
Q. Can a function have more than one return statement?
Ans. Yes, a function can have more than one return statement.
But only top most will execute, and function will exit.

Q. In above program, what is the output?
Ans. Welcome.

Q. Why “Hello World” is not printed?
Ans. Because return 10 will exit the function so remaining lines will not run.

#include <stdio.h>
void Ujjain();
void Indore();
void Burhanpur();
int main() {
    printf(“Hello World!”);
    Ujjain(); 
    return 0;
}
void Ujjain()
{
        printf(“I am in Ujjain”);
       void Dhar(); //Declaration
       Dhar();      
}
void Burhanpur()
{
        printf(“I am in Ujjain”);
}
void Indore()
{
        printf(“I am in Ujjain”);
}
void Dhar()
{
         printf(“I am in Dhar”);
}
Q. What if a function declared but not defined?
Ans.No error.

Q. Is there any order of function definition related to function declaretion?
Ans. No

Q. In function call, is written type necessary?
Ans. No

Q. What if i use, void Indore() for function call?
int main() {
    printf(“Hello World!”);
    void Indore();
    return 0;
}
Ans. It will pretend void Indore() as function declaration. Will not call function Indore.

Q. Can a function call any function any time?
Ans. Yes

Q. Can a function be defined in another function body?
Ans. No.

Q. Can a function be declared in another function body?
Ans. Yes
int main() {
    printf(“Hello World!”);
    void Dhar(); // Function declaration
    Dhar();
    return 0;
}

Q. If a function declare in Ujjain() can it be called in Indore()?
Ans. No, because function declaration have a scope.
The scope of a function calling related to scope of function declaration.
If Dhar() is declared in Ujjain(), than only Ujjain() can call the
Dhar().

Q. Can function declaration and function definition have different types?
void Indore();// Function declare
int Indore() // Function define
{
        printf(“I am in Ujjain”);
}
Ans. No, ot gives error.