Skip to main content

Functions:---

Definition:-
  • it is basically a piece of code
  • it has name for identification
  • a function can be called multiple times to maintain the reusability and modularity.
  • by writing function we can avoid same writing/piece of code
Function aspects:- 3 aspects of function are
1.function declaration:- a function must be declared globally in c , to tell the compiler about function name, function return type, function parameter.
         Syntax:- return_type function_name(arguments or data_types parameter);
            example:-  void sum(int a,int b);
2.function call:- a function can be called anywhere from the program, pass the same no. of parameter as we write in function declaration.
       Syntax:- function_name(argument_list)
           example:-  sum(a,b)
3.function definition:- it contains the actual statements which is executed, when a function is called.
   
     Syntax:- return_type function_name(argument_list)  {  function body ; }
             example:-  void sum(int a, int b){ int c=a+b;}
note:- the return_type and argument_list in function write same, in function calling on function name is required which must be same as function_declaration.


Function type:- there are 2 types of function
  1. predefined or library function:- are those function which are already defined in the c header files such as printf(), scanf(), getch(), puts(), gets(), ceil(), floor(), etc.
  2. user-defined function:- are those function which can be defined by the programmer.so that the programmer use it multiple times. 

Different ways to define a function calling:- 

  • takes nothing return nothing
  • takes something return nothing
  • takes nothing return something
  • takes something return something
1) takes nothing return nothing:-in this a function is created which have no return_type (i.e return nothing) or no arguments in definition(takes nothing) and in function_call also.
Explanation:- here first function is declared with name add. which have void i.e no_return type and also doesn't have any arguments.
after declaration function definition is provided which takes no parameter and return nothing(i.e return type is void).
last inside main body a function is called which pass nothing and takes nothing.  

2)takes something return nothing:- in this a function which takes the parameter and use them but return nothing.
Explanation:- first declare a mult function which takes one argument of int data types but return nothing.
second the declared function is defined where argument b =n(i.e 4) enter any no. multiply it with an entered number.
third to execute a function , it is called inside main body from where one argument is passed, which is n=4.

3)takes nothing return something:- in this a function is created which have no argument but return some value. 
Explanation:-
1) function sum is declared with return_type int(i.e it return integer value when a function is called, which we need to store ) and takes nothing because there is no argument.
2) inside main function a function is called, and assign it to a result variable because we know that this function return integer value which is store in result variable, after storing result is printed
3) now function definition, defining what is return, it return the addition of two variable a and b.

4)takes something return something:-in this a function is created which return a value and also takes the arguments.

Please share with your friends      














Comments

Popular Posts

CALL BY VALUE AND REFERENCE--

A function can be called by two types 1) Call By Value:-   when an original value is not modified , is known as call by value. In call by value the actual parameter(the parameter passed to function) is copied into the formal parameter(parameter receive by function). in this the value of each variable in calling function is copied into the corresponding dummy variable of the called function. the changes made to the dummy variable have no effect on the actual variable. Example:-   2)Call By Reference:-   when an original value is modified, is known as call by reference.in this a reference is used , denoted by &(symbol). we can use either pointer or reference for call by reference in this the address of the variable is passed into the function called by actual parameter. in this the memory allocation is same for both actual and formal arguments. Example:-   

Static Memory Allocation

Statement are of two types:- Declaration:- in which we declare the variable Action:- in which for,if-else,getch,printf,scanf etc are used. SMA(STATIC MEMORY ALLOCATION):-                                whenever we create a program,there is a line of declaration statement(or data type declarartion statement) such as int a;char b;etc. Due to this statement we create a variable a,now this variable is the example of SMA.SMA means how much memory is consumed by variable at the time of compiler,this decision is taken before the compilation. At the compile time these variable such as a and b does not having a memory.Memory is given when program is run i.e when object file convert into .exe file.But when program is run how much memory is taken by the variable is decided at the compilation time.This is known as SMA(Static Memory Allocation).   

Compilation Process in C

Compilation The conversion of source code into object code is known as compilation.This process is done with the help of compiler. below is a full diagram of showing how source code convert into software(i.e software development in c) Explanation:- Source code :- the c programming coding is known as source code. save your programe with .c extension then your file become source file for example in fig: sum.c is a source file in which source code is written to print a hello. Prerocessor :- first we need a preprocessor software to convert source file (i.e .c file).Preprocessor handle all those statement which is starting from # symbol. Header file :-  In our programe as seen clearly in sum.c we did not create any header file (i.e which start from # symbol) we just use it.Preprocessor add the content of header file in our source file and creates a new file this file same as our source file but only difference is that the header file removed from the new file which ...