Week12_Functions
Week12_Functions
• Read the values of n and k, calculate the required factorials by calling the
function you defined in the first part three times, and then calculate the
result of the formula by using the results obtained from the factorial function.
Function Definition
functionType functionName ( parameter list )
{
local variable declarations
other C statements body
}
Function type
• Function type indicates the data type of the result produced by the
function. (return)
• It may be int, double, char, etc.
• If the function will return no results, the function type must be void.
Function name
• Function name is the name chosen by the programmer.
• It must be a valid identifier, and it should indicate what the function is
doing
Parameters List
• (type1 parameter1, type2 parameter2, ... typen parametern)
• The parameters provide the means for communicating information
between functions.
• If the function does not have any parameters, void may be written in
place of the parameter list.
Function body
• The body of a function must be written within curly braces. It
may include local declarations and any number of statements.
• All variables declared in function definitions are local variables,
thus, they are known only in the function in which they are
defined.
• A function’s parameters are also local variables
Declaring functions
• Just like other identifiers in C, a function must
be defined before it can be referred (i.e.,
called).
• Thus, in a C program, the place of a function
definition is in between preprocessor
directives and main. Thus, the ordering of
modules in the source file is:
Preprocessor Directives
Function Definitions
Main
Declaring functions
• It is also possible to insert the
function prototype before the main
function, and defining the function
after the main. The function
prototype is the starting line of the
function definition ending with a
semicolon (not including the
function’s body).
• The names of the parameters may not
be written in the prototype, but their
data types must be indicated.
Two Questions:
1. Does the function take parameter(s)?
Functions.. 2. Does the function return a value?
The function total_n inputs the number n, calculates the total of numbers
from 1 to n and outputs the result. Main only calls this function.
1. Functions without arguments that return no value.
Solution (parameter?-No , return?-No)
2. Functions with input arguments that return no value.
Formal Parameters
• General form: void functionname(formal parameter list);
• Function call: functionname(actual parameter list);
The main program inputs n value from user and call the function
total_n. The function total_n calculates the total of numbers from 1 to
n and outputs the result.
2. Functions with input arguments that return no value.
Solution (parameter?-Yes , return?-No)
3. Functions without arguments that returns a single value.
The function total_n inputs n value from user, calculates the total of
numbers from 1 to n and returns the result to main program. In main,
call the function and output the result.
3. Functions without arguments that returns a single value.
Solution (parameter?-No , return?-Yes)
A function Only 1 value
Returning Values can receive
Function
returned
many values
The main function inputs n from user and outputs the result.
𝒏!
𝑪(𝒏, 𝒓) =
𝒓! 𝒏 − 𝒓 !
The values of n and r should be taken from the user in the main program.
Then compute combination by using a function and return the result.
int combination (int n, int r);
Hint: We should use a function factorial to compute combination:
int factorial (int n);
SAMPLE RUN
Enter n and r values:3 2
C(3,2)=3
Exercise 3.
• Write a program to
compute the
combination by using
the formula.
𝒏!
𝑪(𝒏, 𝒓) =
𝒓! 𝒏 − 𝒓 !