7/23/2019 Functions Intro to Computer Programming
1/14
7/23/2019 Functions Intro to Computer Programming
2/14
also called subroutines, subprograms
allow complicated programs to be divided
into small blocks divide and conquer
a named sequence of statements thatperforms some useful procedure
is a complete and independent program
7/23/2019 Functions Intro to Computer Programming
3/14
A Function must have:
1. Return type (void if function does not return anything)
2. Name followed by a pair of ()
3. Names follow rules in writing identifiers
4. 0 or more parameters to be placed inside the pair of ()
What is a parameter? - A parameter is a local variable. It has a data typeand a name. It is a special kind of variable that refers to data that asubroutine receives on which to operate
5. Pair of curly braces {} (inside the brackets is the function body)
6. A return value
7/23/2019 Functions Intro to Computer Programming
4/14
Function Syntax:
functionName( )
{
return ;
}
Note that:
If return-type is omitted, C defaults to int. The return-value mustbe of the declared return-type.
functionName follows rules in writing identifiers and is used incalling the function
To use a function, simply call it using the functionName
7/23/2019 Functions Intro to Computer Programming
5/14
A Function may belong to any of the followingtypes:
1. Functions with no parameter and no return
type
2. Functions with parameters and no returntype
3. Functions with parameters and return types
7/23/2019 Functions Intro to Computer Programming
6/14
Function with no parameter and no return type
void drawSquare()
{
// prints a 3x3 square
printf(***\n);
printf(***\n);printf(***\n);
}
Note that:
Function has no return type it is VOID. Function Name is drawSquare No Parameters drawSquare prints / displays a 3x3 square
7/23/2019 Functions Intro to Computer Programming
7/14
Code A: Without usingFunctions
main()
{
printf(***\n);printf(***\n);
printf(***\n);
}
Code B: Using Functions
void drawSquare()
{
// prints a 3x3
squareprintf(***\n);
printf(***\n);
printf(***\n);
}
int main(){
drawSquare();
return 0;
}
7/23/2019 Functions Intro to Computer Programming
8/14
Looking at the comparison you can observethat:
Code A and Code B produces the same output
Code B has 2 functions namely main() and
drawSquare()
Code B uses a function (drawSquare()) toprint the 3 x 3 square
7/23/2019 Functions Intro to Computer Programming
9/14
Functions with parameters and no return type
void addition(int a, int b)
{
// compute for the sum of a & b
int sum;
sum = a + b;
printf(The sum of is %d, sum);
}
Note that:
Function has no return type it is VOID. Function Name is addition Two Parameters a and b both of the type int addition will display the sum of two integers
7/23/2019 Functions Intro to Computer Programming
10/14
Functions with parameters and return type
int addition(int a, int b)
{
// compute for the sum of a & b
int sum;
sum = a + b;
return sum;
}
Note that:
Function has no return type int. Function Name is addition Two Parameters a and b both of the type int addition will return the sum of two integers
7/23/2019 Functions Intro to Computer Programming
11/14
CODE A: Function with parameters andno return type
void addition(int a,int b)
{
// compute for the sum of a & b
int sum;
sum = a + b;
printf(The sum of is %d,
sum);
}
int main()
{
int x = 5 , y = 3;
addition(x, y);return 0;
}
CODE B: Function with parameters andreturn type
int addition(int a,int b)
{
// compute for the sum of a & b
int sum;
sum = a + b;
return sum;
}
int main()
{
int sum, x = 5 , y = 3;
sum = addition(x, y);printf(The sum of is %d, sum);
return 0;
}
7/23/2019 Functions Intro to Computer Programming
12/14
Parameters vs Arguments
int addition(int a,int b)
{
// compute for the sum of a & b
int sum;
sum = a + b;
return sum;
}
int main()
{
int sum, a = 5 , b = 3;
sum = addition(a, b);
printf(The sum of is %d, sum);return 0;
}
Here a & b are calledparameters
Here a & b are called
arguments
So whats the difference?
7/23/2019 Functions Intro to Computer Programming
13/14
Parameter
A piece of information you provide in order to
call a function.
Parameters are like variables in the sense that
they contain values and have types
rgument
A value that you provide when you call a
function. This value must have the same type as the
corresponding parameter.
7/23/2019 Functions Intro to Computer Programming
14/14
Top Related