Skip to main content

Overview Of Functions in C Program #DOTC_Mdb

Overview of Functions in C.

 

Hi Everyone,

Today we will discuss about Function in C and its uses and benefits in C Program. In my Previous blog I discussed about Pointers  so you can use Pointers in Functions as well . So before starting Functions in C please go through my previous blog in below link where you will be able to see the topics in archive folder and it’s easy for you to understand the programs and uses of Functions in C.

dheeraj60.blogspot.com

Let’s Start with Function In C.

Function is a block of statements that performs a specific task, For re usability we use Function in C Program If you are building an application in C language and in one of your program, you need to perform the same task more than once we can use functions.

Uses of Functions in C:

1.     C functions are used to avoid rewriting same code again and again in a program.

2.     No limit in calling C functions to make use of same functionality wherever it is required.

3.     We can call functions any number of times in a program and from any place in a program.

4.     If you write large program in C it can be easily tracked when it is divided into functions

 

There are two Types of  Functions in C .

1.     Pre-defined Function : Pre- Defined Function we use is puts(), gets(),scanf() , printf() etc which we already defined in header file stdio.h so we can use this functions whenever it’s required as per requirement of the program.

 

2.     User – Defined Function : Programmer creates the function its own that is User- Defined function . In User- Defined function Programmer can manipulate the function whenever it’s required and have control over the program.

 

Understand the uses of Functions by example:

Syntax of Function:

return_type  function_name (argument list)

{

     statements – Block of code

}

 

Here  return_type can be used for any data type like int, char , float etc. function_name we can give any suitable name so that we can refer and use it accordingly.In Argument list we can have multiple variable with their data types and finally block of code is set of C statement where we can use programming as per our requirement.

Let’s use this syntax to write one simple program by using User – Defined Function where we can use the function for addition and call the function in program only.

 

Program is as below:

#include <stdio.h>

#include <stdlib.h>

 

int add(int i, int j)

{

     int sum;

     sum = i+j;

    return sum;

}

int main()

{

     int a, b;

     printf("Enter First number: ");

     scanf("%d",&a);

     printf("Enter second number: ");

     scanf("%d",&b);

     int result = add(a,b);

     printf ("Output: %d", result);

 

     return 0;

}

 

Here in above program before main we created one function add and in statement / block of c code we will use the function add.

Arguments  used in Function add before main in function  is add as int add(int i, int j){     int sum;      sum = i+j;  Function return type is integer so we are returning integer as sum . Now in code block under main we use input from user  as integer as  for calling the function add here in the function return type. Integer variable to hold the return value of add function. And for output we simply use function add to  add the number passed by the user.

 

Below is the output of above program in compiler code block.

Here you can see the output as sum of two number by using function . So we can write some another program with different datatype for function and use it in same manner. Let’s  take 1 simple example of finding the square of any number by using function with float data type .

 

Program is as below:

 #include <stdio.h>

#include <stdlib.h>

 

float square ( float i );

 

int main( )

{

    float a, b ;

    printf ( "\nEnter some number for finding square \n");

    scanf ( "%f", &a ) ;

       b = square (a) ;

    printf ( "\nSquare of the given number %f is %f",a,b );

}

 

float square ( float i )

{

    float s ;

    s = i * i ;

    return ( s ) ;

}

Here we  define float square ( float i )  so  main function  program will starts from here and in block code or statement float square ( float i) we define the function and return the function in variable so here we not use return 0  as we already returning value from function square variable s.

Below is the output of above Program in Compiler Code Block.


 

Note : If Function return type is char then function should return a value of char data type and while calling this function the main() function should have a variable of char data type to store the returned value.

Structure of function by using char data type should be as below:

 

char dotc(char ch1, char ch2)

{

   char ch3;

   return ch3;

}

int main()

{

   char c1 = abc('a', 'b');

   }

 

Hope you are able to understand the basic overview of functions and it’s benefits. Please try to write some code by using user defined functions and call it in code and see the benefits and if any doubt let me know.

Function is a big topic as function can be uses as Function – Call by value method and Call by reference method. So in next blog I will come up with both the topics in functions see its uses and benefits then we will also see the uses of Pointers in Functions.

Please read this blog carefully so that it will be easy for you to understand function call by value method and call by reference method.

Thanks.


Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li