0% found this document useful (0 votes)
9 views

CSE 295 Lecture 3 - Function

This document discusses functions in C programming. It covers: 1. The two types of functions - library functions and user-defined functions. 2. The structure of a function including return type, function name, parameters, and function body. 3. Function prototypes and how they declare the return type, number of parameters, and parameter types before a function is defined. 4. Function arguments and parameters, and the differences between them. 5. Best practices for placing functions in a program and the differences between function declarations and definitions.

Uploaded by

Atika Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CSE 295 Lecture 3 - Function

This document discusses functions in C programming. It covers: 1. The two types of functions - library functions and user-defined functions. 2. The structure of a function including return type, function name, parameters, and function body. 3. Function prototypes and how they declare the return type, number of parameters, and parameter types before a function is defined. 4. Function arguments and parameters, and the differences between them. 5. Best practices for placing functions in a program and the differences between function declarations and definitions.

Uploaded by

Atika Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

CSE 295

Computer Programming Techniques


Function
Tanveer Awal, PhD
Assistant Professor
Department of CSE, BUET

1
Function
• Two types of function
– Library function
• scanf, printf, gets, puts, getch, sqrt etc.
– User defined function

2
Function Structure
ReturnType FunctionName (Parameter List)
{
//Declaration of Local Variables
//Doing some activities
//Return values accordingly
}

3
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

4
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);

5
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

6
Where to place the function?
• Above the main function
• Below the main function (but you have to use prototype then!)

7
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

8
A Normal Function

2.c9
Parameters

3.c
10
Parameters

4.c 11
Parameters

12
5.c
Parameterized Function

13
Return Types

14
6.c
Return Types

15
7.c
Return Types

8.c 16
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

17
Return
• More than one values cannot be returned
– return a, b;
• Reference can be used
– Discussed later

18
Function Scope
(Local and Global Variables)

19
global_variable1.c
Function Scope
(Local and Global Variables)

space

20
global_variable2.c
Function Scope
(Local and Global Variables)

21
global_variable3.c
Function Arguments

22
Task
• Write a program that takes the base and exponent as user input and
computes baseexponent. Your program must use a function baseExpo(int base,
int expo) that returns a double value to compute the result (baseexponent).

23
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
• Minimal function is
– dummy(){}

24
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

25
Why call by reference?

Swap_code.c
26
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
27
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
28
Question 1

What if we run with 4 and 5?

scanf(“%d %d”, &x, &y);


z= myfunc(x,y);

29
question_1.c
Question 2

What if we give 60 and 2 as


input?

30
question_2.c
Some for practices
• Write a C program to check whether a number is prime, even/odd, or
perfect using functions.
• Write a C program to print all perfect numbers between given interval using
functions.

31
Recursive Function (Recursion)

32
Rec1_sum_of_numbers.c
33
Recursion
• Advantages
• Disadvantages

34
Factorial
• How to determine Factorial of a number using recursive function?

35
36
Rec2_Factorial.c
Fibonacci
• How to determine nth Fibonacci numbers using recursive function?

Fib(0)=0;
Fib(1)=1;
Fib(n)=Fib(n-1)+Fib(n-2).

37
38
Rec3_Fibonacci.c
Reference
• TEACH YOURSELF C by Herbert Schildt (3rd Edition)
– Chapter 1 (1.7-1.9)
– Chapter 7 (7.1-7.3)

39
Thank You 
Acknowledgement: MB

40

You might also like