You are on page 1of 41

UNIT -2

CONTINUATION

FUNCTIONS
*Definition
• A function is a sub-program that performs a
specific task.
• A function is a set of statements that take
inputs, do some specific computation and
produces output.
• A function is a block of code which only runs
when it is called.
Types of functions in C programming

• There are two types of functions in C


programming:
• Standard library functions / Built – in
Functions- eg: printf(), scanf() getchar() etc
• User defined functions
Standard library functions

• The standard library functions are built-in


functions in C programming to handle tasks
such as mathematical computations, I/O
processing, string handling etc.
• Eg:scanf(), fprintf(), getchar() ,printf()
User-defined functions

• C allow programmers to define functions. Such


functions created by the user are called user-
defined functions.
• Depending upon the complexity and
requirement of the program, you can create as
many user-defined functions as you want.
*Structure of C Functions
return_datatype function_name([ type1 arg1, type2 arg2 ... ])
{
Local variable declaration; - [variables declared inside function]

Statements;
-----------------
-----------------
return(expression);
}
• return_datatype - Denotes the datatype of the
result returned by the function.
• Eg: int ,float etc...
• function_name- represents the name of the
function. function name is an identifier and should
be unique
• type1 arg1, type2 arg2 ... - are the list of arguments
or variables separated by commas.
• There is no semi colon(;) after the closing
parenthesis.
• Local variable declaration - Are the variable
declaration within the function.
• Return statement- Is used to send back values from
the sub program or function to the main function.
*Local & Global variables
• Local Variables – The variables which are
declared within the function is known as local
variables.
• Global Variables – The variables which are
declared outside the function is called global
variables.
How user-defined function works?
#include <stdio.h>

main()
{
int a,b
functionName(); ----------------------Function Call(calling function)

}
Return_Datatype functionName() -------------------(called function)
{
int x,y ;  local variables\return (exp);
-----
return(exp);
}
• The execution of a C program begins from
the main() function. When the compiler
encounters functionName(); inside the main
function, control of the program jumps to void
functionName() And, the compiler starts
executing the codes inside the user-defined
function.
• The control of the program jumps to statement
next to functionName(); once all the codes
inside the function definition are executed, it
will return the value to the calling function.
*Advantages of function
• The program will be easier to understand,
maintain and debug.
• Reusable codes that can be used in other
programs.
• A large program can be divided into smaller
modules. Hence, a large project can be divided
among many programmers.
Example: User-defined function
**Types Arguments/Parameters

• Actual Arguments: The arguments


appearing in the function call are known as
actual arguments.

• Formal Arguments: The arguments that


appear in the function header or function
definition is known as formal arguments.
Example: Actual & Formal Parameters
*Function prototype
• A function prototype is simply the declaration of a
function that specifies function's name, parameters
and return type. It doesn't contain function body.
Syntax of function prototype
Return dataType functionName(type1 argument1, type2
argument2,...);
In the above example,
int addNumbers(int a, int b); is the function
prototype
• name of the function is addNumbers()
• return type of the function is int
• two arguments of type int are passed to the
*Calling a function or Function Call
• Control of the program is transferred to the
user-defined function by calling it.
Syntax of function call
• functionName(argument1, argument2, ...);
• In the above example, function call is made
using addNumbers(n1,n2);
*Function definition
• Function definition contains the block of code
to perform a specific task i.e. in this case,
adding two numbers and returning it.
• Syntax of function definition
return dataType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
When a function is called, the control of the program is
transferred to the function definition. And, the compiler starts
executing the codes inside the body of a function.
Eg:
int addNumbers(int a,int b)// function definition, a and b are formal arguments
{
int result;
result=a+b;
return result; // return statement
}
***
**Passing parameters to a function
• The parameters to a function can be passed in
two ways.
-- Call by Value
-- Call by Reference
*Call by Value
• In call by value, the actual parameters will be
in the form of variables, constants or
expressions. The process of passing variable
values to the called function is called call by
Value
*Call by Reference
Call by Reference is the process of passing
memory addresses of the actual arguments to
the called function rather than passing their
values.
**RECURSION
A function that calls itself again and again until
the condition is satisfied is known as a recursive
function, and this technique is known as
recursion.
**Command Line Arguments
• The arguments that we pass on to main() function
during the runtime or the time of execution or when
the program is invoked are called
“Command Line Arguments”.
• These arguments are handled by main() function.

• When main() has arguments then it is called


command line arguments.
eg: void main(int argc, char *argv[ ])
The function main( ) has 2 arguments: argc and argv

• argc - Argument count is the first argument


of the main function. It is used to count the
no. of arguments passed to the main function.
It is of integer type.
• argv – Argument vector is the second
parameter. It is basically an array of character
pointer which we use to list all the command
line arguments.
Pointers
• Pointers (pointer variables) are special
variables that are used to store address of
another variables.
Any variable which is declared and initialized has
three things associated with it.
1. It has a name.
2. It has a value
3. It has an address
Pointer declaration:
• We can declare a pointer variable by using this syntax

datatype *pointervariable ;

Where datatype is the type of data.


* Represents the variable is of pointer and
The pointervariable is the name of the pointer.
Eg:- int *ptr;
int *p;
int *q;
*Operators used with pointers:
• There are 2 basic operators used with
pointers. They are

• The address operator (&) : it gives the address


of a variable.
• The Pointer operator (*) : It gives the value of
the variable that the pointer is pointing to.
Assigning Address to pointers
Assignment:
A pointer variable can be used to assign the
address of the another variable.
Ex: int *ptr, var;
ptr = &var;
Advantage of pointer

• Pointer reduces the code and improves the


performance.
• It makes to access any memory location
Pointers and Arrays
• Pointers can be used to access the elements in an array.
• Pointers helps in faster execution.
• The array name itself is the address of the start of the array. (base
address)
• It can be assigned to a pointer variable.
int a[5]={1,2,3,4,5};
int *p;
p= a; (same as p = &a[0];

• Then the successive elements can be accessed by just


incrementing the pointer.
p++; (now p will point to a[1])
Access array elements using pointers
#include<stdio.h>
void main()
{
int a[5] = {10,20,30,40,50};
int i;
int *ptr;
ptr = a;
for(i=0;i<5;i++)
{
printf(“Element = %d, Address = %u\n”,*ptr,ptr);
ptr++;
}

You might also like