Академический Документы
Профессиональный Документы
Культура Документы
Function
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
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
Question 1
Write
Passing parameters /
arguments to the function
Passing parameters /
arguments to the function
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
int
{
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
int main()
{
int answer;
answer = multiply(10,20);
printf(The answer is %d\n,answer);
}
Function happen
after main ( )
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;
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;
}
}
Some of the examples are taken from Northumbria Programs teaching materials