Sunteți pe pagina 1din 26

Definition

 A function is a set of statements that take inputs, do


some specific computation and produces output.
 The idea is to put some commonly or repeatedly done
task together and make a function, so that instead of
writing the same code again and again for different
inputs, we can call the function.
Category
 Predefined Functions e.g. printf(), scanf()
 User-Defined Functions
Use of Functions
 Organization
 Reusability
 Testing
 Extensibility
 Abstraction
Function Declaration
 Function declaration tells compiler about number of
parameters function takes, data-types of parameters
and return type of function.
// A function that takes two integers as parameters
// and returns an integer
int max(int, int);

// A function that takes a char and an int as parameters


// and returns an integer
int fun(char, int);
What is the purpose of a function
prototype?
 The Function prototype serves the following purposes

1) It tells the return type of the data that the function


will return.
2) It tells the number of arguments passed to the
function.
3) It tells the data types of the each of the passed
arguments.
4) Also it tells the order in which the arguments are
passed to the function.
Function Definition
 Function definition is nothing but actual Function.
 Function Call is short term used, however function
definition is “Actual Broad Elaboration” of function
call.
 Function definition is different from macro expansion.
 It contain Executable Code ( Executable statements )
Syntax
return-type function-name(parameters)
{
declarations
statements
return value;
}
Example
Function Calling
 function_name(Parameter1 ,Parameter2 ,....Parameter
n);
Calling a Function :
 Call a C function just by writing function name with
opening and closing round brackets followed with
semicolon.
 If we have to supply parameters then we can write
parameters inside pair of round brackets.
Example
#include<stdio.h>
int sum(int n1,int n2)
{ return(n1+n2); }

int main()
{ int num1=11,num2=22;
int result;
result = sum(num1,num2);
printf("\nResult : %d",result);
return(0);
}
Explanation
Function Nane sum

Return Type Integer

Calling Function main

Parameter Passing Method Pass by Value

No of Parameters Passed to Function 2

Actual Parameter 1 num1

Actual Parameter 2 num2

Formal Parameter 1 n1

Formal Parameter 2 n2

Function Will Return n1+n2

Returned Value will be Catch in result


Example
#include <stdio.h>
// An example function that takes two parameters 'x' and 'y’ as input and returns max of two
input numbers
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}

// main function that doesn't receive any parameter and returns integer.
int main(void)
{
int a = 10, b = 20;

// Calling above function to find max of 'a' and 'b'


int m = max(a, b);

printf("m is %d", m);


return 0;
}
Some Points about functions
 Any C program contains at least one function.
 A function can not be defined inside other function.
 There is no limit on the number of functions that
might be present in a C program.
 Each function in a program is called in the sequence
specified by the function calls in main( ).
 After each function has done its thing, control returns
to main( ).When main( ) runs out of function calls,
the program ends.
Call by value & Call by reference
 Whenever we call a function we pass something to it, if
we pass the ‘values’ of variables to the called function.
Such function calls are called ‘call by value’.
It means on calling a function we are passing values of
variables to it. The examples of call by value are :
sum = calsum ( a, b, c ) ;
 We also know that variables are stored somewhere in
memory. So instead of passing the value of a variable, we
can also pass the location number (also called address) of
the variable to a function. This is called call by reference.
For this we should have knowledge of pointers.
Pointer Notation
int i=3;
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.
Printing Address
We can print this address number through the following program:
main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
‘&’ used here is C’s ‘address of’ operator. The expression &i
returns the address of the variable i, which in this case
happens to be 65524. Since 65524 represents an address,
there is no question of a sign being associated with it. Hence
it is printed out using %u, which is a format specifier for
printing an unsigned integer.
‘*’ operator
The other pointer operator available in C is ‘*’,
called ‘value at address’ operator. It gives the value
stored at a particular address. The ‘value at address’
operator is also called ‘indirection’ operator.
printf(“Value of i=%d”,*(&i));
In last program this line will print
Value of i =3
Note that printing the value of *( &i ) is same as
printing the value of i.
Pointer
 We can also write in C as:-> j=&i
 Here j is a variable which stores the address of i.
 j will also be stored at some memory location suppose
address of j is 65522 and value at j is address of i that is
65524
Pointer (contd..)
 As we can see, i’s value is 3 and j’s value is i’s address.
 But we can’t use j in a program without declaring it. And
since j is a variable that contains the address of i, it is
declared as, int *j ;
 This declaration tells the compiler that j will be used to store
the address of an integer value. In other words j points to an
integer. So j is a pointer
int *j ;
 Meaning of * is ‘value at address’.
Thus, int *j would mean, the value at the address contained
in j is an int.
Example
main( )
{
int i = 3 ; Output:-
int *j ; Address of i = 65524
Address of i = 65524
j = &i ;
Address of j = 65522
printf ( "\nAddress of i = %u", &i ) ; Value of j = 65524
printf ( "\nAddress of i = %u", j ) ; Value of i = 3
printf ( "\nAddress of j = %u", &j ) ; Value of i = 3
printf ( "\nValue of j = %u", j ) ; Value of i = 3
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
}
Call by value
#include <stdio.h> Output:
void fun(int x)
x = 20
{
x = 30;
}

int main(void)
{
int x = 20;
fun(x);
printf("x = %d", x);
return 0;
}
Call by Reference
# include <stdio.h>
void fun(int *ptr)
{ Output:
*ptr = 30; x = 30
}

int main()
{
int x = 20;
fun(&x);
printf("x = %d", x);

return 0;
}
Swaping by Call by Value
void main()
{ int a=10,b=20;
printf("value of a and b before swaping is%d %d",a,b);
swap(a,b);
printf("\n value of a and b after swaping function is%d %d",a,b);
}
swap(int x,int y)
{ int t;
printf("\n value of x and y before swaping is%d %d",x,y);
t=x;
x=y;
y=t;
printf("\n value of x and y after swaping is%d %d",x,y); }
Swaping by Call by Reference
void main()
{ int a=10,b=20;
printf("value of a and b before swaping is%d %d",a,b);
swap(&a,&b);
printf("\n value of a and b after swaping function is%d %d",a,b);
getch();
}
swap(int *x,int *y)
{ int t;
t=*x;
*x=*y;
*y=t;
}

S-ar putea să vă placă și