Chapter 5 - 2014-2015
Chapter 5 - 2014-2015
CEIT-22014
YTU CEIT 1
Functions
❑ What is a function?
- A function is a sub-unit of a program which performs a
specific task.
- A function can return at most one argument.
YTU CEIT 2
Why use Functions?
❑ Avoids rewriting the same code over and over
❑Easier to write programs and keep track of what they are
doing
YTU CEIT 3
Simple Function
(1) Function Declaration
(2) Calling a Function
(3) The Function Definition
YTU CEIT 4
Function Definitions
❑ Defining a Function
YTU CEIT 5
#include <stdio.h>
int italy();
int brazil();
int argentina();
❑ Call function
main()
{ - Any function can be
printf("\nI am in main");
italy(); called from other
printf("\nI am finally back in main");
functions
}
italy()
{
printf("\nI am in italy");
brazil();
printf("\nI am back in italy");
}
brazil()
{
printf("\nI am in brazil");
argentina(); Program Output
}
I am in main
argentina()
I am in italy
{
I am in brazil
printf("\nI am in argentina");
I am in argentina
}
I am back in italy
I am finally back in main
YTU CEIT 6
#include <stdio.h>
}
message()
{
printf("\nJewel Thief!");
}
Program Output
Jewel Thief!
Jewel Thief!
YTU CEIT 7
Passing Values between Functions
2 // Finding the sum of three integers */
3#include <stdio.h>
❑ Outline
4
5 ▪ Input values
6
7 main()
▪ Call function
8 { ▪ Function
9 int a, b, c, sum ;
10 printf( “\nEnter any three integers: " ); definition
11 scanf( "%d%d%d", &a, &b, &c );
12 sum = calsum(a,b,c); /* calling function to get value */
13
14 printf(“\nSum = %d”, sum);
15
16 }
17
18 /* Function definition */
19calsum( int x, int y, int z )
2020 {
21 int d;
22 d = x + y + z;
23 return(d);
24 ⚫ Program Output
25 }
YTU CEIT 10
Call by Value
2 // Finding the maximum of two Outline
integers */
3#include <stdio.h> ⚫ Values of a and b
4
remain unchanged
5
6
even after exchanging
7 main() the values of x and y
8 {
9 int a = 10, b =20;
10 swapv(a, b);
11printf( “\na=%d b=%d”, a, b);
12
13 }
{
14 /* Function definition */
15swapv(int x, int y)
16 {
17 int t;
18 t = x;
19 x = y;
20 y = t;
21printf(“\nx=%d y=%d”, x, y);
22 }
x = 20 y = 10 ⚫ Program Output
a = 10 b = 20 YTU CEIT 11
Call by Reference
2 // Finding the maximum of two integers */ Outline
3#include <stdio.h>
4
5
⚫ To exchange the
6 values of a and b
7 main()
8 {
using their addresses
9 int a = 10, b =20; stored in x and y
10 swapr(&a, &b);
11printf( “\na=%d b=%d”, a, b);
12
13 }
14 /* Function definition */
15swapr(int *x, int *y)
16 {
{
17 int t;
18 t = *x;
19 *x = *y;
20 *y = t;
21 printf(“\nx=%d y=%d”, x, y);
22 }
a = 20 b = 10 ⚫ Program Output
YTU CEIT 12
Call by Reference
#include < stdlib.h>
#include < stdio.h> areaperi(int r, float *a, float *p)
{
main() *a = 3.14*r*r;
{ *p = 2 * 3.14 * r;
int radius; return(*a, *p);
float area, perimeter; }
printf("Area=%f", area);
printf("\ nPerimeter= %f", perimeter);
system("pause"); Enter radius of a
} circle 5
Area = 78.500000
Perimeter = 31.000000
YTU CEIT 13
Introduction to Pointers
• A pointer refers to a memory location that contains an
address.
• The C programming language provides a special
mechanism for passing variables to functions that is a
little unintuitive, but extremely powerful.
• C provides two operators, & and *, which allow you to
pass a variable instead of its value.
YTU CEIT 14
Introduction to Pointers
• A pointer must be declared and the variable type it
points to must be specified.
Example: int *ptr;
• Address Operator (&) : An address is assigned to a
pointer using the address operator.
Example: prt_v=&x;
• Indirection Operator (*): A value is assigned to a
variable it points to using the indirection operator.
Example: *ptr_v = 77;
YTU CEIT 15
Pointer Notation
⚫ Consider the declaration
int i = 3;
(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
i Location name
3 Value at location
YTU CEIT 16
Example
#include <stdio.h>
main()
{
int i =3;
printf(“\ n Address of i= %u”, &i);
printf(“\ n Value of i = %d”, i);
}
Output
Address of i = 2293572
Value of i = 3 17
YTU CEIT
Con’t
• Example:
main() {
int x = 7 ;
int *px ; /* px is a pointer to an integer */
px = &x ; /* px gets the address of x */
printf( "%d\n" , x ) ; /* show the value of x */
printf( "%p\n" , &x ) ; /* show the address of x */
printf( "%d\n" , *px ) ; /* show the value of what px points to */
}
YTU CEIT 18
Limitations of Pointers
• If memory allocations are not free properly it can cause
memory leakages
YTU CEIT 19