Close
Close

Factorial using function

   satinath

I did not understand factorial of a number calculation using function . I am calcuting factorial of n . then what a is doing here ?


Answers

  •   

    I think you are talking about this example:

    #include <stdio.h>
    
    int factorial( int a ) /* function */
    {
        if( a == 0 || a == 1)
        {
            return 1;
        }
        else
        {
            return a*factorial(a-1);
        }
    }
    
    int main() 
    {
        int n; 
    
        printf("Enter number\n");
        scanf("%d", &n);
    
        int fact = factorial(n);
    
        printf("Factorial of %d is %d\n", n, fact);
        return 0;
    }
    

    A function can be defined by using any name of the variable(s). It represents the number and the type of variables to be passed. For example, factorial(int a) means that this function will be called by passing one integer and the passed integer to this function will stored in the variable having the name ‘a’.

    While calling the function:

    factorial(4) – We have passed one integer to the function as demaned by the function ‘factorial’ and this passed value 4 will be stored in the variable ‘a’ inside the function.

    factorial(4) can also be called as:

    int n;

    n = 4;

    factorial(n)

    So, passing a variable to function is one thing and by which variable this passed variable will be represented is another thing and depends on how we have defined the function.

    Thus after calling factorial(n), the vaule of the variable ‘a’(in the structure of the function – factorial(int a)) will be equal to the passed value that is n.


    • Yes I was going through this problem. Nice explanation .Trying to grasp it. Thanks
      - satinath
    • Hi, I want to know whether in C , code is executed line by line or it can skip or jump to other areas of the code.
      - satinath
    • It can skip or jump when needed.
      - Amit Kumar

Ask Yours
Post Yours
Write your answer