0% found this document useful (0 votes)
31 views29 pages

FUNCTIONS

- Functions allow programmers to divide a program into smaller, reusable modules to simplify programs and avoid duplicating code. - A function is defined with a return type, name, and parameters. It contains a block of code that performs a specific task. - Functions can be called multiple times from different parts of a program. Arguments are passed into functions during calls, while parameters receive these arguments. - This document provides examples of functions that take arguments with and without return values, as well as functions without arguments that do and do not return values. Functions increase code reuse and modularity.

Uploaded by

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

FUNCTIONS

- Functions allow programmers to divide a program into smaller, reusable modules to simplify programs and avoid duplicating code. - A function is defined with a return type, name, and parameters. It contains a block of code that performs a specific task. - Functions can be called multiple times from different parts of a program. Arguments are passed into functions during calls, while parameters receive these arguments. - This document provides examples of functions that take arguments with and without return values, as well as functions without arguments that do and do not return values. Functions increase code reuse and modularity.

Uploaded by

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

FUNCTIONS

Introduction
• There are many situations where we might need to write same line of
code for more than once in a program.
• This may lead to unnecessary repetition of code, bugs and even
becomes boring for the programmer.
• So, C language provides an approach in which you can declare and
define a group of statements once in the form of a function and it can
be called and used whenever required.
• Writing a program using one function(main) is possible but the
program can be very complicated(difficult to debug)
What is a function?
• C function is a self-contained block of statements that can be executed
repeatedly whenever we need it.
• For instance, main(), printf(), scanf() ,,, etc. are some of the built-in
functions in C programming language. Also we have user defined functions.
Syntax
Return_Type Function_Name (Parameters)
{
Local Variable Declaration;
Logic;
Executable Statement 1;
……
}
Why do we deed a function?
• Modularity:- Dividing a big program into small modules. Using functions, the
problem can be divided into small understandable and manageable steps.
• Reusability:- Write once, use many times. Functions avoid repeating code in a
program. One function can be called and used in several places.
• For example printf function, the printf function is defined only one time in
stdio.h header file, but we use it many times in our program.
• Simplicity:- Easy to read instructions. Using functions our program becomes
very simple and understandable.
• Efficiency:- Program performance increased. Usage of functions reduces the
amount of work and development time. Using function we can easily find the
errors in our program.
Parts of Functions
i. Function Prototype (function declaration)
ii. Function Definition
iii. Function Call
Function Prototype (function declaration)
• Syntax:
dataType functionName (Parameter List)
Example: int addition(int a, int b);
• It is written outside/ above the main function.
Function Definition
• Presents the actual logics of the program.
Syntax
returnType functionName(Function arguments)
{
local variable declarations;
executable statements;
return;
}
Example
int addition() { }
Function Call
• When a function is called then control passes to that of the called
function.
• Once the execution of function completed then the control returned
back to the caller function.
• Generally, a function will process information passed to it from the
calling statement of a program and return a single value.
• A function with a returned type void does not return any value. It only
returns the control from called function to calling function.
• Syntax: function name(parameter list);
int Add(int a, int b)
Function Parameters
• Actual parameters- Written in function call
• Formal parameters- Written in function definition.
• If there are no arguments to be passed to the function, function will
return nothing/void.
Arguments vs Parameters
• Information passed to the function via special identifiers or
expression are called arguments.
• The variables that are declared in the header of the function
definition are called parameters.
• Sometimes arguments and parameters are also called actual
parameters and formal parameters respectively.
Example: Additional of two numbers
#include<stdio.h>
/* function declaration */
int addition();
int main()
{
/* local variable definition */ int answer;
/* calling a function to get addition value */ answer = addition();
printf("The addition of the two numbers is: %d\n",answer);
return 0;
}
/* function returning the addition of two numbers */int addition()
{
/* local variable definition */ int num1 = 10, num2 = 5;
return num1+num2;
}
Example2: Subtraction of two numbers
#include <stdio.h>
int Num_subtraction( int i , int j ); // prototype for the function
intmain()
{
int num1 , num2 , output ;
printf( " Please enters the 2 numbers you want to subtract : " ) ;
scanf( "%d %d" , &num1 , &num2 ) ;
output = Num_subtraction( num1 , num2 ) ; //function call
printf( " The subtraction of the given numbers is = %d " , output ) ;
return 0 ;
}
int Num_subtraction( int i , int j )// function definition
{
int results ;
results = i - j ;
return results ;
}
/* Example for Functions in C Programming */
#include <stdio.h>
// Function Declaration
void Average ( float, float, float );
int main( )
{
float a, b, c;
int x = 4, y= 6, z =5;
printf ("\nPlease Enter 3 Number to find Sum & Average \n");
scanf ( "%f %f %f", &a, &b, &c ) ;
Average (a, b, c); //function call
Average (x, y, z); //function call
}
void Average ( float x, float y, float z) //function definition
{
float Sum, Average;
Sum = x + y + z;
Average = Sum/3;
printf ("\n Sum of %.2f, %.2f and %.2f = %.2f", x, y, z, Sum );
printf ("\n Average of %.2f, %.2f and %.2f = %.2f \n", x, y, z, Average);

}
• We have called Average () one more time, this time, we passed local
variables as function arguments.
• We called 2 times this shows that we can call the function many times
from other functions rather than repeating the codes.
Desription
• It is a C function declaration. If you forget this function declaration,
the compiler will throw an error.
void Average ( float, float, float );
• The below statement will ask the user to enter 3 numbers
printf ("\n Please Enter 3 Number to find Sum & Average \n");
• The below statement will store the user input values in a, b, c
variables
scanf ( "%f %f %f", &a, &b, &c ) ;
• In the next line, we called the function.
Average (a, b, c);
• When the compiler reaches this function, it will traverse to the top to
check for the Average () function.
• If the function fails to identify the function with the Average name, then
it will throw an error.
• In this case, while traversing upwards, it will stop at
void Average ( float, float, float );
• The above function declaration will take the compiler to the below
function
void Average ( float x, float y, float z );
• First, this function will check for the arguments, and it will only
execute if
• Number of arguments passing to the function is equal to the declared
function arguments
• Data types of the arguments passing to the function are equal to the
declared function arguments
• Within the C function, we declared 2 local variables Sum and Average.
• In the next line, we calculated the sum and average of three numbers
using Assignment Operators
Sum = x + y + z;
Sum = 10 + 20 + 30 = 60
Average = Sum / 3;
Average = 60 / 3 = 20;
• The below printf statements are used to print the sum and average to
the output
printf ("\n Sum of %.f, %.f and %.f = %.2f", x, y, z, Sum );
printf ("\n Average of %.2f, %.2f and %.2f = %.2f\n", x, y, z,
Average);
More Examples in Functions
• Addition, substruction, multiplication, …..
• Calculator
Types of function definition
• We have four(4) types of function definition;
i. Function with arguments with return values
ii. Function with arguments without return values
iii. Function without arguments with return values
iv. Function without arguments without return values
Parameters
• The parameter is referred to as the variables that are defined during a
function declaration or definition.
• These variables are used to receive the arguments that are passed
during a function call.
• These parameters within the function prototype are used during the
execution of the function for which it is defined.
• These are also called Formal arguments or Formal Parameters.
Arguments
• An argument is referred to the values that are passed within a
function when the function is called.
• These values are generally the source of the function that require the
arguments during the process of execution.
• These values are assigned to the variables in the definition of the
function that is called.
• The type of the values passed in the function is the same as that of
the variables defined in the function definition.
• These are also called Actual arguments or Actual Parameters.
Function with arguments with return values

