Вы находитесь на странице: 1из 26

Function

Function

Several function you know:


main ( ) user defined function
printf( ) & scanf( ) library function

Function enables program to be break up into


segments

Function

Advantages:
Dividing the program into separate well-defined
functions enable each function to be written and
tested separately. It can simplifies the process of
getting the total program to work.
when a big program is broken into functions, project
can be divided to different programmers. Each
programmer responsible on different functions.
There are a set of functions in C libraries which are
free to use. It will speeds up the program
development.

Define Function
General form of a function:
Return values data type
DataType function_name(parameter_list)
{
body of code
}
Parameter passing

* Function Definition

Example 1
To create simple function:
Data type

Name

Parameter passing

void hello (void)


{
printf(hello world !\n );
}

Example 1
#include<stdio.h>
void hello(void)
{
printf(hello world !\n );
}
void main()
{
hello();
}

Called function
The C
Function
body outside
Of the main
program

The c function called from


the main program (calling fun

note the use of the key word void.

Main memory & Func memory


#include<stdio.h>
void func ()
{
int two;
two=2;
printf("two is %d\n", two);
}
Two is 2
int main (void)
{
One is 1
int one;
one = 1;
func ();
printf("One is %d\n", one);
return (0);
}

Main memory & Func memory


local in main = 1
void func()
{
x=5
int local,x,y;
local in func = 2
local = 2;
y = 3;
local in main = 1
x = local + y;
printf ("\t x = %d\n", x);
printf ("\t local in func = %d\n", local);
}
int main (void)
{
int local;
local = 1;
printf("local in main = %d\n", local);
func();
printf ("local in main = %d\n", local);
return (0);
}

Question 1
Write

a program to calculate the area


of a circle using function. The
program should allow user to enter
the value of radius and display the
result with proper unit.

Passing parameters /
arguments to the function

When function is called, the calling function may


have to pass some values to the called function.

There are 2 ways to pass the parameter/arguments


to the called function.
(i) call by value values of the variables are passed
by the calling function to the called function.
(ii) call by reference address of the variables are
passed by the calling function to the called function.

Passing parameters /
arguments to the function

The argument names in the function declaration


and function definition need not to be the same
but the data types or the arguments must match.

The arguments in the defined function must place


in the correct order too.

Passing parameters /
arguments to the function
Function may accept as many parameters/arguments as it
need. Function with no parameter passing is fine too.
void prn_message(int k)
{
One parameter passing
int i;
printf( Here is the message:\n");
for (i = 0; i <k; i++)
{
printf( Have a nice day!\n");}
}

Example 2
#include<stdio.h>
void prn_message(int k)
{
int i;
printf ("Here is the message:\n");
for (i = 0; i <k; i++)
{
printf ("
Have a nice day!\n");}
}
int main (void)
{
int n;
printf ("There is a message for you. \n");
printf ("How many times do you want to see it?");
scanf ("%d", &n);
calling function
prn_message(n);
return (0);
}

Passing parameters /
arguments to the function
Function may accept as many parameters as it need.
void prn_message(int k, int j, int m)
{
int i;
printf( Here is the message:\n");
for (i = 0; i <k; i++)
{
printf( Have a nice day!\n");}
}

Example 3
#include<stdio.h>
void print_min(int x, int y)
{
if (x < y)
printf ("min = %d\n", x);
else
printf("min = %d\n", y);
}
int main (void)
{
int a, b;
printf("Input a?");
scanf ("%d",&a);
printf ("Input b? ");
scanf ("%d", &b);
print_min(a, b);
return (0);
}

Question 2
Write

a program to convert time to


minutes using function. Program
should allow user to enter the hours
and minutes and the time in minutes.

Function with return


Statement
The

return statement is used to terminate


the execution of a function and returns
control to the calling function.

return statement may or may not return


a value to the calling function. Function
can only return 1 value.

function having void as its return type


cannot return any value.

Function with return


Statement
Function may return
only 1 value or no value
int min(int a, int b)
{
if (a<b)
return a;
else
return b;
}

return a to main, a (int)

Function with return


multiply(int
x,int y)
Statement

int
{
return (x*y);
}

/* can be return x*y */

int main()
{
int answer;
answer = multiply(10,20);
printf(The answer is %d\n,answer);
}

Prg50_FuncParameter.cpp

Example 4
#include<stdio.h>
int min(int a, int b)
{
if (a<b)
return a;
else
return b;
}
int main(void)
{
int j, k, m;
printf ("Input two integers: ");
scanf ("%d%d", &j, &k);
m = min(j, k);
printf ("\nThe minimum is %d. \n", m);
return (0);
}

int

Function with return


multiply(int
x,int y);
Statement

int main()
{
int answer;
answer = multiply(10,20);
printf(The answer is %d\n,answer);
}

Function happen
after main ( )

int multiply(int x,int y)


{
return (x*y);
/* can be return x*y */
}

Using Functions:
#include<stdio.h>
#include<math.h> /* contains sqrt() function*/
Void main(void)
{
float x,y;
printf(Enter a floating point number: )
scanf(%f,&x);
y = sqrt(x);

//first way

printf(%f\n,sqrt(x));

//second ways

if (y > sqrt(x))
//third ways
{
printf(Value greater than limit);
}
}
Prg51_FuncMulti.cpp

Example 4
#include<stdio.h>
int addTwoDigits(int);
int firstDigit(int);
int secondDigit(int);
int main(void)
{
int number;
int sum;

int addTwoDigits(int number)


{
int result;
result = firstDigit(number) +
secondDigit(number);
return result;
}

int firstDigit(int num)


{
printf ("Enter an integer: ");
return (num % 10);
scanf ("%d", &number);
}

sum = addTwoDigits(number);
int secondDigit (int num)
printf("Sum of last two digits {
is : %d\n", sum);
int result;
result = (num / 10) % 10;
return (0);
return result;
}
}

Function call by reference


Advantages:

- provide greater time and space efficiency


(arguments are not copied into new
variables)
- function can change the value of
argument in the caller.
- function can return only 1 value. If need
to return multiple values, pass the
arguments by reference so the values can
be modified in caller.

Function call by reference


#include<stdio.h>
Void add (int *n);
void main(void)
{
int num = 8;
printf(Number before calling function:%d, num);
add(&num);
printf(Number after calling function:%d, num);
}
void add (int *n)
{
*n = *n + 10;
printf(Number in the call function:%d, n);
}

When you go back

Some of the examples are taken from Northumbria Programs teaching materials

Вам также может понравиться