Chap v. Functions2015-16
Chap v. Functions2015-16
• In modular programming the various tasks that your overall program must
accomplish are assigned to individual functions and the main program basically
calls these functions in a certain order.
Main program is mainly a wiring together of function calls.
1
Functions
• Functions
Enable you to break a program into smaller pieces (modularization).
Each function performs a specified task. It may receive input data and
return output data.
All variables declared inside functions are local variables
• Known only in that function
Parameters
• Communicate information between functions
2
Functions
3
Start with an example
if ( a > b ){
bigger = a;
}
else{
bigger = b;
}
• While this may not seem like a lot of code, it can get very ugly and
make the program difficult to follow. It would be much nicer to say something
like:
bigger = imax(a,b); 4
C functions, cont.
5
Predefined functions
• C Standard library functions or simply C Library functions are inbuilt
functions in C programming.
• Function prototype and data definitions of these functions are written in their
#include <stdio.h>
int main()
{ /*If you write printf() statement without including header
file, this program will show error. */
printf(“This is a c program”); }
6
Predefined functions
• There are many library functions available in C(++) programming to help the
programmer to write a good efficient program.
7
Predefined functions
Ex.: Math header functions (methods)
8
USER DEFINED FUNCTIONS
• In order to use functions, the programmer must do three things
Define the function
Declare the function
Use the function in the main code (function call).
• The following
is the general program format:
9
Function prototype
• The general format for a prototype is simple:
Return-type function_name(arg_type arg1,………..arg_type argN);
arg_type just means the type for each argument -- for instance, an int, a
float, or a char. It's exactly the same thing as what you would put if you
were declaring a variable.
• There can be more than one argument passed to a function or none at all (where
the parentheses are empty), and it does not have to return a value.
• Functions that do not return values have a return type of void
10
Function definition
11
Function definition
• If the function returns a value then the type of that value must be specified in
return-type. For the moment this could be int, float or char. If the function does not
return a value then the return-type must be void.
12
Function definition
Function with no return type and no parameter
• Many of the programs we have seen use only main function. But as your
program get bigger you can improve their readability by subdivide them in
different functions
Example:
void hello(void)
{
printf (“Hello, every body”);
}
13
Function definition
Function with no return type and no parameter
• In programming, when you use a function, it said that “ you call ” that function.
The following program calls the function hello:
# include <stdio.h>
void hello(void);
{
printf(“Hello, every body\n”);
}
void main(void)
{
hello();
}
14
Function definition
Function with no return type and no parameter
• To well understand, we modify the main party of the program
# include <stdio.h>
void hello(void); // Function prototype
int main(void)
{
printf(“Ready to call the function\n”);
hello();
printf(“return to the main program\n”);
return 0;
}
// function definition
void hello(void)
{
printf(“Hello every body\n”);
}
15
Function definition
Declaring variables within function definition
# include <stdio.h>
void three_hello(void);
{
int compter; //variable
for(compter=1; compter<=3; compter++)
printf(“Hello, every body\n”);
}
void main(void)
{
three_hello ();
}
16
The main Function
• For C, the main function of a program is the first instruction that the program
execute.
All what you can do in the main function, you also can do it in any other
function (variable declaration, using instruction if, while, and even call other
function). Ex:
• The function three_hello would be powerful if you could specify the number the
message will be displayed.
18
Parameters & arguments
• To use an argument, the function have to indicate the type and the name of the
argument
• Example:
repeat_hello(int message_number)
{
// instruction of the function
}
When any function for ex. Main have to call the function repeat_hello, it
must specify (define) a value for the argument
19
Parameters & arguments
The following program shows the topic
# include <stdio.h>
void main(void)
void hello(void)
{
{
printf(“display 2
printf(“Hello every body\n”)
times\n”);
}
repeat_hello(2);
void repeat_hello(int nbr)
printf(“display 5 times\
{
n”);
int cpmt; //variable
repeat_hello(5);
for(cpmt =1; cpmt <= nbr; cpmt++)
}
hello();
}
20
Using many arguments
When a program call the function, it must provide the values for each argument.:
funct_any(20,18.6, ‘A’);
21
Function with return value
• To return a integer value, a function must have been defined as :
22
Function with return value
• The function use return to send the result to the caller
• The returned value can be assigned to a variable or directly used as an argument of
another function:
result = cube(5);
printf (“the cube of 5is %d\n”, cube(5));
• Program using cube function:
# include <stdio.h>
int i_cube(int value)
{
return (value*value*value);
}
void main()
{
printf(“the cube of 3 is %d\n”, i_cube(3));
printf(“the cube of 5 is %d\n”, i_cube(5));
} 23
Recursion Function
• Recursion is a programming technique that allows the programmer to express
operations in terms of themselves
• In C, this takes the form of a function that calls itself.
This makes it sound very similar to a loop because it repeats the same
code, and in some ways it is similar to looping.
24
Recursion Function
Every recursion should have the following characteristics.
1. A simple base case which we have a solution for and a return value. Sometimes
there are more than one base cases.
2. A way of getting our problem closer to the base case. I.e. a way to chop out part
of the problem to get a somewhat simpler problem.
3. A recursive call which passes the simpler problem back into the function.
2 Write a recursive function that computes the sum of all numbers from 1 to n,
where n is given as parameter.
//return the sum 1+ 2+ 3+ ...+ n
int sum(int n)
3. Write a recursive function that finds and returns the minimum element in an
array, where the array and its size are given as parameters.
//return the minimum element in a[]
int findmin(int a[], int n)
26
Data visibility
• Extremely important!
Function can only "see" three types of variables:
Those declared locally within the function
Those passed into the function through it arg list
"Global variables" defined outside of any function
We have not yet used global variables
Any variables defined in other functions (even main) that are not
passed into another function are NOT visible to that function, and it can
not see or manipulate their values!
This behavior is crucial to achieving code modularity.
27
Parameter passing methods
• Recall that a function can have either one return value or no return value
• The return statement only allows you to return 1 value. Passing output variables by
reference overcomes this limitation.
// 3*4+2=14
}
Exercice
1. The program receives 6 numbers from the keyboard, finds the largest number and counts the
occurrence of the largest number entered from the keyboard.
Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4.
37
1. Write a recursion function called “int myprod(int x, int y)”
that take as parameters two integers and return their
product
2. Write a recursion function called “int mydiv (int x, int y)” that
take as parameters two integers and return their integer
division
38
End
39