• Example
• The input from the user is passed to the checkPrimeNumber()
function.
• The checkPrimeNumber() function checks whether the passed
argument is prime or not.
• If the passed argument is a prime number, the function returns 0. If
the passed argument is a non-prime number, the function returns 1.
• The return value is assigned to the flag variable.
• Depending on whether flag is 0 or 1, an appropriate message is
printed from the main() function.
Explanation
• The input from the user is passed to the checkPrimeNumber()
function.
• The checkPrimeNumber() function checks whether the passed
argument is prime or not.
• If the passed argument is a prime number, the function returns 0.
• If the passed argument is a non-prime number, the function returns 1.
The return value is assigned to the flag variable.
• Depending on whether flag is 0 or 1, an appropriate message is
printed from the main() function.
Function with arguments without return
values
Example
• The integer value entered by the user is passed to the
checkPrimeAndDisplay() function.
• Here, the checkPrimeAndDisplay() function checks whether the
argument passed is a prime number or not and displays the
appropriate message.
Function without arguments with return
values
• Example
• The empty parentheses in the n = getInteger(); statement indicates
that no argument is passed to the function.
• The value returned from the function is assigned to n.
• Here, the getInteger() function takes input from the user and returns
it.
• The code to check whether a number is prime or not is inside the
main() function.
Function without arguments without return
values
• Example
• The checkPrimeNumber() function takes input from the user, checks
whether it is a prime number or not and displays it on the screen.
• The empty parentheses in checkPrimeNumber(); statement inside the
main() function indicates that no argument is passed to the function.
• The return type of the function is void. Hence, no value is returned
from the function.Example

You might also like