CSE 109 Lecture 4
CSE 109 Lecture 4
Computer Programming
Function
Prepared by
Johra Muhammad Moosa
Assistant Professor
CSE, BUET
Modified by
Madhusudan Basak
Assistant Professor
CSE, BUET
Some Obvious Points
• Why Function?
• What is happening there? (See from the perspective of the compiler)
• Again, what should be our strategy?
Function
• Two types of function
– Library function
• scanf, printf, gets, puts, getch, sqrt etc.
– User defined function
Function Structure
ReturnType FunctionName (Parameter List)
{
//Declaration of Local Variables
//Doing some activities
//Return values accordingly
}
A Normal Function
2.c
Parameters
3.c
Parameters
4.c
Parameters
5.c
Parameterized Function
s1
s1 s2
s2 s3
s3 vv
s1 s2 s3 v
5.3
Garbage …… 0.4
Garbage … 10.7
Garbag …
… Garbag
Garbage
5.3 … 0.4 … 10.7 … 22.684
value value e value e value
value
38192
38192 38210
38210 39704
39704 41013
41013
38192 38210 39704 41013
Return Types
6.c
Return Types
7.c
Return Types
8.c
Return
• If no return type specified: default int assumed
• when the return statement is encountered: the function returns the control to
the caller immediately
• return statement can be used without return value
– return ;
– Used mostly by void functions
• If the return value is not assigned to anything it is lost, but no harm done
Return
• More than one values can not be returned
– return a, b;
• Reference can be used
– Discussed later
Function Scope
(Local and Global Variables)
global_variable1.c
Function Scope
(Local and Global Variables)
global_variable2.c
Function Scope
(Local and Global Variables)
global_variable3.c
Function Arguments
Function Arguments
• To take arguments a function must have special variables
• Argument:
– The value that is passed to a function
• formal parameter
– The variable that receive the value of the argument inside the function
• Local variables of a function can not have same name as formal parameters
Where to place the function?
• Above the main function
• Below the main function (but you have to use prototype then!)
Function Prototype
• Function prototype declares a function
– before its use
– prior to its definition
• Ends with semicolon
• Example:
– Function:
void myfunc(void)
{
}
– Prototype
void myfunc(void);
Function Prototype
• Prototype declares three attributes of a function
– Its return type
– Number of parameters
– Type of parameters
• Compiler need to know the type of data returned by the function
• Default int assumed
• Report illegal type conversions
• main does not require prototype
• Function with no parameters should contain the keyword void in prototype
• Variable length argument list
– scanf, printf
– Prototype: int myfunc(int a, …);
Declaration Vs. Definition
• Declaration: specifies the type of the object
– Function prototype
• Definition: causes storage for an object to be created
– Function: which contains the body is definition
– It is legal to define a function fully before its use
• Eliminates need of separate prototype
Function
• C program contains at least one function
• main() must be present exactly once in a program
• No limit on number of functions defined
• There can be function which is defined but not called
• There can be function which is declared but not defined
• One function can not be defined inside other function
• Two or more function can not have same name
• Function and variable name can not be same
• No statements can be written outside of function
• Minimal function is
– dummy(){}
Function Call
• Call by value
– Have no effect on the argument used to call
• Call by reference
– Address of an argument is copied into the parameter
• By default C uses call by value
Why call by reference?
Swap_code.c
Why call by reference? (2)
• What to do if we need to pass an array into the function?
Call_by_reference_1.c
Call_by_reference_2.c
Other tasks inside the function
• What other tasks can be done inside the function?
• We can do all the tasks what we can do inside the main, such as
– Scanning the inputs
– Calling another function etc.
Func_call_from_func.c
Question 1
question_1.c
Question 2
question_2.c
Question 3
question_3.c
Question 4
question_4.c
Some for practices
• Write a C program to find the xth power of any number using function.
• Write a C program to find whether a number is palindrome or not using
function.
• Write a C program to print all perfect numbers between given interval using
functions.
• Write a C program to check whether a number is prime, even/odd, or
perfect using functions.
Recursive Function (Recursion)
Rec1_sum_of_numbers.c
Recursion
• Advantages
• Disadvantages
Factorial
• How to determine Factorial of a number using recursive function?
Rec2_Factorial.c
Fibonacci
• How to determine nth Fibonacci numbers using recursive function?
Rec3_Fibonacci.c
GCD
• How to determine gcd of two numbers using recursion?
Rec4_gcd.c
Example
• Palindrome check
• Tower of Hanoi
• Number of digits
• Power
• Series summation
• Nth Fibonacci calculation
Reference
• TEACH YOURSELF C by Herbert Schildt (3rd Edition)
– Chapter 1 (1.7-1.9)
– Chapter 7 (7.1-7.3)
Thank You ☺