0% found this document useful (0 votes)
10 views19 pages

Chapter 5 - 2014-2015

Uploaded by

thutreator229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Chapter 5 - 2014-2015

Uploaded by

thutreator229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming Language I

CEIT-22014

Topic 3: Functions and Pointers

Dr. Khin Khin Zaw

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

return_type function_name(parameter-list) int getsum(int x, int y)


{ {
body of the function return (x +y);
} }

Return type: A function may return a value.


A data type of the result (default int)
void - function returns nothing
Function Name: The actual name of the function
Parameters: The value is referred to as actual parameter or argument
Function body: The function body contains a collection of statements
that define what the function does

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>

int message(); ❑ Call function


main() - A function can be called
{
message(); any number of times
message();

}
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 }

Enter any three numbers: 10 20 30


YTU CEIT 8
Sum = 60
2 // Finding the maximum of two integers */
3#include <stdio.h>
4
❑Outline
Example
5 int maximum( int num1, int num2 ); /* function declaration */ 1. Function declaration
6 (2 parameters)
7 int main()
8 { 2. Input values
9 int a, b, res ;
10 2.1 Call function
11 printf( "Enter two integers: " );
12 scanf( "%d%d", &a, &b ); 3. Function definition
13 res = maximum(a,b); /* calling function to get max value */
14 printf( "Maximum is: %d\n", res );
15 return 0;
16 }
17
18 /* Function maximum definition */
19int maximum( int num1, int num2 )
20 {
21 int result;
22
23 if ( num1 > num2 )
24 result = num1;
25
26 else
27 result = num2;
28
29 return result;
30 }

Enter two integers: 22 85 ⚫ Program Output


Maximum is: 85 YTU CEIT 9
Passing Values between Functions
There are two approaches to passing arguments to a function
They are
(1) Call byValue
(2) Call by Reference

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("\ nEnter radius of a circle");


scanf("%d", &radius);

area, perimeter = areaperi(radius, &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

65524 Location number

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

• If not used properly can make the program difficult to


understand

YTU CEIT 19

You might also like