3.3 Functions in C programming

 Functions: -

  • A function is a block of code that performs a specific task.
  • Functions help us to divide a large program into smaller, manageable, and reusable pieces.
  • Every C program must have at least one function called main( ) function where program execution begins.

            Syntax: -


        Functions in C are mainly of two types:

            1. Predefined Functions.

            2. User-Defined Functions.

 

1) Pre-defined Functions: -

  • Pre-defined functions are built-in functions provided by C libraries.
  • These are also known as library functions.
  • It provides predefined functionality to a c program.
  • These functions are available in library function or header functions of c.
  • Predefined functions are defined for c compiler to understand reusability.
  • These functions are available in library built-in directory called “lib”.

            Examples: - 

                                    printf( ) , scanf( ) ,

                                    date( ) , time( ) ,

                                    sin( ) , cos( ) , tan( ) ,

                                    strcpy( ) , strcmp( ) ,

                            floor( ) , ceil( ) , sqrt( ), pow( ), etc..

                     Example: -



                    Output: -


2) User-Defined Functions: -

  • The functions which are created by user or programmer to perform specific tasks are known as User defined functions.
  • They help in making code more organized, readable, and reusable.
  • To define a user defined function, we must follow

                a) Function prototype / function declaration.

                b) Function definition / function implementation.

                c) Function calling.

 a) Function prototype / function declaration: -

  • A function declaration tells the compiler about a function name and how to call a function.
  • A function declaration consist a return type followed by function name and arguments with ‘;’. Here the arguments are called formal arguments. Here return type represents any data type ( int , char , float , double , void). 
  • The actual body of the function can be defined separately.

              Syntax: -



            Example: -


b) Function definition / function implementation: -

  • it is also called as called function.
  • We can write actual statements implementation.
  • It consists of two parts:

            i. Function header: - It consists a return type followed by function name and arguments without                                                               semicolon (;). Here, arguments are called formal arguments.

             ii. Function body: - It includes the behavior or functionality of the function which are enclosed                         between “ {      } “. It includes list of arguments, statements, local variables and return statement.

                Syntax: -


                Example: -


 c) Function calling: -

  • Calling function is a function which contains actual arguments to pass the given values to function definition.

            Syntax: -   


            Example: -    

                                z = sum (10,20);

                                z = 30;

            Note: -

                            Actual arguments: - these are used in function calling.

                            Formal arguments: - these are used in function definition.

             Example: - 


                Output: -



Types of User-defined functions: -

  • User-defined functions are further classified into 4 types based on arguments and return values:

                1. Function with no arguments and no return value.

                2. Function with arguments and no return value.

                3. Function with no arguments and with return value.

                4. Function with arguments and with return value.

 

1) Function with no arguments and no return value: -

  • This type of function does not take any input and does not return any value.
  • If the function is not having arguments, then the information must be read with in the function definition and the function is not having the return value, then the result is displayed in the function definition.

                Example: -


                    Output: -


2) Function with arguments and no return value: -

  • This function takes some input values (arguments) but does not return any value to the calling function.
  • It performs its task using the given input.
  • If the function is having arguments, then the information must have be sent from the main function and the function is not having the return value, then the result is displayed in the function definition.

            Example: -


                       Output: -


3) Function with no arguments and with return value: -

  • This type of function does not take any input, but it returns a value to the calling function.
  • It may read data from the user or perform some calculation and return the result.
  • If the function is not having arguments, then the information must have read with in the main function and the function is having the return value, then the result is displayed in the function definition.

              Example: -

                    Output: -


4) Function with arguments and with return value: -

  • This function takes input values and also returns a result to the calling function.
  • It is the most commonly used type because it can perform operations and send results back.
  • If the function is having arguments, then the information must have be sent from the main( )  function and the function is having the return value, then the result is displayed in the main function.

            Example: -


                   Output: -


Advantages of Using Functions:

  • Code reusability
  • Easier debugging and maintenance
  • Better readability
  • Logical program structure

 

Need for User-Defined Functions: -

  • User-defined functions are very important in C programming because they make programs modular, readable, and easy to maintain.

 1. Modularity: -

  • Functions help divide a large program into smaller, manageable parts called modules.
  • Each function performs a specific task, which makes the program well-organized.
  • Example: Separate functions for input, calculation, and output.

 

2. Code Reusability: -

  • Once a function is written, it can be used multiple times in the same program or other programs.
  • This saves time and avoids rewriting the same code again and again.

 

3. Easy Debugging and Maintenance: -

  • Errors can be found and fixed easily since each function handles a specific task.
  • Updating or improving one function does not affect the entire program.

 

4. Improves Readability and Understanding: -

  • A program with functions is easier to read and understand because each function has a clear purpose and name.
  • Example: Functions like add( ), display( ), or calculateAverage( ) clearly show what they do.

 

5. Avoids Repetition of Code: -

  • Functions prevent duplication of code by allowing the same set of instructions to be executed whenever needed using a simple function call.

 

6. Better Program Structure: -

  • Functions provide a logical structure to programs by dividing them into smaller tasks, making complex programs easier to design and execute.

 

Recursion: -

  • Recursion is the process in which a function calls itself either directly or indirectly to solve a problem.
  • Recursive function breaks the problem down into smaller, identical sub-problems, solves them, and combines the results.
  • A recursive function has two parts:

            1. Base Case: Every recursive function must have a condition to stop calling itself, otherwise it will                                             result in infinite recursion and eventually a stack overflow.

            2. Recursive Case: The part where the function calls itself with a smaller or simpler input.

             Example: -


            Output: -


Advantages of Recursion: -

  • Makes code shorter and cleaner for problems like factorial, Fibonacci, GCD, and tree traversal.
  • Useful for problems that have self-similar structures (like Divide and Conquer algorithms).

 

Disadvantages of Recursion: -

  • Can lead to high memory usage due to function call stack.
  • Slower compared to iterative solutions in some cases.
  • Risk of stack overflow if base case is not reached.

Popular posts from this blog

operators in c programming

2.4 Arrays in c programming