× Home
Next Lec → ← Previous Lec

What is a Function?

Now let’s understand in simple terms what function is? Function is nothing but a group of code put together and given a name and it can be called anytime without writing the whole code again and again in a program. I know its syntax is bit difficult to understand but don’t worry after reading this whole information about Functions you will know each and every term or thing related to Functions.

Syntax

return_type funciton_name(data_type parameter 1, data_type parameter2,...){
//code to be executed
return value;
// instead of return_type we can also write 'void' means the function doesn't return anything.
}

Advantages of Functions :

Function Aspects :

Types of Functions

Function code examples

Arguments are the value which is passed to function

                    

                            #include <stdio.h>
                            int sum(int a, int b); // this is called function prototype declaring before defining it.
                            int main()
                            {
                                int a, b, c;
                                a = 9;
                                b = 87;
                                c = sum(a, b); // function call
                                printf("%d", c);
                                return 0;
                            }
                            int sum(int a, int b) // function declaration
                            {
                                return a + b;
                            }
                    
                

Without arguments and without return value :

In this function we don't have to give any value to the function (argument/parameters) and even don't have to take any value from it in return.

// No Argument and No Return Value void Star_pattern() // when function is not returning anything we use 'void' { int a; printf("Enter how many stars(*) you want : \n"); /* In this both declaration and definition of function are done together.*/ scanf("%d", &a); for (int i = 0; i < a; i++) { printf('*'); } }

As you can see in above example the function with name 'Star_pattern' is not taking any argument (Since it's parentheses are empty) and even it is not returning any value as we haven't even used return keyword for returning something and in return_type we have written 'void' which means nothing. So, with this example you can understand about functions which do not take any argument or value and even don't return anything.

Without arguments and with return value

In these type of functions we don't have to give any value or argument to function but in return we get something from it i.e. some value.

                        

                            // Without arguments and with return value :
                            int Sum(); /* Declaration of Function */
                            
                            /*Other Code*/
                            int Sum()
                            {
                            
                                int x, y, z;
                                printf("Enter no. 1 : \t");
                                scanf("%d", &x);
                                printf("\nEnter no. 2 : \t");
                                scanf("%d", &y);
                                z = x + y;
                                return z;
                            }
                        
                    

In above example you can see that our function with name 'Sum' is not taking any value or argument (Since parentheses are empty) but it is returning an integer value. So, that's how we can use functions which don't take any argument but returns something.

With arguments and without return value

In these type of function we give arguments or parameters to function but we don’t get any value from it in return.

                        

                            // With arguments and without return value :
                            void Product(int x ,int y ); /* Declaration of Function */
                            
                            // Other Code
                            Product(5,4); /* Calling Product Function in main() */
                            
                            
                            void Product(int a,int b)
                            {
                                int Multiplication;
                                Multiplication = a*b ; /* Definition of Function */
                                printf("The Product is %d\n\n",Multiplication);
                            }
                        
                    

In this example as you can see we have declared function globally i.e. this function can be used anywhere in our program not just only in main() function. After that we have defined it to tell what this function will do and in this function as it will take some value or argument, it will process that data and will give some output but it will not return anything. When we pass arguments in function at the time of calling it at that time only the value of variables get copied to variable of that function means only values are passed. It means any modification which is done on passed values do not affect the actual values of the variables in main() function. And this call is known as Call by Value.

With arguments and with return value

In these functions we give arguments to function and in return we also get some value from it.

// With arguments and with return value : float Percentage(int x,int y); // Declaration of Function /*Code*/ int x; x = Percentage(80,95); // Calling Function /*Code*/ float Percentage(int x,int y) { float perct; perct = ((x+y)/200.0)*100.0; // Definition of Function return perct; }

So, this is the widely used method to define function in this way we give arguments to function and also get some value in return from it. So, that's all about Functions in C Language.

                


                        #include <stdio.h>
                        void multi(int a);
                        int main()
                        {
                            int num = 0;
                            printf("Enter the number whose multiplication table you want : ");
                            scanf("%d", &num);
                            multi(num);
                            return 0;
                        }
                        
                        void multi(int a)
                        {
                            printf("The multiplication table of :%d\n", a);
                            for (int i = 1; i < 11; i++)
                            {
                                printf("%d X %d = %d \n", a, i, a * i);
                            }
                        }