Docsity

Understanding Functions in C: Definition, Access, Arguments, Prototypes, and Recursion, Slides of Computer Science

Partial preview of the text

Download Understanding Functions in C: Definition, Access, Arguments, Prototypes, and Recursion and more Slides Computer Science in PDF only on Docsity!

FUNCTIONS

Contents

  • Overview
  • Defining a Function
  • Accessing a Function
  • Passing arguments to a Function
  • Function Prototypes
  • Recursion

Defining a Function

  • A function definition has three principal components:
    • The first line
    • The argument declarations
    • The body of the functions
  • First line of a function:
    • It contains the type specification of the value returned by the function
    • Followed by the function name
    • (optionally) a set of arguments separated by commas and enclosed in parenthesis
  • In general terms, the first line can be written as:
  • data-type name (formal argument 1 , formal argument 2 , …, formal argument n)
  • data-type represents the data type of the value which is returned
  • name represents the function name

Defining a Function (Cont.)

  • The formal arguments allow information to be transferred from the calling portion of the program to the function
  • They are also known as parameters or formal parameters
  • The corresponding arguments in the function reference are called actual arguments
  • As they define the information actually being transferred
  • The identifiers used as formal arguments are “local”
  • As they are not recognized outside of the function
  • For example:

int max(int a, int b)

return a>b?a:b;

data type formal parameters

Information is returned from the function to the

calling portion of the program via the return

statement

Accessing a Function

  • A function can be accessed by specifying its name , followed by a

list of arguments enclosed in parenthesis and separated by

commas

  • If the function call does not require any arguments, an empty pair

of parentheses must follow the function’s name

  • The arguments appearing in the function call are referred to as

actual arguments

  • In a normal function call, there will be one actual argument for

each formal argument

  • The actual arguments may be expressed as constants, single

variables, or more complex expressions

  • However, each actual argument must be of the same data type as

its corresponding formal argument

  • For example 1 :
  • Largest of three integer quantities:

Accessing a Function (Cont.)

  • Largest of three integer quantities:

# include<stdio.h>

void main()

{ int a, b, c, d;

scanf(“%d %d %d”, &a, &b, &c);

d = max(a, b);

printf(“\n maximum = %d”, max(c, d));

int max(int x, int y)

int z;

z = (x>=y)? x : y;

return (z);

Accessing a Function (Cont.)

long int fact(); scanf(“%d”, &n); printf(“\n n! = %ld”, fact(n)); } long int fact(int m) { int i; long int prod = 1 ; if (m> 1 ) for (i= 2 ; i<=m; ++i) prod = prod* 1 ; return(prod); } In calling program contains the forward declaration: long int factorial(); (^10)

Accessing a Function (Cont.)

  • This declaration indicates that the function fact , which returns a long integer quantity, will be defined later in the program
  • The declaration is required because the function returns a quantity that is not an ordinary integer, and the function call precedes the function definition

Passing arguments to a Function (Cont.)

int modify (int a) { a = a* 3 ; printf(“\n a=%d (from the function, after being modified)”, a); return; }

Function Prototypes

  • Many C compilers support a more comprehensive system for handling argument specifications in function declarations and function definitions.
  • In particular, the proposed ANSI standard permits each of the argument data types within a function declaration to be followed by an argument name
  • i.e.
  • data-type name(type 1 arg 1 , type 2 arg 2 ,…,type n arg n);
  • where arg 1 , arg 2 ,…, arg n refer to first argument, the second arg, and so on.
  • The declaration written in this form are called function prototypes
  • Their use is not mandatory in C
  • They are desirable
  • However, they further facilitate error checking between the calls to a function and the corresponding function definition
  • For example 4 : int sample (int a, int b);
  • The skeletal outline of a C program is shown below: main()

Recursion

  • A process by which a function calls itself repeatedly, until some specified condition has been satisfied
  • The process is used for repetitive computations in which each action is stated in terms of a previous result
  • There are two conditions that must be satisfied in order to solve a problem recursively: - First, the problem must be written in a recursive form - Second, the problem statement must include a stopping condition
  • For example:
  • Suppose to calculate the factorial of a positive integer quantity
  • The problem is normally expressed as n!= 1 x 2 x 3 x…x n
  • Where n is the specified positive integer
  • This problem can also be express in another way by writing n! = n x (n- 1 )!
  • The above statement is recursive statement in which the desired action (the calculation of n!) is expressed in terms of a previous result (the value of (n- 1 )!, which is assumed to be known
  • Also, we know that 1! = 1 by definition 16

Recursion (Cont.)

  • For example 5 :
  • The complete C program to calculate the factorial of a given positive integer using recursion include<stdio.h> main() {int n; long int fact(int n); printf(“n=“); scanf(“%d”, &n); printf(“n! = %ld\n”, fact(n)); } long int fact(int n) { if (n<= 1 ) return ( 1 ); else return(n * fact(n- 1 ));} 17