0% found this document useful (0 votes)
9 views3 pages

Lecture 7 Functions NewLMS

Uploaded by

janithadilsham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Lecture 7 Functions NewLMS

Uploaded by

janithadilsham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

11/30/2023

Outline
CSC 1113 • Functions
Programming Techniques • How Functions Work
Lecture 7: Functions • Parameters and Arguments
• Categories of functions
– Built-in
– User Defined

Mrs.T.D.Gilmini
Senior Lecturer
Department of Computer Science

Learning Outcome of Today’s Lesson Function


• Define functions to perform specified tasks • What is a function in a PL?
– Function is a block of statements that performs a
• Implement functions in C programming specific tasks
language – Encapsulates a complex computations in a single
operations
• Compare parameter pass by value and pass by • A function declaration
reference – tells the compiler about a function's name, return
type, and parameters.
return_type function_name(parameter list );
• A function definition
– provides the actual body of the function.

Function Function
• Calling a Function
• General form of a function 1. the program control is transferred to the called
return_type function_name( parameter_list ) function
{ body of the function } 2. performs a defined task
• Defining a Function 3. When its return statement is executed or ending
– consists of a function header and a function body closing brace is reached, it returns the program
– return_type is the variable type that the function returns. control back to the main program
– function_name is the actual name of the function
– parameter_list is the list of parameters that the function takes
• pass the required parameters along with the
separated by commas function name
• If no parameters are given, then the function does not take any and should be
defined with an empty set of parenthesis or with the key word void /* function declaration */
• body of the function is a collection of statements that define Int max(int num1, int num2);
what the function does /* calling a function to get max value */
ret = max(a, b);

1
11/30/2023

Functions Arguments
• These variables are called the formal parameters
of the function.
• Two ways arguments can be passed
– Call by value
• copies the actual value of an argument into the formal
parameter of the function.
• changes made to the parameter inside the function have no
effect on the argument.
– Call by reference
• copies the address of an argument into the formal
parameter
• Inside the function, the address is used to access the actual
argument used in the call.

Functions Arguments
• Default - call by value to pass arguments
• Ex 1 : Pass by value

Functions Arguments Why Functions?


• Ex 2 : Pass by reference • Code Reusability
– avoid rewriting same logic/code again and again in a
program
• Code Abstraction
– can call functions any number of times in a program and
from any place in a program
• Improve Readability
– dividing a big task into small pieces to achieve the
functionality improves understandability
• Reduce Size of the code
• Easy debugging
– easily be tracked when it is divided into functions

2
11/30/2023

Types of Functions Practice Question


• Standard Library Functions • Write a function that takes two parameters
– built-in functions in C num1 and num2 and returns the maximum
programming (defined in
header files) value between the two.
– E.g.: printf() , sqrt()
• User Defined Functions
– created functions as per
users need
– It reduces the complexity
of a big program and
optimizes the code

#include <stdio.h>
/* function declaration */
int max(int num1, int num2); Recursion
int main () {
/* local variable definition */
int a = 100; int b = 200; int ret;
• if a programming allows you to call a function
inside the same function that is called recursive
/* calling a function to get max value */ call of the function
ret = max(a, b);
printf( "Max value is : %d\n", ret ); • The C programming language supports recursion
return 0; ie. a function to call itself
}
/* function returning the max between two numbers */ – But while using recursion, programmers need to be
int max(int num1, int num2) careful to define an exit condition from the function,
{ otherwise it will go in infinite loop
/* local variable declaration */
int result;
• Recursive function are very useful to solve many
if (num1 > num2) mathematical problems like to calculate factorial
result = num1; of a number
else
result = num2;
return result;
}

Example-Recursion

You might also like